4

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.

mark
  • 59,016
  • 79
  • 296
  • 580

2 Answers2

4

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>
Vishal
  • 3,189
  • 1
  • 15
  • 18
3

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).

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I find `manifestclasspath` task inconvenient, because it stores relative paths to classpath jars. So in my case pathconvert and flattenmapper ([Vishal's answer](http://stackoverflow.com/a/13518698/772981)) work best. – Jarekczek Mar 26 '13 at 11:49
  • 1
    @Jarekczek Understood. However I normally ship my executable jars referencing dependencies delivered in a "lib" sub-directory. In this scenario manifestclasspath's ability to generate relative paths is actually quite useful. If you check my example I deliberately use a fileset referencing jars in my deployment directory a location that would be in the distribution location of my project, not the source directories. – Mark O'Connor Mar 26 '13 at 20:52