Building a Library with ANT

This post is in continuation from my last post: Using ANT to build an Application. So hopefully you read that last post or already have a working knowledge of ANT as we start looking at how to create a Library (swc file) using ANT.

The library that I have created is very very very simple, only containing one file to build: SuperLabel.as.

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
package com.unitedmindset.components
{
    import mx.controls.Label;
   
    /**
     * Label that adds 'Super' to the beginning of each string.
     * @author Administrator
     *
     */

    public class SuperLabel extends Label
    {
        public function SuperLabel()
        {
            super();
        }
       
        /**
         * Sets the value and appends "Super".
         * @param value
         *
         */

        override public function set text(value:String):void
        {
            super.text = "Super "+value;
        }
   
    }
}

Here you can also see the Flex Navigator explaining what is in my library.

Flex Explorer

Flex Explorer

Now we will go further and build on previous concepts to create a SWC via ANT.

The Starting Point
First let’s look at the shell that we will need.

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
<?xml version="1.0" encoding="UTF-8"?>
<project name="Library Build File" basedir="." default="buildLibrary">
    <!-- 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}" />

    <target name="buildLibrary" depends="compileLibrary" />
   
    <target name="compileLibrary" depends="cleanDeploy,compileSWC" />

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

    <target name="compileSWC" description="compiles the Library">
        <echo>Compiling Library SWC To Deploy SWC Folder</echo>
        <echo>Compiled Library SWC To Deploy SWC Folder</echo>
    </target>
   
</project>

You will see that we set up the project, clean the directory we will be writing to, and then compile the library. We also include the flexTasks.jar so that we can use the compc function and a link to our build.properties for our variables.

The build.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#######################################
#
#  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
#source directory
Src.dir=src
#debug flag
Debug.Boolean=false
#the location of your application classes on your computer
src.dir=${basedir}/src
#the location you wish to output bin on your computer
bin.dir=${basedir}/release
#location of the library file
library.dir=libs
#library name
Library.name=ANTBuildLibrary

The Library Build Target
When building a SWC, the compiler expects a list of classes to include. If you are curious what that looks like, check out .flexLibProperties.

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<flexLibProperties version="1">
  <includeClasses>
    <classEntry path="com.unitedmindset.components.SuperLabel"/>
  </includeClasses>
  <includeResources/>
  <namespaceManifests/>
</flexLibProperties>

For our little Library this isn’t a big deal, but for a larger library this could be daunting.

When creating your swc file you have one of three options, write out a list of specific classes to include in your manifest (often titled manifest.xml), use a little ANT script to go through your library and automatically collect each class and supply the compc compiler with each and every class automatically, or just use the provided code and have all your classes be include automatically. Your choice, I prefer the one with least amount of typing, number 3.

The script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    <target name="compileSWC" description="compiles the Library">
        <echo>Compiling Library SWC To Deploy SWC Folder</echo>

        <compc debug="${Debug.Boolean}" output="${bin.dir}/${Library.name}.swc">
                        <source-path path-element="${src.dir}" />
            <include-sources dir="${src.dir}" includes="*"/>
            <source-path path-element="${src.dir}" />
            <compiler.library-path dir="${basedir}/" append="true">
                <include name="${library.dir}" />
            </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>

With our nice list of classes we set the output directory of the swc, set the source path, library path, and some metadata. That’s it! Even embedded images and other sources will automatically be included in your build.

You’re done, you can now build your swc file from an ANT script.

  • Share/Bookmark

Comments (2)

[...] This post was mentioned on Twitter by HowDo.us, Richard Laksana. Richard Laksana said: Building a Library with ANT – http://su.pr/7qyxHK [...]

[...] 1) 14 Jan 10 – An example to demonstrate how to build a Library with ANT. [...]

Leave a comment

Your comment