Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate manifest class-path from <classpath> in Ant

In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path

There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.

Edit:
It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.

<?xml version="1.0"?> <project name="Higgins" default="jar" basedir=".">      <property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>      <path id="project.class.path">       <pathelement location="build"/>       <fileset dir="lib">         <include name="forms-1.2.0.jar"/>         <include name="BrowserLauncher.jar"/>       </fileset>     </path>      <target name="prepare">         <mkdir dir="build"/>     </target>      <target name="compile" depends="prepare" description="Compile core sources">         <javac srcdir="src"                includes="**"                destdir="build"                debug="true"                source="1.5">           <classpath refid="project.class.path"/>         </javac>     </target>      <target name="jar" depends="compile" description="Generates executable jar file">         <jar jarfile="higgins.jar">             <manifest>                 <attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>                 <attribute name="Class-Path" value="${jar.class.path}"/>             </manifest>             <fileset dir="build" includes="**/*.class"/>                         <fileset dir="src" includes="**/*.properties"/>                  </jar>     </target>  </project> 
like image 876
amarillion Avatar asked May 13 '09 15:05

amarillion


People also ask

What is manifest in ant?

Manifests are processed according to the Jar file specification. Specifically, a manifest element consists of a set of attributes and sections. These sections in turn may contain attributes. Note in particular that this may result in manifest lines greater than 72 bytes being wrapped and continued on the next line.

What is manifest MF Java?

A file with . mf extension is a Java Manifest file that contains information about the individual JAR file entries. The MF file itself is contained inside the JAR file and provides all the extension and package-related definition. JAR files can be produced to be used as an executable file.


2 Answers

<path id="build.classpath">   <fileset dir="${basedir}">      <include name="lib/*.jar"/>   </fileset> </path>  <pathconvert property="manifest.classpath" pathsep=" ">   <path refid="build.classpath"/>   <mapper>     <chainedmapper>        <flattenmapper/>        <globmapper from="*.jar" to="lib/*.jar"/>     </chainedmapper>   </mapper> </pathconvert>  <target depends="compile" name="buildjar">   <jar jarfile="${basedir}/${test.jar}">      <fileset dir="${build}" />      <manifest>        <attribute name="Main-Class" value="com.mycompany.TestMain"/>        <attribute name="Class-Path" value="${manifest.classpath}"/>      </manifest>  </jar> </target> 

For further information check out this article.

like image 96
Qianjigui Avatar answered Oct 07 '22 14:10

Qianjigui


Assuming Ant 1.7 or above, you can use the manifestclasspath task.

<path id="dep.runtime">     <fileset dir="./lib">         <include name="**/*.jar" />     </fileset> </path> <property name="dep_cp" value="${toString:dep.runtime}" />  <target name="default">     <manifestclasspath property="manifest_cp" jarfile="myjar.jar">         <classpath refid="dep.runtime" />     </manifestclasspath>     <echo message="Build Classpath: ${dep_cp}" />     <echo message="Manifest Classpath: ${manifest_cp}" /> </target> 
like image 20
McDowell Avatar answered Oct 07 '22 16:10

McDowell