Building a Library and Application with ANT

Now we have two ANT build files, one for the project and one for the library. What if we want the build file to build the library, store the swc, and then build the application with the newly created swc. These are the next steps we need to take to ensure a brand new clean build each time we make our application.

This is the step that things get a bit hairy – at least more so then it was in the past. Our current steps look like:

Now we need to go a bit further and do some better cleanup then we were doing previously. In this step we will also become much more tied to the computer that is running the build file. I say this because we will be reaching cross projects and no longer is everything going to be simple paths from the base directory. This isn’t an issue and can all be handled in the build.properties for every environment, but still just another hurdle. When we end this step our build process will look like this:

*Denotes new functionality

Make new temporary folder to store generated SWCs
Starting (following the earlier defined order) with creating a new temporary folder to hold the dependent SWC files that we will create in the ANT files. Later we will delete this file, but we need to hold all our SWC files into one location to simplify the build process.

1
2
3
4
5
6
7
8
    <target name="cleanDeploySWCs">
        <echo>Deleting Deploy SWC Directory...</echo>
        <delete dir="${DeploySWC.dir}" failOnError="false" includeEmptyDirs="true"/>
        <echo>Deleted Deploy SWC Directory</echo>
        <echo>Creating Deploy SWC Directory...</echo>
        <mkdir dir="${DeploySWC.dir}"/>
        <echo>Created Deploy SWC Directory</echo>
    </target>

For this we need to add a new ANT variable in the build.properties.

1
2
#Deploy SWC Directory
DeploySWC.dir=deploySWC

Copy SWCs that the application depends on into temp folder
Next let’s copy all of the files that the Flex application is expecting into our newly created temporary folder.

1
2
3
4
5
6
7
    <target name="copyPrecompiledSWCs">
        <echo>Copying to Deploy SWC Directory...</echo>
        <copy todir="${DeploySWC.dir}" includeemptydirs="false" overwrite="true">
            <fileset dir="${ApplicationLibs.dir}"/>
        </copy>
        <echo>Copied to Deploy SWC Directory...</echo>
    </target>

And the new build.properties additions.

1
2
#Application Libs
ApplicationLibs.dir=libs

Build Library, outputting into temp folder
In this step we will be making a modified version of the Library ANT build script that we used earlier, while also defining the location of the library. When we compile the library it’s output will result in the SWC file being placed in the temporary folder that we created earlier. This will be the final SWC necessary to build our application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    <target name="compileLibrary" description="compiles the Library">
        <echo>Compiling Library SWC To Deploy SWC Folder</echo>
        <compc debug="${Debug.Boolean}" output="${DeploySWC.dir}/${Library.name}.swc">
            <source-path path-element="${LibrarySrc.dir}"/>
            <include-sources dir="${LibrarySrc.dir}" includes="*"/>
            <compiler.library-path dir="${LibraryLibs.dir}" append="true">
                <include name="*.swc"/>
            </compiler.library-path>
            <metadata>
                <creator>Jonathan Campos</creator>
                <publisher>UnitedMindset</publisher>
                <language>EN</language>
            </metadata>
        </compc>
        <echo>Compiled Library SWC To Deploy SWC Folder</echo>
    </target>

Additional Properties

1
2
3
4
5
6
7
8
#Library Directory
Library.dir=C:/workspace/ANTBuildLibrary
#Library Libs
LibraryLibs.dir=${Library.dir}/libs
#Library Src
LibrarySrc.dir=${Library.dir}/src
#library name
Library.name=ANTBuildLibrary

Build the application
With all the SWCs compiled and put together we can now build the application. You will notice that there is one small change in the application target to include a library folder file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <target name="compileApplication">
        <echo>Compiling SWF To Deploy</echo>
        <mxmlc file="${Src.dir}/${Application.name}.mxml"
            incremental="false"
            actionscript-file-encoding="UTF-8"
            output="${Deploy.dir}/${Application.name}${timestamp}.swf"
            debug="${Debug.Boolean}"
            keep-generated-actionscript="false">
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
            <default-background-color>0xFFFFFF</default-background-color>
            <metadata>
                <creator>Jonathan Campos</creator>
                <publisher>UnitedMindset</publisher>
                <language>EN</language>
            </metadata>
            <compiler.source-path path-element="${Src.dir}" />
            <compiler.library-path dir="${basedir}/${DeploySWC.dir}" append="true">
                <include name="*.swc"/>
            </compiler.library-path>
        </mxmlc>
        <echo>Compiled SWF To Deploy</echo>
    </target>

Clean up (delete) temp folder
With our application created and safely sitting in our deploy folder, we just need to clean up (delete) the temporary folder that we created previously so that it isn’t loitering in our project.

1
2
3
4
5
    <target name="cleanTempDirectories">
        <echo>Deleting Deploy SWC Directory...</echo>
        <delete dir="${DeploySWC.dir}" failOnError="false" includeEmptyDirs="true"/>
        <echo>Deleted Deploy SWC Directory</echo>
    </target>

Your Current Build File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?xml version="1.0" encoding="UTF-8"?>
<project name="Build File" basedir="." default="compileProject">
   
    <!-- Set Up    ============================================== -->
   
    <!-- file description -->
    <description>Build Script</description>
   
    <!--location of property file -->
    <property file="./build.properties"
        description="properities for builds" />
   
    <!-- timestamp -->
    <tstamp>
        <format property="timestamp" pattern="yyyyMMdd" />
    </tstamp>
   
    <!-- additional tasks -->
    <taskdef resource="flexTasks.tasks" classpath="${FlexTasks.file}"/>
   
    <!-- ======================================================== -->
   
   
    <!-- Compile Project    ===================================== -->
    <target name="compileProject"
        depends="cleanDeploy,buildWrapper,buildCustomWrapper,copyNonEmbeddedFiles,compileLibraries,compileApplication,cleanTempDirectories"
        description="compiles application"/>
   
    <target name="cleanDeploy" description="Cleans the deploy file">
        <echo>Deleting Deploy Directory...</echo>
        <delete dir="${Deploy.dir}" failOnError="false"
            includeEmptyDirs="true" />
        <echo>Deleted Deploy Directory</echo>
        <echo>Creating Deploy Directory...</echo>
        <mkdir dir="${Deploy.dir}" />
        <echo>Created Deploy Directory</echo>
    </target>
   
    <target name="buildWrapper">
        <echo>Building Wrapper...</echo>
        <html-wrapper title="${Application.name}"
            height="100%" width="100%"
            bgcolor="#FFFFFF" file="${Application.name}.html"
            application="${Application.name}"
            swf="${Application.name}${timestamp}"
            version-major="${Major.version}"
            version-minor="${Minor.version}"
            version-revision="${Revision.version}"
            history="true"
            template="express-installation"
            output="${Deploy.dir}" />
        <echo>Built Wrapper</echo>
    </target>
   
    <target name="buildCustomWrapper">
        <echo>Building Custom Wrapper...</echo>
        <copy file="${Template.file}" tofile="${Deploy.dir}/${Output.file}" overwrite="true">
            <filterchain>
                <replacetokens>
                    <token key="version_major" value="${Major.version}"/>
                    <token key="version_minor" value="${Minor.version}"/>
                    <token key="version_revision" value="${Revision.version}"/>
                    <token key="application" value="${Application.name}"/>
                    <token key="width" value="100%"/>
                    <token key="height" value="100%"/>
                    <token key="bgcolor" value="#FFFFFF"/>
                    <token key="swf" value="${Application.name}${timestamp}"/>
                </replacetokens>
            </filterchain>
        </copy>
        <echo>Built Custom Wrapper</echo>
    </target>
   
   
    <target name="compileApplication">
        <echo>Compiling SWF To Deploy</echo>
        <mxmlc file="${Src.dir}/${Application.name}.mxml"
            incremental="false"
            actionscript-file-encoding="UTF-8"
            output="${Deploy.dir}/${Application.name}${timestamp}.swf"
            debug="${Debug.Boolean}"
            keep-generated-actionscript="false">
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
            <default-background-color>0xFFFFFF</default-background-color>
            <metadata>
                <creator>Jonathan Campos</creator>
                <publisher>UnitedMindset</publisher>
                <language>EN</language>
            </metadata>
            <compiler.source-path path-element="${Src.dir}" />
            <compiler.library-path dir="${basedir}/" append="true">
                <include name="${DeploySWC.dir}"/>
            </compiler.library-path>
        </mxmlc>
        <echo>Compiled SWF To Deploy</echo>
    </target>
    <!-- ======================================================== -->
   
    <!-- NONEMBEDDED FILES ====================================== -->
    <target name="copyNonEmbeddedFiles">
        <echo>Deleting Deploy Assets Directory...</echo>
        <delete dir="${DeployAssets.dir}" failOnError="false" includeEmptyDirs="true" />
        <echo>Deleted Deploy Assets Directory</echo>
        <echo>Creating Assets Folder</echo>
        <mkdir dir="${DeployAssets.dir}"/>
        <echo>Created Assets Folder</echo>
        <echo>Copy Nonembedded Resources To Deploy</echo>
        <copy todir="${DeployAssets.dir}" includeemptydirs="false" overwrite="true">
            <fileset dir="${Assets.dir}"/>
        </copy>
        <echo>Copied Nonembedded Resources To Deploy</echo>
    </target>
    <!-- ======================================================== -->
   
    <!-- COMPILE LIBRARIES ====================================== -->
    <target name="compileLibraries" depends="cleanDeploySWCs,copyPrecompiledSWCs,compileLibrary" />

    <target name="cleanDeploySWCs">
        <echo>Deleting Deploy SWC Directory...</echo>
        <delete dir="${DeploySWC.dir}" failOnError="false" includeEmptyDirs="true"/>
        <echo>Deleted Deploy SWC Directory</echo>
        <echo>Creating Deploy SWC Directory...</echo>
        <mkdir dir="${DeploySWC.dir}"/>
        <echo>Created Deploy SWC Directory</echo>
    </target>

    <target name="copyPrecompiledSWCs">
        <echo>Copying to Deploy SWC Directory...</echo>
        <copy todir="${DeploySWC.dir}" includeemptydirs="false" overwrite="true">
            <fileset dir="${ApplicationLibs.dir}"/>
        </copy>
        <echo>Copied to Deploy SWC Directory...</echo>
    </target>
   
    <target name="compileLibrary" description="compiles the Library">
        <echo>Compiling Library SWC To Deploy SWC Folder</echo>
        <compc debug="${Debug.Boolean}" output="${DeploySWC.dir}/${Library.name}.swc">
            <source-path path-element="${LibrarySrc.dir}"/>
            <include-sources dir="${LibrarySrc.dir}" includes="*"/>
            <compiler.library-path dir="${LibraryLibs.dir}/" append="true">
                <include name="*.swc"/>
            </compiler.library-path>
            <metadata>
                <creator>Jonathan Campos</creator>
                <publisher>UnitedMindset</publisher>
                <language>EN</language>
            </metadata>
        </compc>
        <echo>Compiled Library SWC To Deploy SWC Folder</echo>
    </target>
   
    <!-- Clean Up =============================================== -->
    <target name="cleanTempDirectories">
        <echo>Deleting Deploy SWC Directory...</echo>
        <delete dir="${DeploySWC.dir}" failOnError="false" includeEmptyDirs="true"/>
        <echo>Deleted Deploy SWC Directory</echo>
    </target>

    <!-- ======================================================== -->
   
</project>

Your Current build.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
###############################################################################
#
#  Compiler Properties
#
###############################################################################

# Flex SDK File Location
FLEX_HOME=C:/Program Files/Adobe/Flex Builder 3 Plug-in/sdks/3.5
#flextasks jar location
FlexTasks.file=flexTasks/flexTasks.jar
#deploy directory
Deploy.dir=deploy
#source directory
Src.dir=src
#debug flag
Debug.Boolean=false
#application name
Application.name=ANTBuildProject
#version major
Major.version=9
#version minor
Minor.version=0
#version revision
Revision.version=124
#Template File
Template.file=index.template.html
#Output HTML File
Output.file=index.html
#Assets
Assets.dir=${Src.dir}/assets
#Assets
DeployAssets.dir=${Deploy.dir}/assets
#Deploy SWC Directory
DeploySWC.dir=deploySWC
#Library Directory
Library.dir=C:/workspace/ANTBuildLibrary
#Library Libs
LibraryLibs.dir=${Library.dir}/libs
#Library Src
LibrarySrc.dir=${Library.dir}/src
#library name
Library.name=ANTBuildLibrary
#Application Libs
ApplicationLibs.dir=libs
  • Share/Bookmark

Comments (3)

[...] This post was mentioned on Twitter by Jonathan Campos, HowDo.us. HowDo.us said: Building a Library and Application with ANT: Now we have two ANT build files, one for the project and one for the … http://bit.ly/5RxLDl [...]

[...] script to run your Flex based life I feel like we’ve reached some midway point. We have our application being built off of a Flex Library, building the default Adobe html-wrapper along with our own custom wrapper while keeping the [...]

[...] ANT build file that will work right into your continuous integration script. Now you can make a release quality build, document it, and run both unit tests and automated tests against your [...]

Leave a comment

Your comment