Ant including both jar files and .class files in dest jar - java

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"/>

Related

Ant does not include generated class files to packaged jar, until I do a Project->Clean in Eclipse

I ran into some strange behaviour that seems to be connected to Eclipse and it's way to refresh the workspace.
I'm using an Ant build file to create my class-files and to create a package from that class-file. This functionality is working fine.
This is the target doing the work:
<target name="package" depends="compile,javaDoc">
<jar destfile="${dist.dir}/MyApplication.jar" basedir="${build.dir}" includes="**/*.class">
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="MyApplication" />
</manifest>
</jar>
</target>
Now I want to add functionality to first clean the directories where the class-files and the jar file are beeing generated.
This is the target code:
<target name="clean" description="Removes all *.class and *.jar files. Also deletes the java doc files.">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" /></target>
This target also works fine.
Now I would like the package target dependent on the clean target, so that the folders are first cleaned before the packaging is beeing done.
This would look like this:
<target name="package" depends="clean,compile,javaDoc">
<jar destfile="${dist.dir}/MyApplication.jar" basedir="${build.dir}" includes="**/*.class">
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="MyApplication" />
</manifest>
</jar>
</target>
The problem is, that this target does not include the generated class files into the jar. Even on the file system the generated class files are not displayed. Only after I go to Project->Clean in Eclipse and clean the project the class files become visible on the filesystem.
Any help is appreciated.
My compile target looks like this:
<target name="compile" depends="init" description="Compiles the source file to the created directory.">
<javac classpathref="classpath" srcdir="${dist.dir}" destdir="${build.dir}" includeantruntime="false" />
</target>
fixing the error in the compile target worked.
That is the working version:
<target name="compile" depends="init" description="Compiles the source file to the created directory.">
<javac classpathref="classpath" srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" /></target>

Apache Ant: adding and using a .txt file

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?

How do I use ant dirset with the ear task?

I'm building an EAR with ant which needs to include the lib folder (including jars) from my EAR project. I've tried this but although a lib folder is created in the ear file no jars are included. Only the war files are copied into the ear.
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<dirset dir=".">
<include name="lib" />
</dirset>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>
I used the zipfileset task instead, which does the trick:
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<zipfileset dir="lib" prefix="lib"/>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>

Java Jar Ant include folder

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

Apache ant manifest class-path?

I have a standard project layout for a java project:
project /
src /
source_file_1.java
...
source_file_N.java
build /
classes /
source_file_X.class
...
jar /
MyJar.jar
lib /
SomeLibrary.jar
SomeOtherLibrary.jar
As far as I can tell, I am building the project correctly with Ant. I need to set the class-path attribute in the Manifest file so my classes can use the required libraries.
The following relevant information from build.xml
<target name="compile" depends="init">
<javac srcdir="src" destdir="build\classes">
<classpath id="classpath">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="build\jar\MyJar.jar" basedir="build\classes" >
<manifest>
<attribute name="Built-By" value="${user.name}" />
</manifest>
</jar>
</target>
Any push in the right direction is appreciated. Thanks
Assuming the libraries do not change location from compiling to executing the jar file, you could create a path element to your classpath outside of the compile target like so:
<path id="compile.classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
Then you can use the created path inside your javac task in place of your current classpath.
<classpath refid="compile.classpath"/>
You can then use the path to set a manifestclasspath.
<target name="jar" depends="compile">
<manifestclasspath property="jar.classpath" jarfile="build\jar\MyJar.jar">
<classpath refid="compile.classpath"/>
</manifestclasspath>
<jar destfile="build\jar\MyJar.jar" basedir="build\classes" >
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
The manifestclasspath generates a properly formatted classpath for use in manifest file which must be wrapped after 72 characters. Long classpaths that contain many jar files or long paths may not work correctly without using the manifestclasspath task.
Looking at my NetBeans-generated build file, I found this snippet in the -do-jar-with-libraries task:
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
So in other words, it looks like you just need to add another attribute to the manifest task that you already have.
See also the Manifest Task documentation.

Categories

Resources