I am a bit of an ant newbie, and I'm having trouble making a jar correctly. As an example, say I want to make a jar with my StringUtil class. Using the following ant directive, I can create the jar, but the problem is that the directory structure is lost. It simply puts StringUtil.class in the base directory of the jar. How can I correct this ant directive so that StringUtil.class is inside the com/test directory in the jar?
<jar destfile="myjar.jar" >
<fileset file="${build}/com/test/StringUtil.class"/>
</jar>
Thanks!
You need to tell Ant to build the jar from the base directory, then tell it to include only the desired file. Like so:
<jar destfile="myjar.jar" >
<fileset dir="${build}" includes="com/test/StringUtil.class"/>
</jar>
Here's the doc for <fileset> tags.
Related
I'm using ANT to build a Java project that has dependency jars during compile time. The dependency jars are nested inside a directory. What this means is, the directory contains sub-directories (which might contain sub-directories) and jars as well.
I want to create an ANT element to include all the jars inside the directory. Below is my code but doesn't work. I'm wondering if this can't be done in ANT or I'm doing something wrong:
<path id="javaee.classpath">
<fileset dir="${javaee.modules.dir}">
<patternset>
<include name="**/*.jar" />
</patternset>
</fileset>
</path>
I have a folder structures
build/classes/com/vaannila/domain/.class files
build/classes/com/vaannila/service/.class files
build/classes/com/vaannila/web/.class files
My requirement is to create a jar file using ant, how can I include the specified folders in the jar,say I want include only domain and service (or) domain and web in the jar, like the way I create the jar using Eclipse(Export-->archive file).
Can anyone help me with this please?
You can use include and exclude tags in the fileset element, and choose specific files...
<jar destfile="${jar.dir}/${jar.file.name}" index="true"
manifest="${manif.dir}/${manif.file.name}">
<fileset dir="${build.classes.dir}" >
<include/>
<exclude/>
</fileset>
</jar>
<fileset dir="${build.classes.dir}">
<exclude name="**/property/*"/>
<include name="**/abcd/*"/>
</fileset>
This way you can remove/include respective directories.
I have a java project (MyProject) with the below mentioned structure
src->package1(read as com.test.Atrribute)->File1.java,File2.java
src->package2(read as com.test.Objects)->obj1.java,obj2.java
src->directory(read as Webcontent.Objects)-> Folder1 -> application.properties file and some more files
Currently the build.xml creates a jar for the above project and copies the class files from package1 and package2.
However, my jar should also include the folder(Webcontent.Objects) with all the content's within it (i.e folders and files).
How can I do this in the build.xml ?
I have never created a build.xml before and pretty much new to all this.
Following is the jar task in the build.xml to include the class file's in the jar.
<target name="MyProject-jar" depends="compile"
description="Jar for the Project">
<jar destfile="${output.dir}/MyProject.jar" basedir="${output.dir}/">
<include name="com/test/Attribute/*.class"/>
<include name="com/test/Objects/*.class"/>
</jar>
</target>
Appreciate if anybody could help.Thanks.
You could add
<include name="Webcontent/Objects/**/*"/>
to your jar task
Note: The ** recursively considers directories under its parent
I am looking forward for an example which unjars a file referenced from the classpath inside the ANT. I couldn't find any example in the web. When I tried using the path inside the unjar target, it unjars all the jar files inside the classpath. Can anyone suggest me something giving some example ?
You have to specify the jar name which you what to unjar.
Probably you have wildcard(*) in your src
This will unjar all jars in ${ant.home}/lib/
<unzip src="${ant.home}/lib/*.jar" dest="..."/>
I think you should use:
<unzip src="${ant.home}/lib/ant.jar" dest="..."/>
If you want to unjar some files from your jar look at this example:
<unzip src="${ant.home}/lib/ant.jar" dest="...">
<patternset>
<include name="**/ant_logo_large.gif"/>
<include name="**/LICENSE.txt"/>
</patternset>
</unzip>
Look at unzip task documentation
Is there a way I can modify a file in a jar using ant script. Like, I have a x.properties in a y.jar. I want to edit this x.properties and put it back into the y.jar using ant script. Is this possible?
To extract a file from a jar:
<unjar src="y.jar" dest="build">
<patternset>
<include name="x.properties"/>
</patternset>
</unjar>
To add it back in:
<jar jarfile="y.jar" update="true">
<fileset dir="." includes="x.properties"/>
</jar>
jar -xvf <filename> extracts a file out of a jar.
jar -uvf <filename> can update a file in the jar.
You can use a script to do this and then use Ant to call the script.
I usually unpack the whole jar file, alter the file I want to alter, and then create a new jar file. I is probably not the fasted way, but my build scrips are not time critical.