Sunday 4 April 2021

Oracle APEX - export static application files as installation scripts

If You want to export static application files as components from APEX GUI... well, it is not working.

Static application files are provided in component type list... but if You select them and click on GO... a list is empty and You can not select any static file to export:


Also, static application files can not be found in APEX_APPL_EXPORT_COMPS view. If You execute following script for application which contains static application files to get all component types... there are no static app files.

SELECT DISTINCT type_name
FROM apex_appl_export_comps
WHERE application_id = 400
;




Fortunately, there is a way to get those script files using PLSQL and APEX_EXPORT package.

Let's look at following example - application 400 and static file "js/page6100.js". 
First You need to locate exact component file name for desired file with PLSQL code:

DECLARE

    l_files apex_t_export_files;

BEGIN
    --get files
    l_files := APEX_EXPORT.get_application(
        p_application_id => 400,
        p_split => true,
        p_with_translations => true,
        p_with_comments => true
    );

    FOR t IN 1 .. l_files.count LOOP
        dbms_output.put_line(l_files(t).name);
    END LOOP;

END;


Result:

Then You can add IF statement in PLSQL code and get script content for page "js/page6100.js" component:

DECLARE
    l_files apex_t_export_files;

BEGIN
    --get files
    l_files := APEX_EXPORT.get_application(
        p_application_id => 400,
        p_split => true,
        p_with_translations => true,
        p_with_comments => true
    );

    FOR t IN 1 .. l_files.count LOOP
        --dbms_output.put_line(l_files(t).name);
        
        if l_files(t).name = 'f400/application/shared_components/files/js_page6100_js.sql' then
            dbms_output.put_line(l_files(t).contents);
        end if; 
    END LOOP;
    
END;

Result:

In this example I used DBMS_OUTPUT package to get script files but You can also insert them in some table / CLOB column.

Generated script contains only procedure call to crate a file (wwv_flow_api.create_app_static_file). So, if You need to remove existing file first then You should modify this script manually and add wwv_flow_api.remove_app_static_file procedure call.