Ant jar task - Include files not underneath basedir - java

I am trying to write an ant target that will create a jar based on a bunch of java files and some properties files, but am having trouble including the properties files the way the project is set up currently.
These are the ant targets I have:
1 - Compile the java souce files from the "myjar.src" folder and put the resulting classes into a "myjar.classes" folder. Once this is done copy all non .java files from "myjar.src" to "myjar.classes".
2 - Create the jar using the "jar" command using basedir = ${myjar.classes} and tell it to include everything.
These are the ant targets I want:
1 - Compile the java souce files and put the resulting classes into a "myjar.classes" folder. Only .java files are included
2 - Create the jars using the "jar" command using basedir = ${myjar.classes.location} but also include the .properties and .xml files from "myjar.src."
The key difference is I want the properties and xml files from "myjar.src" to be included when I package up the classes in "myjar.classes" using basedir = ${myjar.classes} - How do I include these fies when they are not underneath "myjar.classes"?
This is the ant target I want to modify:
<CreateManifest title="myjar classes etc"/>
<jar
destfile="${myProject.build.jars.dir}\ta_test_driver.jar"
basedir="${myjar.classes}"
manifest="${manifest}">
<include name="**"/>
</jar>
The reason I am not just using a directory one level up for basedir is that none of the other jar creation calls in the project do that, and I am hesitant to change that for just this one. I did try to do that, but had trouble specifying the right directory. (There are other jars that use a similar directory structure, and I don't want to interfere with them either now or in the future.) I was just wondering if these is a better way to do this? Thanks very much.

I managed to figure this out from here: How to include file in Jar through Ant at specific location
This was my modified ant call in the end:
<CreateManifest title="myjar classes etc"/>
<jar
destfile="${myProject.build.jars.dir}\ta_test_driver.jar"
basedir="${myProject.classes}\ta_test_driver"
manifest="${manifest}">
<include name="**"/>
<zipfileset dir="${myjar.src}"
includes="**/*.xml, **/*.properties, **/*.gif"
/>
</jar>
Notice the zipfileset tag.

Related

How to exclude a source package from jar in Netbeans project

I have a Netbeans project where I am letting Netbeans handle the packaging; its using Netbean's build-impl.xml Ant script to package it. There are certain packages that I do not want ending up in the final jar that Netbeans creates. Is there a way to exclude those packages from ending up in the final jar?
In the build-impl.xml file I think this is where the Jar command is called, or at least defined.
<target name="-init-presetdef-jar">
<presetdef name="jar" uri="http://www.netbeans.org/ns/j2se-project/1">
<jar compress="${jar.compress}" index="${jar.index}" jarfile="${dist.jar}">
<j2seproject1:fileset dir="${build.classes.dir}" excludes="${dist.archive.excludes},**/editor/**"/>
</jar>
</presetdef>
</target>
One of the packages that I want to exclude is called "editor". I added it in the copy above in effort to try to get it to be excluded but that didn't work, the "editor" package and all its classes still show up in the final jar.
Any ideas how I can get that package excluded from the jar? I would rather not touch the build-impl.xml file and instead accomplish this by messing with the build.xml file which Netbeans sets aside for us to mess with... so bonus points if the solution only touches that file.
thanks
OK I found two ways to do this (each with their own drawbacks), both from the Sources section of the Properties window.
1) Move the packages you do not want to one of your test package folders. In Properties -> Sources you can add or edit your test Package Folders. The drawback here is that you can only use the package as a test. This worked fine for me because the only time I had to use that code was when I was on my IDE. It might become an issue if I ever need to add this package to a jar.
2) In Properties -> Sources at the lower right hand there is a button labeled Includes/Excludes that you can use to add files or packages to exclude for the final jar. The drawback here is that the code is completely unusable if it relies on the other packages. Netbeans essentially ostracizes it. This didn't work for me.
An ideal solution would be a way to design which packages make it to the final jar then have various profiles for building these jars. That is to say I can build Jar-A or Jar-B, each with their own packages. I will wait for that solution before marking this question answered.
2) You can directly define all of the *.class files that has to be excluded rather than defining the whole package from the Final jar. Go to the tag in your build-imple.xml file
<target name="-init-presetdef-jar">
<presetdef name="jar" uri="http://www.netbeans.org/ns/j2se-project/1">
<jar compress="${jar.compress}" index="${jar.index}" jarfile="${dist.jar}">
<j2seproject1:fileset dir="${build.classes.dir}" excludes="${dist.archive.excludes},**/editor/**"/>
</jar>
</presetdef>
</target>
which generates jar file. Now find the attribute excludes="${dist.archive.excludes} And EDIT this.
Example: excludes="**/X*.class,**/Y*.class,**/FullClassName.class"
You don't need to move your packages anywhere. You can still exclude all of the *.class files that you don't want to include in your final JAR.
Open your nbproject/project.properties file and find dist.archive.excludes then give all of the *.class file name (to be excluded) as a comma-separated value to that key dist.archive.excludes.
Example:
dist.archive.excludes=`**/X*.class,**/Y*.class,**/FullClassName.class`
**/X*.class, **/Y*.class, X and Y are prefixes of the class, You can give the full class name as shown above
The syntax is **/X*.class where X is the prefix of the class name.

Some clarifications about how ant copy some files into a folder? [duplicate]

This question already has an answer here:
Some clarification about how ant copy some files into a folder?
(1 answer)
Closed 8 years ago.
I am pretty new to ant (I came from Maven and Ant is a nightmare for me !!!)
I have this target:
<target name="linuxdriver"
description="Linux Driver">
<copy file="${deps.linuxdriver.dir}/${deps.linuxdriver.name}"
tofile="${project.datadir}/${deps.linuxdriver.name}"/>
<copy file="${deps.linuxdriver.dir}/${deps.linuxdriver.name}"
tofile="${project.deploy}/data/${deps.linuxdriver.name}"/>
<chmod perm="+x" file="${project.datadir}/${deps.linuxdriver.name}"/>
<chmod perm="+x" file="${project.deploy}/data/${deps.linuxdriver.name}"/>
</target>
and I have also a property file in which there is definied the "variable" (are named variable?) used in the previous ant code, specifically I have:
project.datadir = ${basedir}/data
project.deploy.dir = Release
project.deploy = ${basedir}/../${project.deploy.dir}
deps.linuxdriver.name = atmosfs
And now I have some doubts:
1) What represents ${basedir}? A specific directory? What? Reading on the ant manual (http://ant.apache.org/manual/properties.html) say that this is: the absolute path of the project's basedir (as set with the basedir attribute of ).
So, is it the absolute path of my project in the Eclipse workspace?
2) Using the previous information what exactly are the two destination folder in which the files are copied (using the "copy file...to file" tag)?
1) What represents ${basedir}? A specific directory?
Yes. ${basedir} is the directory where you either started Ant, or the directory specified in the <project> entity on the top of your Ant file. Normally, it is set to "." which makes it the same directory as the directory that contains your Ant build file.
2) Using the previous information what exactly are the two destination folder in which the files are copied (using the "copy file...to file" tag)?
You didn't list your whole Ant file, and your whole properties file. I'm not even sure if your properties file is read in (You need a <property file="xxxx.properties"/> near the top of your Ant file).
Assuming that you are executing this in the same directory as your Ant file, and your ${basedir} is the same directory as your Ant file:
<copy file="${basedir}/atmosfs/atmostfs"
verbose="true"
tofile="${basedir}/Release/atmosfs"/>
<copy file="${basedir}/atmofs/atmofs"
verbose="true"
tofile="${basedir}/../Release/data/atmofs"/>
Again, I am assuming ${basedir} is the directory where your Antfile is stored, and that you are executing the script from that directory.
Notice I have verbose="true" in the <copy>. I recommend you make that change. This will show you what file is being copied and where when <copy> is executed. It's probably the best way to handle this.
By the way, one rule I have is that all action takes place in the project tree. Your last tofile is being written outside of the project directory (where I assume your Ant file is located). Imagine someone checking out the project, and finding out that the build process wrote a file outside of the checked out directory and onto his computer in a random place. Doing this is just considered impolite.
Even more polite is to write all files and do all build processing under a subdirectory. Some people use build, I prefer target because that's a Maven standard. THe idea is that I can clean up the entire build process by simply deleting that one directory.

How do I change the destination directory of Ant's fileset command?

I have an Ant buildfile for a Java library. It looks something like this:
<project ... ><target ... >
<jar destfile="C:\path\to\export.jar">
<manifest> ... </manifest>
<fileset dir="C:\path\to\bin" />
<fileset dir="C:\path\to\src" />
<fileset dir="C:\path\to\doc" />
<zipfileset src="C:\path\to\included\library.jar" />
</jar>
</target></project>
The only problem is that my JavaDoc is being exported directly into the root directory of the resulting jar file. Essentialy, I'd like some equivalent of the <copydir> command that can be used inside the <jar> command.
My desired structure is this:
export.jar
META-INF
Manifest.MF
com
example
whatever
Blah.class
Blah.java
org
external
somelibrary
Magic.class // contents of the included library jar file
doc
// javadoc files here
The current structure is:
export.jar
META-INF
Manifest.MF
com
example
whatever
Blah.class
Blah.java
// some javadoc files here
org
external
somelibrary
Magic.class // contents of the included library jar file
// more javadoc files here
My current "solution" is to omit the documentation <fileset> command, then, once the jar has exported, go into Windows Explorer and right click → 7-Zip → Open Archive; I can then drop the doc directory in there just fine. However, this pretty completely defeats the purpose of Ant as a completely automated build system.
If it matters, this file was originally generated by Eclipse with the Runnable JAR exporter. However, I obviously need to modify it to add source files, etc. because it's a library and not actually a runnable jar. I exported it as a runnable jar to get Eclipse to package in the required libraries; apparently libraries on the build path aren't available for export via the standard File → Export → JAR file.
A jar is actually like a zip file. Hence you can use a zipfileset. Its attribute prefix is what you are looking for.
The zipfileset command can accept either a zip file via src or a filesystem directory via dir. Using the latter, you can add the following command:
<zipfileset dir="C:\path\to\doc" prefix="doc" />
Also worth to note is that zipfileset supports all attributes of fileset. Thus if you want to include just a single file in a specific location you can use:
<zipfileset file="C:\path\to\doc\file.txt" prefix="doc" />
Further reading: http://ant.apache.org/manual/Types/zipfileset.html

Ant <jar> task: using excludes parameter

Got a following build.xml string:
<jar destfile="${lib.dir}/rpt.jar" basedir="${classes.src}" excludes="**/*.java" />
I am new to Ant and i don't understand how excludes string works. What files are affected? All java source files?
Thanks.
First about the statement
<jar destfile="${lib.dir}/rpt.jar" basedir="${classes.src}" excludes="**/*.java" />
this target is used to package your files inside a jar archive
destfile : specifies the name and location of the destination file, the archive that would be created
basedir : specifies the base directory of the files that needed to be packaged. note that all files and subfolders would be included
excludes : this is used to exclude files from basedir that you dont need inside your package (jar)
Now to your question
what the above statement would do is that it will package all the files inside classes.src to $(lib.dir)/rpt.jar but will exclude any .java files found at or inside any sub folder of basedir.
EDIT :
This exclude="*/.java" is generally done to exclude source code form the jar which would be used, distributed,exported etc
Yes, with your code all Java files are excluded.
Take a look at the pattern definition: This page explains pretty good, how the Ant patterns work. It also contains a lot of examples illustrating it. Patterns are used everywhere, so if you continue working with Ant, you really need to understand them.
The ** basically means every sub directory. And /*.java means every Java file in these directories.
<jar destfile="${lib.dir}/rpt.jar" basedir="${classes.src}" excludes="**/*.java" />
is equivalent to
<jar destfile="${lib.dir}/rpt.jar">
<fileset dir="${classes.src}" excludes="**/*.java" />
</jar>
i.e. it includes all files from the ${classes.src} directory and its subdirectories except those ending in .java (and except the default excludes)

Can ant replace text in files inside a jar/ear/war?

I want to build my ear file once and then use ant to change some settings in application.xml, property files etc.
Is there way to do this with ant?
[edit] Just found this
How do I modify a file in a jar file using ANT?
The only way you can modify a file inside your jar or ear is to use the <unzip> task, use the <replace> task to modify the fields in the file, and then rezip the file back up with either the <zip> or <jar>/<ear> task.
There are several ways you can handle this without having to unzip and rezip your ear/jar/war files:
The preferred method is to setup your application server, so it can find your properties outside of the ear itself. It is also possible to configure the application.xml file not to use relative directories when specifying locations instead of specifying locations from the root of the machine. By removing embedded environment information from your ear, you can use the same earfile on all of your environments.
We, unfortunately, are unable to do the above and must provide separate ear files for each environment. We use Jenkins as our continuous build server. When Jenkins does our builds, we build multiple ears, one for each environment, at the same time. This way, as we move from Dev to QA to STAGE to Production, we can at least refer to the same build number. We compile once, then use the AntContrib <foreach> task to ear up the earfile with the correct properties file settings and the correct application.xml file. We use <filterset> in our <copy> task to modify the properties and application.xml as we build the ear.
You can do something like this
<zip destfile="tmp.jar" >
<zipfileset src="lib/myjar.jar" excludes="org/example/My*.class" />
<zipfileset dir="bin" includes="org/example/My*.class" />
</zip>
<move file="tmp.jar" tofile="lib/myjar.jar"/>
In this example we create a tmp.jar using myjar.jar as the source but excluding all classees beginning My in the org/example directory. We then add our new version in from the bin directory. We then replace the jar file with our new version.

Categories