Using ant 1.5
I am trying to create a jar file with files compiled under product directory that can constantly change the build name
/target
|---myapp-1.3.5-SNAPSHOT
|--- WEB-INF
|---classes
|---i18n
It's kind of picky but I have to make a jar file that still keep the web structure
|---myapp-utility.jar
|--- WEB-INF
|---classes
|---i18n
That means I have to put wild card to identify the directory with build and snapshot
<jar destfile="myapp-utility.jar">
<manifest>
....
</manifest>
<fileset dir="myapp-**/"/>
</jar>
and it doesn't work, wild card is not recognized. So what is the alternative?
You can use pathconvert to analyze the wildcard specification:
<pathconvert property="myapp.dir">
<dirset dir="target" includes="myapp-*"/>
</pathconvert>
<jar destfile="myapp-utility.jar">
<manifest>
<!-- .... -->
</manifest>
<fileset dir="${myapp.dir}"/>
</jar>
Ant 1.5 is a very old version of Ant. If you can use at least Ant 1.8.2 (released in 2010), you can use the following...
<jar destfile="myapp-utility.jar">
<mappedresources>
<fileset dir="target">
<include name="myapp-*/**"/>
</fileset>
<cutdirsmapper dirs="1"/>
</mappedresources>
</jar>
The <mappedresources> resource collection wraps the <fileset> element and then applies a mapper to the results.
It's true that the dir of <fileset> can't handle wildcards. Luckily, <include> can handle wildcards. So, the above puts the wildcard test into <include>. The ** ensures all files under myapp-* are matched.
Finally, <cutdirsmapper> removes the myapp-1.3.5-SNAPSHOT/ part from each file.
Related
I'm creating some runnable JARs (desktop JavaFX applications).
I have a fileset of the 3rd party JARs I'm including:
<fileset id="shared_lib" dir="${aux.debian.lib.dir}">
<include name="commons-lang3-3.4.jar"/>
<include name="commons-io-2.4.jar"/>
...
</fileset>
And I then use zipgroupfileset to include those in the final runnable JAR:
<jar destfile="myapp.jar" filesetmanifest="mergewithoutmain">
<zipgroupfileset refid="shared_lib"/>
<manifest>
<attribute name="Main-Class" value="mypackage.myapp"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="./classes" />
</jar>
When I then run ProGuard I get the following warnings multiple times (as each of the zipped JARs contain a LICENSE.txt and NOTICE.txt):
[proguard] Warning: can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [myapp.jar:META-INF/LICENSE.txt])
[proguard] Warning: can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [myapp.jar:META-INF/NOTICE.txt])
I've tried the various exclude options when defining the fileset, but they only exclude files from the fileset, not from within the include JARs.
Is there away to simply filter what gets zipped as part of the zipgroupfileset? Perhaps in ProGuard?
This is only a minor annoyance, as everything works with the warnings, but it's annoying me that something that I have a feeling should be simple is eluding me.
You can add duplicate="preserve" to your <jar> element, as described in the documentation:
duplicate
behavior when a duplicate file is found. Valid values are "add", "preserve", and "fail". The default value is "add".
I'm using Apache Ant to compile, jar and run a java project for school. I've run into a problem that I don't know how to include a single .txt file into my .jar, and then reference it as well in my java code.
Here is my compile and jar command in my build.xml.
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
</jar>
</target>
What command do I use to include a .txt file into my jar. I've seen a couple Stack Overflow questions about it already, notably
adding non-code resources to jar file using Ant
How can I include data text files in a jar using Ant?
But none of them really explained how to include a file. They just had commands that didn't really make sense to me. I've tried,
<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources" />
</jar>
But that didn't seem to help because I just don't understand Ant, I couldn't find documentation for it that actually explained why you do different things in ant.
My .txt file is in, src/resources.
The <jar> task tasks multiple filesets:
<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources" />
</jar>
The <manifest> task is a sub entity. It's very much like the <manifest> task. In your jar is a file called META-INF/MANIFEST.MF and this is adding another attribute to that jar. This particular attribute tells the Jar what is the main class which should launch when the jar is exeuted.
The <fileset dir="src/resources"/> is adding all the files that are found in theresources` directory. What if you only want to add a particular file? You can use selectors. For example:
<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources">
<include name="foo.txt"/>
</fileset>
</jar>
This time I'm not adding the entire resources directory tree. I'm just adding a single file called foo.txt into my jar file.
Use <copy>:
<target name="copy" depends="blablabla" description="Copies additional files">
<echo>Start Copying Files</echo>
<copy file="./src/resources/a.txt" overwrite="true" todir="build/classes/"/>
<echo>End Copying Files</echo>
</target>
As a side note
If we want to include some files under src folder, we can write under target something like:
There is nothing in the documentation that says, "you can only add .class files". The fileset tag should work. Perhaps it's putting the file in a place you're not looking. Run in verbose mode to figure out what it's doing with your .txt file. To control the destination, you should use the zipfileset tag.
But that didn't seem to help. My .txt file is in, src/resources.
You're going to have to elaborate on that. How didn't it help? Is the problem that the file was nested under the zip or didn't go in there or you got an error?
I have defined a jar task using ant build.xml. I need to bundle all the dependencies into my jar. I don't understant why ant extracts the jars and includes both .jars and .class files into my jar. It unnecessarily increases the size of my jar file. Here is my jar task.
<jar destfile="build/main/ERS2SupportingUtilities.jar">
<fileset dir="target/classes">
<exclude name="*.sh"/>
</fileset>
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Main-Class" value="com.cibc.ers2.invoker.JobTest"/>
<attribute name="Class-Path" value="./lib/log4j-1.2.16.jar
./lib/org.springframework.context-3.0.3RELEASE.jar
./lib/org.springframework.asm-3.0.3.RELEASE.jar
./lib/junit-4.7.jar
./lib/org.springframework.orm-3.0.3.RELEASE.jar
./lib/org.springframework.transaction-3.0.3.RELEASE.jar
./lib/org.springframework.aspects-3.0.3.RELEASE.jar
./lib/commons-pool-1.5.4.jar
./lib/org.springframework.core-3.0.3.RELEASE.jar
./lib/commons-logging-1.1.1.jar
./lib/HashUtility.jar
./lib/org.springframework.expression-3.0.3.RELEASE.jar
./lib/commons-lang-2.6.jar
./lib/org.springframework.instrument-3.0.3.RELEASE.jar
./lib/mockito-all-1.9.5.jar
./lib/com.springsource.org.aopalliance-1.0.0.jar
./lib/ojdbc14.jar
./lib/commons-io-2.4.jar
./lib/commons-collections-3.1.jar
./lib/org.springframework.jdbc-3.0.3.RELEASE.jar
./lib/spring-batch-infrastructure-2.1.9.RELEASE.jar
./lib/org.springframework.context.support-3.0.3.RELEASE.jar
./lib/commons-dbcp-1.4.jar
./lib/spring-batch-test-2.1.9.RELEASE.jar
./lib/org.springframework.beans-3.0.3.RELEASE.jar
./lib/org.springframework.oxm-3.0.3.RELEASE.jar
./lib/org.springframework.aop-3.0.3.RELEASE.jar
./lib/commons-beanutils.jar
./lib/org.springframework.binding-2.1.1.RELEASE.jar
./lib/spring-batch-core-2.1.9.RELEASE.jar
./lib/org.springframework.test-3.0.3.RELEASE.jar
./launch-context.xml
./log4j.xml"
/>
</manifest>
</jar>
I have also tried zipgroupfileset but that also is giving the same problem.
EDIT
Apologies for not giving enough info about what I am trying to achieve. I have got an application which I am compilint # target/classes. I need to package this application including my class files and dependecies into one jar.
<fileset dir="target/classes">
<exclude name="*.sh"/>
</fileset>
includes all files from target/classes which are not shell scripts.
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
It seems like you again include classes and jars here.
If you want only jar change all above to :
<zipfileset dir="./src/main/resources/lib" includes="*.jar"/>
In my project I can include packages under src folder, but I need to include other folders (which don't contains .java or .class files) and libraries.
How do I include libraries and other non class folders in a project while creating a jar file using <jar> task in my build.xml ?
Here's a snapshot of how I do this:
<jar destfile="adasim-${version}-deps.jar" manifest="resources/MANIFEST.MF">
<fileset dir="${class.dir}"/>
<fileset dir=".">
<include name="resources/xml/adasim.xsd"/>
<include name="resources/VERSION"/>
</fileset>
<archives>
<zips>
<fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/junit*.jar"/>
</zips>
</archives>
</jar>
you use <fileset> to select files that you want to copy into the JAR and you use <zips> combined with JARs to include that contents from JARs.
Extensive documentation can be found in the ant manual.
My Question is: How am i able to put files in a subdirectory into my jar via ant? Right now my Code is:
<jar destfile="${dist.dir}\wo42.jar" basedir="bin">
<manifest>
<attribute name="Main-Class" value="org.alternativedev.wo42.App" />
<attribute name="Class-Path" value="lib" />
</manifest>
<zipgroupfileset dir="lib/." excludes="natives/*" />
<fileset dir="data/." includes="." />
It creates a structure like
ROOT-Jar
-org
--bla
-filefromdata1
-filefromdata2
But it should be
ROOT-Jar
-org
--bla
-data
--filefromdata1
--filefromdata2
Do You know what I mean?
Greetings, BigTeddy
Change the last line to
<fileset dir="." includes="data/**" />
No need to copy files around.
An alternative way (which is useful if you want to have the directory in the archive to have a different name) would be
<zipfileset dir="data" includes="." prefix="folder-name-in-jar"/>
First, you create the file structure you need and copy to it all the files required. Then you run jar command on the resulting root directory.
In order to copy files you can use the ANT copy task
For example:
<copy todir="../dest/dir">
<fileset dir="." includes="data/**/*.java">
</fileset>
More on how to pack jar (basics) here