开发者

Ant: compile classes depending on property

I'm trying to get Ant to compile only parts of my project depending on a property (module) I set in the properties file. This is my first "real" interaction with ant so please bear with me :).

I 开发者_如何学JAVAdon't want to create multiple targets because the only thing that differs between modules in the build process is the number of classes that are being compiled and some resources.

Now, here's my compile target:

<target name="compile.classes" depends="init" description="compile the source">
    <mkdir dir="${classes}"/>
    <mkdir dir="${lib}"/>

    <copy todir="${classes}">
        <fileset dir="${src}">
            <patternset refid="resources"/>
        </fileset>
    </copy>

    <echo message="${classes}"/>

    <!-- Compile the java code from ${src} into ${classes} -->
    <javac srcdir="${src}" destdir="${classes}">
        <!-- Conditions for compiling the separate modules -->
        <classpath refid="libs"/>
    </javac>
</target>

What I want to have are some conditionals based on the value of the module property I've already set. Something like:

<javac srcdir="${src}" destdir="${classes}">
    <if>
        <equals arg1="${module}" arg2="gpl" />
        <then>
            <patternset refid="gpl-classes"/>
        </then>
        <elseif /> <!-- etc -->
    </if>
</javac>

Although I've installed ant-contrib fine (I've tested the if tag somewhere else with a simple echo and it worked) it doesn't let me place if conditionals inside javac tags. Also, it doesn't let me place them inside patternset tags.

Is there any way I can condition what classes I compile depending on a property?


If I understand correctly, the file sets can be catered for by means of reference ids.

Essentially you

  • define a patternset with id for each of the sets of sources associated with modules
  • select the patternset to use by means of a property value in a patternset with refid

Here's an illustration:

<patternset id="pattern1">
    <include name="*1.txt" />
</patternset>

<patternset id="pattern2">
    <include name="*2.txt" />
</patternset>

<property name="pattern_choice" value="pattern2" />

<mkdir dir="dest" /> 
<delete dir="dest" /> 

<copy todir="dest">
    <fileset dir=".">
        <patternset refid="${pattern_choice}" />
    </fileset>
</copy>

By changing the value of property pattern_choice the set of files copied is changed. You can set this in your properties file.


If you want conditional logic functionality, you may use antcontrib library, specifically its if task.

Then you can do something like this:

<if>
  <equals arg1="${module}" arg2="gpl" />
  <then>
    <path id="javac.classpath">
      <path refid="libs"/>
      <path refid="gpl-classes"/>
    </path>
  </then>
  <else>
    <path id="javac.classpath" refid="libs"/>
  </else>
</if>

<javac srcdir="${src}" destdir="${classes}">
  <classpath refid="java.classpath"/>
</javac>


It looks like you want to use MacroDef

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜