I'm making a JAR file using Ant, but I need to specify the classpath loading order for the JAR file generated in the Manifiest file.
The reason for this is because some of the dependencies I have use different version of another library, and I need to make sure that the latest one is loaded. This is easy using Eclipse interface, but I'm not sure how to do this with Ant.
This is what I have now, for the JAR task:
<target depends="build-project" name="jar">
<!-- Get the timestamp -->
<set.timestamp/>
<!-- Create our own manifest file for our JAR -->
<manifest file="MANIFEST.MF">
<attribute name="Built-By" value="${username} - ${company.name}"/>
<attribute name="Main-Class" value="some.package.Main"/>
<attribute name="Class-path" value=". ${lib.list}"/>
<attribute name="Implementation-Version"
value="${major.version.number}.${minor.version.number}.${revision.number}"/>
<attribute name="Built-Date" value="${current.time}"/>
</manifest>
<jar whenmanifestonly="fail" destfile="${dist.dir}/${jar.name}_${major.version.number}.${minor.version.number}.${revision.number}.jar" manifest="MANIFEST.MF">
</target>
Now this is the Classpath property:
<manifestclasspath property="lib.list" jarfile="${dist.dir}/${jar.name}_${major.version.number}.${minor.version.number}.${revision.number}.jar">
<classpath refid="manifest.classpath" />
</manifestclasspath>
And this is the manifest.classpath:
<!-- Manifest classpaht -->
<path id="manifest.classpath">
<fileset dir="${dist.dir}/lib">
<include name="*.jar"/>
</fileset>
</path>
To me it looks like I?l have to define each library manually one by one above, instead of using a wildcard...but will that order be matained afterwards?
Thanks!
Alejandro
When you define the classpath, you can duplicate jars in that path, much like you can duplicate directories in $PATH. It's sloppy, but it's not bad:
PATH="/bin:/usr/bin:$HOME/bin:/usr/local/bin:/bin"
The /bin directory is specified twice, but that doesn't matter. If I specify ls, The first instance of /bin in my $PATH will find it. The last instance of /bin in my $PATH doesn't do anything. No real harm done with the duplication.
If you need to specify your classpath that certain jars must be first, then specify what you need first. If $PATH contains that particular jar name again, it doesn't matter:
<path id="manifest.classpath">
<pathelement location="${first.jar}"/>
<pathelement location="${second.jar}"/>
<fileset dir="${manifest.jars.dir}"/>
</path>
Even if ${first.jar} and ${second.jar} are in that ${manifest.jars.dir}, it doesn't matter. They'll still be picked up first in your path, and ignored later in your path.
Now, you can do this:
<manifestclasspath property="manifest.path">
<pathelement location="."/>
<classpath refid="manifest.classpath"/>
</manifestclasspath>
And finally:
<jar destdir="....">
<manifest>
<attribute name="Class-path" value="${manifest.path}"/>
...
</manifest>
</jar>
As far as I know, the the classpath in the manifest will be defined with the entries ordered in the same order you provided them. I know it works like this in Maven, and pretty sure the same is in Ant, so what you can do is provided the "critical" entries first - and then put the wildcard, hence providing the order needed - while not manually defining all the required dependencies.
Related
I'm attempting to add a splash-screen to a large Java project. The application is compiled into an executable .jar file using ANT.
I am able to get the splash screen working easily from NetBeans by simply adding -splash:src/com/.../.../image.PNG to my main project's VM options. However, adding SplashScreen-Image: com/.../.../image.PNG to my manifest file fails with "SplashScreen.getSplashScreen() returned null"
I have already opened up my .jar archive to confirm that things were set up correctly: my META-INF\MANIFEST.MF file includes the SplashScreen-Image line. I have tried moving it before or after Main-Class. My actual image.PNG is also in the archive, in the correct path location.
I compile this java project with ANT, which I can guess is the source of my problems (I was able to make a simple, "jar cmf ..." example work just fine).
I use the following to get the project elements ready for achiving:
<target name="compile" depends="init"
description="Compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<path id="lib.path.ref">
<fileset dir="${path_to_jre}" includes="*.jar"/>
</path>
<javac srcdir="${src}" destdir="${build}" source="${java_ver}" target="${java_ver}"
includeantruntime="false">
<!--compilerarg value="-Xbootclasspath/p:${toString:lib.path.ref}" compiler="javac1.7"/-->
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<fileset dir="${LibDir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${dependant_jar}"/>
<pathelement path="${another_dependant_jar}"/>
</classpath>
</javac>
<!-- Copy the .png files -->
<copy todir="${build}">
<fileset dir="${src}" casesensitive="false">
<include name="**/*.PNG"/>
</fileset>
</copy>
</target>
Notice that I use a copy to move .PNG files in with .class files. My image.PNG for the Splash Screen is just in with the others. I added it to my netbeans project by simply copying it there - nothing fancy.
My achieve target is as follows:
<target name="archive" depends="compile"
description="Generate the .jar file">
<!-- Put everything in ${build} into the .jar file -->
<jar jarfile="${jarfile}" basedir="${build}">
<!-- Merge in contents of dependency .jars -->
<zipfileset src="${LibDir}/snakeyaml.jar"/>
<zipfileset src="${dependant_jar}"/>
<zipfileset src="${another_dependant_jar}"/>
<!-- Specify main class in manifest -->
<manifest>
<attribute name="SplashScreen-Image" value="com/../../image.PNG"/>
<attribute name="Main-Class" value="${mainclass}"/>
</manifest>
</jar>
</target>
So my manifest in the eventual .jar is created here, which is also where I eventually realized to add the splashscreen tag.
I am somewhat new to working with ANT, so any advice regarding how to handle this or what to look for is appreciated.
It is not an issue with Ant. The problem is that the -splash option takes a file name, but the SplashScreen-Image manifest attribute must refer to a jar entry. If com/example/brian/image.PNG isn’t in the .jar file, it won’t be usable by SplashScreen-Image. The purpose of a .jar file is to be act as a self-contained module or application, so it should include all of the resources it needs.
The solution is to include the image in your .jar file:
<jar jarfile="${jarfile}" basedir="${build}">
<fileset dir="${src}" includes="**/*.PNG"/>
Update: Your build file was already adding the images to the .jar with a <copy> task, and I failed to notice it.
I have a solution, although I'm sure someone else will understand this better than I do.
I was able to run, with a splash-screen, using the Java binary in ../jre/lib/Java.exe, however I had initially been working from ../lib/Java.exe.
Looks like this is a known bug? Link here: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=7129420. I guess the path to the SplashScreen dll is hardcoded.
Note: Thanks to user VGR for the exhaustive help.
I am using zipgroupfileset to bundle all jars from lib folder to include in my executable application jar file.
<zipgroupfileset dir="${lib.dir}" />
Jars must be getting included in some default order. I want to alter this default order.
A note on why I want this: my Java desktop application uses many third party jars which are included in class path. When I run my code through Eclipse it works fine. But when I build the jar file using ANT it doesn't work as expected. I am sure it is related to jar sequence in classpath as if I change jar order in Eclipse it fails there as well.
Note: I am using Eclipse Kepler, Java 7, Ant 1.8.
Finally I got the solution. I build the Jar after altering my ANT file. Now my ANT build includes 3rd party APIs explicitly in classpath and that was the key to the solution.
<property name="lib.dir" value="lib" />
<manifestclasspath property="jar.classpath" jarfile="${lib.dir}/*.jar">
<classpath refid="project.class.path"/>
</manifestclasspath>
<jar destfile="${jar.dir}/${jar.name}">
<fileset dir="${class.root}" includes="**/*.*" />
<manifest>
<attribute name="Main-Class" value="${Main-Class}" />
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
Previously I had class-path entry in ANT as follows:
<attribute name="Class-Path" value="." />
I need to include some third party jar file to my project jar. I mentioned it in my build.xml and include this to MANIFEST.MF. Now i get thirdparty1.jar thirdparty2.jar file into inside the project jar. But still i can't able to use the jars. Is it need any addition configuration
Here is my build.xml
<manifest>
<attribute name="Class-Path" value="thirdparty1.jar thirdparty2.jar thirdparty3.jar"/>
If i copy the two jar separately it works well. But i don't understand what is the need for copy these separate. How it solve with out copying jar separately.
If the dependency jar is packaged inside the project jar, you need a solution to load it from there. The standard class-path handling in Java won't access jar files located inside other jar files.
See this answer: Classpath including JAR within a JAR. Specifically the One Jar solution: http://one-jar.sourceforge.net/.
It's also possible to use zipgroupfileset for that.given is the sample ant task for that.
<!-- Build JAR file -->
<target name="jar" depends="init-build-dir,compile-main">
<!--creating a temp jar contains all jar -->
<jar jarfile="${project.build.lib.dir}/external-libs.jar">
<zipgroupfileset dir="${project.lib.redist.dir}">
<include name="**/*.jar" />
</zipgroupfileset>
</jar>
<sleep seconds="1" />
<!-- creating main jar with temp jar-->
<jar jarfile="${project.build.lib.dir}/${ant.project.name}.jar" manifest="MANIFEST.MF">
<fileset dir="${project.build.main.classes.dir}" includes="**/*.*" />
<zipfileset src="${project.build.lib.dir}/external-libs.jar">
<exclude name="*" />
</zipfileset>
</jar>
<!--removing temp jar -->
<delete>
<fileset dir="${project.build.lib.dir}">
<include name="external-libs.jar" />
</fileset>
</delete>
</target>
I have a package(with some outdated Java files) which i need to build using ant tool. I have some updated classes for which source is not available. So, I need to add the updated class to the jar during build and skip the outdated Java files from the build. Please let me know how to achieve this.
For example - I dont have the updated source java file for com.org.auth.ABC.Java, but i have the updated class file which i got from the another source ie com.org.auth.ABC.class. During build i need to point to the updated class(com.org.auth.ABC.class) for this class alone and create a new jar.
Please find how i am currently pointing to the class files below.
<target name="xxx.jar" depends="xjc,compile">
<jar destfile="${dist.dir}/xxx.jar">
<fileset dir="${classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-source.dir}" includes="**/bgm.ser,**/jaxb.properties"/>
</jar>
</target>
If you want to leave some package from compilation you can use excludes attribute of fileset ant tag.
for example:
<fileset dir="src/">
<exclude name="**/dir_name_to_exclude/**" />
</fileset>
In order to include the specified class in compilation you can put the containing folder in your class-path using ant.
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="path to your class file's base folder"/>
</path>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="project.class.path">
... put source files here..
</javac>
And if you want to include that class-file in your jar then add it to fileset using include tag:
<fileset dir="classfiles">
<include name="your class file name"/>
</fileset>
Hope this helps
I have craated a Java application with an Ant build file containing the jar-task task that generate a jar file from the application.
<target name="jar-task" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="jar/guix.jar" basedir="${bin.dir}">
<fileset dir="${basedir}">
<include name="${basedir}/images/**/" />
</fileset>
<manifest>
<attribute name="Main-Class" value="IO.Deep.clk.GUI"/>
<attribute name="Class-Path" value="${basedir}/SPLASH-2.0.0.jar ${basedir}/lib/dist/* ${basedir}/user.properties"/>
</manifest>
<filelist dir="${basedir}" files="user.properties"/>
</jar>
</target>
When I execute on the command line however a NoClassDefFoundError stating
Could not find the main class IO.Deep.clk.GUI. Program will exit.
Now, the GUI file is exactly in the specific folder and in the same package, I really can't understand where the error may be...can anyone help?
The name images suggests, that the jar file will contain only images. But where is the actual code?
<fileset dir="${basedir}">
<include name="${basedir}/images/**/" />
</fileset>
This piece
<attribute name="Class-Path" value="${basedir}/SPLASH-2.0.0.jar ${basedir}/lib/dist/* ${basedir}/user.properties"/>
will write full qualified pathnames into the manifest. Are sure, that this is correct? Also note, that the code will not find user.properties by putting the file on the classpath. The classpath can contain only directories or jar-files in which classes or other stuff will be searched for. But simple files won't work.
I'm also not sure about the lib/* part. Is the class IO.Deep.clk.GUI in one of the jar files in that directory? That would be a hint, that all directories must be listed explicitly. If that's not a problem - good.
EDIT:
The classpath problems can be avoided by adding the task manifestclasspath (Ant docs) before the call of jar and by using the generated classpath value inside the manifest attribute. Outline:
<manifestclasspath property="jar.class.path" jarfile="jar/guix.jar">
<classpath>
<fileset dir="." includes="*.jar" />
<fileset dir="." includes="lib/*.jar" />
</classpath>
</manifestclasspath>
<echo message="Class-Path will be: ${jar.class.path}" />
<jar ....>
....
<attribute name="Class-Path" value="${jar.class.path}" />
Are you sure your resulting filest inside the jar task contains all the files you need?
For debugging you can use a path convert to the fileset out of the jar task and echo it so that you can verfiy that you have the correct files. If this is OK then I don't see something else wrong in your file although I have limited experience with the jar task myself.