How to get hold on the flattenmapper result in ant

2020-04-10 00:42发布

问题:

Please, find below a few targets from my ant file:

<fileset id="test-dep-jars" dir="o:/java">
    <include name="junit-4.10.jar"/>
    <include name="easymock-3.1\easymock-3.1.jar"/>
    <include name="easymockclassextension-3.1\easymockclassextension-3.1.jar"/>
</fileset>

<target name="copy-test-deps">
    <mkdir dir="${deploy.dir}"/>
    <copy todir="${deploy.dir}">
        <fileset refid="test-dep-jars"/>
        <flattenmapper/>
    </copy>
</target>

<target name="jar" depends="copy-test-deps">
    <jar destfile="${deploy.dir}/test-${ant.project.name}.jar" basedir="${test.classes.dir}" 
         includes="**/*.class" filesetmanifest="skip">
        <manifest>
            <attribute name="Class-Path"
                       value="${ant.project.name}.jar junit-4.10.jar easymock-3.1.jar easymockclassextension-3.1.jar"/>
        </manifest>
    </jar>
</target>

My problem is that I have to state the test dependency jars twice - once when defining the test-dep-jars fileset and the second time when specifying the Class-Path manifest attribute of the produced jar.

If I only could get hold on the flattenmapper result, then I would be able to use it in the Class-Path as is.

How can I get hold on the flattenmapper result?

Thanks.

回答1:

I would recommend using the manifestclasspath task instead:

<manifestclasspath property="jar.classpath" jarfile="${jar.file}">
    <classpath>
        <fileset dir="${deploy.dir}" includes="*.jar"/>
    </classpath>
</manifestclasspath>

<jar destfile="${jar.file}" basedir="${classes.dir}">
    <manifest>
        <attribute name="Main-Class" value="${jar.main.class}" />
        <attribute name="Class-Path" value="${jar.classpath}" />
    </manifest>
</jar>

It will generate the correct classpath property definition and even works with relative paths (for example if you were to place the dependent jars in sub-directory).



回答2:

If you want to use flattenmapper you can use following...

<pathconvert property="mf.classpath" pathsep=" ">
    <path refid="build.class.path" />
    <flattenmapper />
</pathconvert>

<manifest>
            <attribute name="Class-Path"
                       value="${ant.project.name}.jar ${mf.classpath}"/>
        </manifest>


标签: ant