How can I include data text files in a jar using Ant? - java

In my src folder there is another folder called data which contains files data1.txt and data2.txt. The application loads a graph from these files in the initialization, so I want to include these files in my final jar. I use Ant to produce the jar file.

Example from http://ant.apache.org/manual/Tasks/jar.html :
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"/>
<fileset dir="${src}/resources"/>
</jar>
So basically you would want to include the data-files in the same way as "resources" are included above.
From the documentation of the <jar> task:
It is possible to refine the set of files that are being jarred. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes.

Copy the files to your classes directory, where they will be included into the jar.
enter code here
<target name="copyHibernateXml">
<copy todir="classes">
<fileset dir="${basedir}/${sourceDir}" includes="*.xml,*.csv"/>
</copy>
</target>

Related

Unknown level in directory, fetch the name of directory with .java file in it

How to access the directory name where I only know it contain .java files and level of directory keep on changing. so is their any way in ANT Script to access those directories with particular .java files in it.
I am using below Script:
<target name="check">
<for param="check1">
<path>
<dirset dir="${project_path}/adapters" includes="*/src/*.java"/>
</path>
<sequential>
<echo>#{check1}</echo>
</sequential>
</for>
</target>
Result:
It is giving nothing as output because it is searching .java file inside src folder but .java files are present under multilevel directories of src. I am not getting how to do it.
As the ant fileset doc states (https://ant.apache.org/manual/Types/fileset.html) you can use ** to search recursively into directories.
<dirset dir="${project_path}/adapters" includes="*/src/**/*.java"/>

Netbeans, Ant and nesting build directories

I am having a problem with an Ant project in Netbeans 7.4 . When building a certain web app that i had to create via importing it from a WAR into Netbeans it keeps nesting the output directories in the build directory deeper and deeper.
It happens during the Ant target -copy-webdir . It outputs "x.class omitted as pathtox/x.class" for every file in the build/web directory and creates an even deeper nested set of directories on each build.
Here is a small extract from the project.properties file:
build.classes.dir=${build.web.dir}/WEB-INF/classes
build.classes.excludes=**/*.java,**/*.form
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
build.web.dir=${build.dir}/web
build.web.excludes=${build.classes.excludes}
target -copy-webdir in the nbproject/build-impl.xml:
<target name="-copy-webdir">
<copy todir="${build.web.dir}">
<fileset dir="${web.docbase.dir}" excludes="${build.web.excludes},${excludes}" includes="${includes}"/>
</copy>
<copy todir="${build.web.dir}/WEB-INF">
<fileset dir="${webinf.dir}" excludes="${build.web.excludes}"/>
</copy>
</target>
What am i doing wrong here?

Create Jar for a specific file in a project using ant

I compiled all the java files using ant and stored the class files in a folder.
I want to make jar for a specific class file form the output folder(compiled).
How can i done this.
NOTE:The class file depends another classes in the project
Use the ant Jar Task with a nested fileset that specifies only the file(s) you wish to include in the jar file.
Something like:
<jar jarfile="pinky.jar">
<fileset dir="build/classes" >
<include name="com/example/Brain.class" />
</fileset>
</jar>

Trying to get a properties file from a jar file in another jar file

I have an executable jar file (A.jar), built with Eclipse, that relies on other jar files (B.jar) that are included in the jar file, and accessed via the jarinjarloader. While this works to access the java code inside those jar files, when that code calls
InputStream inStream = <Class>.getClassLoader().getResourceAsStream(propFile);
it fails to find the properties file contained in B.jar. The code works when run in Eclipse, so I know the correct properties files are there.
I tried extracting A.jar, then extracting B.jar, and then zipping up the combined files, but this does not work. Looking at the Manifest file, the Main-Class is org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Is there some change I can make to the Manifest file (say, adding something to Rsrc-Class-Path) that will get this to work? Rsrc-Class-Path includes "./" already, but that clearly isn't letting the properties loader see my properties files.
TIA,
Greg
I'd also recommend working around nested jars. I usually just do this in my build.xml to build one jar with all the libraries unpacked into it:
<target name="fatjar" depends="compile">
<jar destfile="${isamjar}" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="com.xyz.Main"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="./bin"/>
<fileset dir="." includes="src/**"/>
<zipfileset excludes="META-INF/*.SF" src="${l}/derby.jar"/>
<zipfileset excludes="META-INF/*.SF" src="${l}/jconn3.jar"/>
<zipfileset excludes="META-INF/*.SF" src="${l}/log4j-1.2.9.jar"/>
<zipfileset excludes="META-INF/*.SF" src="${l}/${jar4j}"/>
</jar>
Turns out Eclipse has a setting I missed. When Exporting a "Runnable JAR File" you can select "Extract required libraries into generated JAR". while this may give some license issues (depending upon the JARs you're merging in), in my case it worked like a charm, giving me a single (smaller, since there were a lot of duplicates among the various jar files I'd been including) jar that actually worked.

Using ant to create a distributable zip file with JAR and libraries and doc

I have a library that is typically distributed by making a zip file of the JAR and its dependencies and its javadoc by hand. I would like to automate this task in ant.
One quirk for the intended use case for this distribution is that when it is unpacked, the JAR my team has created and any library JARs should all be in the same path. We cannot have myproject.zip/the.jar and myproject.zip/lib/a_library.jar both should be in the root path of the zip.
I have had much success using the following task:
<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc"
description="Build the distributable JAR for myproject">
<zip destfile="${basedir}/dist/myproject.zip">
<fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
<zipfileset dir="mycompany_myproject/lib" prefix="lib">
<patternset id="myproject.dist.libs">
<include name ="**/*.jar"/>
</patternset>
</zipfileset>
<zipfileset dir="docs/myproject" prefix="docs"/>
</zip>
</target>
The only thing it doesn't do is 'flatten' or move the library JARs to the root path of the zip.
I have tried using <zipfileset prefix="/"> for the libs but that did not work as expected.
The prefix attribute of the zipfileset is used to describe where the files should appear in the created zip file. Since you want the jar files to appear at the root of the zip file you don't need to specify this, and can leave it out (I'm not sure what the effect of setting it to "/" will be, I think it'll be safer to omit it).
You problem seems to be that your libs are stored in subdirectories under your lib dir, but you want them to be directly in the root of the zip file. The 'zip' task, unlike the copy task, doesn't accept a mapper directly to change how files should appear in the zip, but if you're using ant 1.7 or later it will accept a resource collection. You can use a mappedresources element with a fileset and a flattenmapper to get the effect you're after:
<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc" description="Build the distributable JAR for myproject">
<zip destfile="${basedir}/dist/myproject.zip">
<fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
<mappedresources>
<fileset dir="mycompany_myproject/lib" includes="**/*.jar" />
<flattenmapper />
</mappedresources>
<zipfileset dir="docs/myproject" prefix="docs"/>
</zip>
</target>
This means you don't have to use copy first to put the jars into a staging area.
According to this post the zipgroupfileset should do the trick; have not tried it myself though...

Categories