Conditionally including Flex libraries (SWCs) in m

2019-02-27 10:54发布

问题:

I have been struggling trying to figure out how to conditionally include Flex libraries in an ant build based on a property that is set on the command line. I have tried a number of approaches with the <condition/> task, but so far have not gotten it to work. Here is where I am currently.

I have an init target that includes condition tasks like this:

 <condition property="automation.libs" value="automation.qtp">
  <equals arg1="${automation}" arg2="qtp" casesensitive="false" trim="true"/>
 </condition>

The purpose of this task is to set a property that determines the name of the patternset to be used when declaring the implicit fileset on a mxmlc or compc task. The pattern set referenced above is defined as:

 <patternset id="automation.qtp">
  <include name="automation*.swc"/>
  <include name="qtp.swc"/>
 </patternset>

The named patternset is then referenced by the mxmlc or compc task like this:

<compc>
 <compiler.include-libraries dir="${FLEX_HOME}/frameworks/libs" append="true">
  <patternset refid="${automation.libs}"/>
 </compiler.include-libraries>
</compc>

This doesn't appear to work. At least the SWC size does not indicate that the additional automation libraries have been compiled in. I want to be able to specify a command line property that determine which patternset to use for various types of builds.

Does anyone have any ideas about how to accomplish this? Thanks!

回答1:

If you can't get <patternset> to work correctly, you might want to take a look at the <if> <then> and <else> tasks provided by ant-contrib. We ended up doing something like this:

<target name = "build">
    <if>
        <equals arg1="automation.qtp" arg2="true"/>
        <then>
            <!--
               - Build with QTP support.
               -->
        </then>
        <else>
            <!--
               - Build without QTP support.
               -->
        </else>
    </if>
</target>

There is some duplication of build logic between the if and else branch, but you can factor some of that out if you wrap <mxmlc> with a macrodef.



回答2:

The mxmlc task supports loading configuration files <load-config filename="path/to/flex-config.xml" />. So, generate the config xml on the fly, by combining the echoxml task and if-then-else.

<echoxml file="path/to/flex-config.xml">
    <flex-config>
        <compiler>
            <library-path append="true">
                <path-element>${lib.qtp}</path-element>
            </library-path>
        </compiler>
    </flex-config>
</echoxml>

If your needs are more complicated, you could even generate several xml configs and <load-config ... /> them all.

Personally, I find any logic very terse and ugly to write using Ant's conditions or if-then-else, XML is not a pretty language to use for programming. Luckily, it's possible to use more flexible approach - write a script to produce the config xml, before calling mxmlc. E.g. use the script task with your favorite scripting language

<script language="javascript">
    <![CDATA[
        // Create your XML dynamically here.
        // Write that XML to an external file.
        // Later, feed that file to mxmlc using `<load-config ... />`.
    ]]>
</script>