I have a directory structure like this (pretty much standard in Netbeans)
src/
build/generated/gen-1
build/generated/gen-2
...
build/generatad/gen-n
I'm packaging all the sources in a single jar with the following ant command:
<jar destfile="sources.jar" compress="true">
<fileset dir="src"/>
<fileset dir="build/generated/gen-1"/>
<fileset dir="build/generated/gen-2"/>
...
<fileset dir="build/generated/gen-n"/>
</jar>
Is there a compact way to say that I want to jar together all the subdirectories of build/generated, but avoiding the gen-i prefix?
I tried fiddling with <dirset>, but was not able to mix it with <jar>.
I would like a way which avoids copying everything to a temporary directory (like in this answer).
Assuming you have at least Ant 1.8 you can use <mappedresources>
<jar destfile="sources.jar" compress="true">
<fileset dir="src"/>
<mappedresources>
<fileset dir="build/generated"/>
<regexpmapper from="^gen-\d*/(.*)$$" to="\1" handledirsep="true"/>
</mappedresources>
</jar>
(note the doubled dollar sign in the from attribute, which is actually a single dollar in the regexp due to the way Ant does property expansion in attributes)
I doubt it is possible in Ant. You can take a look at one-jar library, but that one also requires all file sets to be specified.
If you have a chance of using another build tool, then I would recommend Gradle, where it is definitely possible.
Related
I want to create a .jar file with Ant, which contains all JavaDocs of my library and its dependencies. I did a lot of searching yesterday afternoon/evening and this morning, but none of the solutions work for me.
My first solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}" windowtitle="${ant.project.name}">
<classpath path="${lib}/nv-websocket-client-2.9-javadoc.jar"/>
<classpath path="${lib}/gson-2.8.6-javadoc.jar"/>
</javadoc>
My second solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}">
<classpath>
<fileset dir="${lib}">
<include name="gson-2.8.6-javadoc.jar"/>
<include name="nv-websocket-client-2.9-javadoc.jar"/>
</fileset>
</classpath>
</javadoc>
In both cases, however, only the JavaDoc for my own code is produced. The libraries are completely ignored. In the log of the Ant-Task there are errors, that the classes from the libraries were not found.
I don't know if this is the best aproach, but for me it's easier to use "jar" from command line.
Then all you have to do is indicate where are your files located:
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_files\*.*
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_other_files\*.*
If the libraries you want to add are already packed in a jar file, I would extract them first in the root directory, so the path of every file is correct. If you execute the previous commands, you'll have all your files in the "your_jar_file.jar" file.
if you type jar --help from command line you'll see more options. I hope it helps.
Here is an example-target that I tried. Turns out, it wants to delete everything because the comma separates "**/*" and "cover" -- understandable.
<target name="clean">
<delete
verbose="true">
<fileset dir="." includes="**/*.pyo"></fileset>
<fileset dir="." includes="**/*,cover"></fileset>
</delete>
</target>
How do I specify an embedded comma?
I'm trying to learn Ant so I won't have to maintain different build-systems for different operating-systems. In this case, it's in a Python environment, where *,cover files are created by a code-coverage checking tool called Coverage.
You don't need to escape this. Just use <include/> instead of includes arg. Try this:
<project name="test" default="clean">
<dirname property="build.dir" file="${ant.file.test}" />
<target name="clean">
<delete>
<fileset dir="${build.dir}/test">
<include name="**/*,*.xml" />
</fileset>
</delete>
</target>
</project>
By the way. You shouldn't use . (dot) in you dir argument. If you want to delete files in directory where you have got build.xml file you should pass absolute path (to do this you can use <dirname/> like in my example). If you will use . then you will have problems with nested build. Let's imageine that you have got two builds which delete files but first build also call second build:
maindir/build1.xml
<delete dir="." includes="**/*.txt" />
<!-- call clean target from build2.xml -->
<ant file="./subdir/build2.xml" target="clean"/>
maindir/subdir/build2.xml
<delete dir="." includes="**/*.txt" />
In this case build2.xml won't delete *.txt files in subdir but *.txt files in maindir because ant properties will be passed to build2.xml. Of course you can use inheritAll="false" to omit this but from my experience I know that using . in paths will bring you a lot of problems.
Unless you have other files with names that end in cover that you don't want to delete, just leave the comma out:
<fileset dir="." includes="**/*cover"></fileset>
If you do have other files that end in cover that you don't want deleted, try the backslash suggestion from MattDMo's comment. You may have to double-backslash it ("**/*\\,cover").
Another possibility: Can you configure Coverage to put its output in another directory, so you can just delete the whole directory? Or can you configure it to use a different output filename so you don't have this problem? I'm not familiar with Coverage, but looking at the link you provided, it looks like the data_file option might do one or both of those things.
I have multiple Java eclipse projects. Each of them has "jardesc" file for building jar. It's nice - double click -> finish and jar file is made. But when i have to export several jars it's a pain - i have to repeat procedure several times.
Please tell me, can i use Ant script to run several "jardesc" files at once (and get several jars according to each jardesc file)? How to do it?
You could use the jar target to make the jars for you:
<jar destfile='destination.jar' basedir='source\dir\' />
so your build.xml would look a little like this:
<project default="makejars">
<target name="makejars">
<jar destfile="app1.jar" basedir="app1\src\" />
<jar destfile="app2.jar" basedir="app2\src\" />
<jar destfile="app3.jar" basedir="app3\src\" />
</target>
</project>
then just run ant in the same directory as build.xml, and the jars should be made.
Take a look at subant task in ant. You can create ant-file which would call other files to.
<subant target="create_jar1">
<fileset dir="." includes="jar2.xml"/>
</subant>
<subant target="create_jar2">
<fileset dir="." includes="jar1.xml"/>
</subant>
You can use some loops to create ant parameters however there is no way to loop to create multiple jars (even with ant-commons extension), a copy & paste is the only viable solution unless you want to write an ant plugin (which doesn't really take that much 2 hours reading docs + write simple plugin)
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...
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>