Search code examples
javaantant-contrib

using ant manifest path entry building from path of the dependency jars


My problem is , i have to build the manifest entry classpath from the path like

  • C:\Users\rosansamuel.ivy2\cache\log4j\log4j\jars\log4j-1.2.14.jar

Can you please any one suggest some ideas?

Below code is , i am trying with propertyregex, but actually i can't able to get the jar names.

<for list="${ofsml.manifest.classpath.list}" delimiter=";" param="individual.path">
  <sequential>
    <property name="single.artifact.path" value="@{individual.path}"/>
    <echo message="single aritfact path name : ${single.artifact.path}"/>
    <path id="my.base.path">
      <pathelement path="${single.artifact.path}"/>
    </path>
    <property name="artifact.id.file" refid="my.base.path"/>
    <echo message=" artifact.id.file: ${artifact.id.file}"/>
    <propertyregex property="artifact.id" input="${artifact.id.file}" regexp=".*.jar" select="\1"/>
    <echo message="jar name : ${artifact.id}"/>
    <echo message="individual.path = @{individual.path}"/>
  </sequential>
</for>

Solution

  • There is a manifestclasspath task in ANT that can generate a list of relative filepaths from a fileset.

    I also see that you're using ivy, so why not use its retrieve task to place your dependency jars in a local directory relative to the jar you are building. Otherwise your jar will be hard-coded to expect its dependencies to be an absolute path that works on your machine, but not very portable.

    Here's an small snippet:

      <target name="build" depends="compile">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact].[ext]"/>
    
        <manifestclasspath property="jar.classpath" jarfile="${dist.jar}">
          <classpath>
            <fileset dir="${dist.dir}/lib" includes="*.jar"/>
          </classpath>
        </manifestclasspath>
    
        <jar destfile="${dist.jar}" basedir="${build.dir}/classes">
          <manifest>
            <attribute name="Main-Class" value="${dist.main.class}"/>
            <attribute name="Class-Path" value="${jar.classpath}"/>
          </manifest>
        </jar>
      </target>
    

    For a more complete example, see: