Ant - build script does not find pathelement defin

2019-09-08 16:17发布

问题:

I have an ant build script that has the following targets:

<target name="_initLiveProps">
        <property file="buildscripts/live.properties"/>
</target>

<target name="buildLive"  depends="_initLiveProps">
        <property file="buildscripts/live.properties"/>
</target>

Within the build script i have several pathelements declared as shown below:

<path id="project.class.path">      
        <pathelement location="./../lib/log4j-1.2.16.jar" />
        <pathelement location="${product-def.jar}"/>
</path>

The product-def.jar definition is defined in the buildscripts/live.properties file as

product-def.jar=./../lib/product-def/live/product-def.jar

When i build the project (using ant buildLive) i get compilation errors and mainly because it cannot find classes defined within product-def.jar.

I tried to print out the classpath as shown below

<property name="myclasspath" refid="project.class.path"/>
<echo message="${myclasspath}" />

And the output comes out as c:\product\lib\log4j-1.2.16.jar;c:\product\${product-def.jar}

The above suggests that the following definition is not correct

<pathelement location="${product-def.jar}"/>

What is the correct way of defining a path element that is defined in a properties file?

Edit

I think the issue is that the definition for project.class.path is initialised before the properties file is loaded in the buildLive target. Is there a way to delay the initialisation of project.class.path until after buildLive target has completed?

回答1:

Is there a way to delay the initialisation of project.class.path until after buildLive target has completed?

Put the <path> definition inside the <target>

<target name="_initLiveProps">
        <property file="buildscripts/live.properties"/>
        <path id="project.class.path">      
                <pathelement location="./../lib/log4j-1.2.16.jar" />
                <pathelement location="${product-def.jar}"/>
        </path>
</target>

The <path> will be visible to all targets that depend (directly or indirectly) on this one.

If you have several different targets that load different properties, e.g. _initLiveProps, _initDevProps, etc. then you could put the <path> definition into a common target as follows

<target name="classpath">
        <path id="project.class.path">      
                <pathelement location="./../lib/log4j-1.2.16.jar" />
                <pathelement location="${product-def.jar}"/>
        </path>
</target>

<target name="_loadLiveProps">
        <property file="buildscripts/live.properties"/>
</target>
<target name="_initLiveProps" depends="_loadLiveProps, classpath" />

<target name="_loadDevProps">
        <property file="buildscripts/dev.properties"/>
</target>
<target name="_initDevProps" depends="_loadDevProps, classpath" />


标签: java ant build