How to jar java source files from different (sub-)directories? - java

Consider the following directory structure:
./source/com/mypackage/../A.java
./extensions/extension1/source/com/mypackage/../T.java
./extensions/extension2/source/com/mypackage/../U.java
...
./extensions/extensionN/source/com/mypackage/../Z.java
I want to produce a source jar with the following contents:
com/mypackage/../A.java
com/mypackage/../T.java
com/mypackage/../U.java
...
com/mypackage/../Z.java
I know I could use a fileset for each source directory.
But is there an easy solution using ANT without having to refer to all extensions explicitly?

How about flattening all the files to be included in the archive into a single directory structure, then archiving from there?
Use a regexpmapper to do the flatten during copy, something like this:
<delete dir="merged" />
<mkdir dir="merged" />
<copy todir="${basedir}/merged">
<fileset dir="${basedir}">
<include name="source/**"/>
<include name="extension*/**"/>
</fileset>
<regexpmapper from=".*source/(.*)" to="\1" />
</copy>
<jar destfile="mypackage.jar" filesonly="yes">
<fileset dir="merged">
<include name="**" />
</fileset>
</jar>

Related

using ANT to package the Web app directory with the executable jar

I am working on an open source project that uses ANT which can perform all kind of useful targets.
One of them generate an executable jar and a webapp directory in the same directory. I tried to find ways to package that webapp directory with the jar, there is tons of information out there but non of them provide a example that fits my problem.
Open Refine is a great data cleansing tool, you can start it using a script that takes different start up arguments.
It is built using jetty Web server and Web app for UI. Architecture here.
It uses ANT for deployment management, where there is a task that create the Linux kit:
<target name="linux" depends="jar, prepare_webapp">
<mkdir dir="${linux.dir}"/>
<copy todir="${linux.dir}/server/lib">
<fileset dir="${server.lib.dir}">
<include name="**/*.jar"/>
</fileset>
</copy>
<copy file="${build.dir}/${fullname}-server.jar" tofile="${linux.dir}/server/lib/${fullname}-server.jar"/>
<copy todir="${linux.dir}/webapp">
<fileset dir="${built.webapp.dir}">
<include name="**"/>
</fileset>
</copy>
<mkdir dir="${linux.dir}/licenses"/>
<fixcrlf srcDir="${basedir}/licenses" destDir="${linux.dir}/licenses" eol="lf"/>
<fixcrlf srcDir="${basedir}" destDir="${linux.dir}" eol="lf">
<include name="refine"/>
<include name="refine.ini"/>
<include name="README.txt"/>
<include name="LICENSE.txt"/>
</fixcrlf>
<mkdir dir="${dist.dir}"/>
<tar longfile="gnu" compression="gzip" destfile="${dist.dir}/openrefine-linux-${version}.tar.gz">
<tarfileset dir="${linux.dir}/.." filemode="755">
<include name="${release.name}/refine"/>
</tarfileset>
<tarfileset dir="${linux.dir}/..">
<include name="${release.name}/**"/>
<exclude name="${release.name}/refine"/>
</tarfileset>
</tar>
</target>
This task creates a web directory, which is used in the previous task:
<target name="prepare_webapp" depends="jar_webapp, build">
<mkdir dir="${built.webapp.dir}" />
<copy todir="${built.webapp.dir}">
<fileset dir="${webapp.dir}">
<include name="**/*"/>
<exclude name="WEB-INF/classes/**"/>
<exclude name="WEB-INF/lib-src/**"/>
<exclude name="WEB-INF/lib/icu4j*.jar"/>
</fileset>
</copy>
and the Jar are prepared using another ANT target as following:
<target name="jar_webapp" depends="prepare_jar, build_webapp">
<jar destfile="${build.dir}/${fullname}.jar" basedir="${webapp.classes.dir}"/>
</target>
The result of running ant linux is an executable jar and a webapp directory, Is it possible to package the entire thing as one fat jar?

Ant: How to include library in javadoc?

I have made an Ant build file for my Java project. I'm using the matlabcontrol library, i.e. I have the following files:
lib: matlabcontrol-4.1.0.jar
lib/src: matlabcontrol-4.1.0-sources.jar
lib/docs: matlabcontrol-4.1.0-javadoc.jar
I have two questions regarding my build file:
<path id="project-classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
Will this also include the source and javadoc file? Of course I don't want to include them.
Second, I'm creating the javadoc with the following code:
<target name="docs" depends="clean docs, compile">
<mkdir dir="${docs.dir}" />
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
Is there a possibility to also include the javadoc and sources of the matlabcontrol library or is this usually not done?

How can I change the name of the jar file after siging it - Apache ANT

I am signing my jars using following ant commands.
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
signedjar="${dist.dir}/${jar.signed.fileName}"
lazy="true">
<fileset dir="${dist.dir}">
<include name="*.jar" />
<include name="lib/*.jar" />
</fileset>
</signjar>
For single file it is fine that I can change the name of the jars file after signing it,
but when I have multiple jars the above line is not useful, what i am trying to accomplish is ,
for example
If the unsigned jar file name is ab.jar after signing it I want to prependSigned_to it's name likeSigned_ab.jar` and so on for all the jars i have in my fileset.
Can anyone tell me how to do that ?
You can use the ant jar command/task like below in your build script :
<jar basedir="bin" destfile="Signed_${jar-name}.jar">
btw, you missed to type the command you're using, in your question.
The signjar task can take a mapper defining how to translate the input file name into an output file name:
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
destdir="${dist.dir}"
lazy="true">
<fileset dir="${dist.dir}">
<include name="*.jar" />
<include name="lib/*.jar" />
<!-- since we're dumping signed JARs in the same dir as the source
ones, we need to prevent already-signed JARs from being signed
again. A better approach might be to put the Signed_* JARs in
a different directory -->
<exclude name="**/Signed_*" />
</fileset>
<regexpmapper handledirsep="yes"
from="^(.*?)/([^/]*)$$" to="\1/Signed_\2" />
</signjar>
May be you can write couple of macros & call 'signJarsParallel' macro once for every directory for which you want to sign jars.
This is not tested code. You can give it a try. It makes use running tasks parallely & hence increasing speed & signs to temporary-prefixed name you want :
<macrodef name="signMyJars">
<element name="myJar" implicit="true"/>
<sequential>
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
lazy="true">
<myJar/>
</signjar>
</sequential>
</macrodef>
Call only below macro inside your ant target.
<macrodef name="signJarsParallel">
<attribute name="dirName"/>
<sequential>
<for param="file" parallel="true" threadCount="5">
<path>
<fileset dir="#{dirName}">
<include name="*.jar"/>
<include name="lib/*.jar"/>
</fileset>
</path>
<signMyJars>
<fileset file="Signed_#{file}.jar"/>
</signMyJars>
</for>
</sequential>
</macrodef>

Exclude some JARs from Web-Inf/Lib Folder

I'm starting with Ant. I created a build.xml to generate a WAR file of a Web Project and it worked OK.
Then, I made some change to exclude all *.jar from WEB-INF/lib Folder and also works OK.
Now I need to make the changes to exclude all JARs files, but leave some especial JARs in WEB-INF/lib Folder. This JARs are from other project created by me.
The idea es exclude all third parties JARs and only leave my own JARs inside WEB-INF/lib folder.
There is some way to do that?
All my Jars start with "fnet" so maybe I can use that to create some rule, but I don't know how to do that
This is my Build.xml:
<?xml version="1.0" ?>
<project name="warConLibs" default="build-war">
<target name="clean">
<delete file="c:/projweb.war"/>
<delete file="c:/projweb_sl.war"/>
</target>
<target name="build-war">
<war destfile="c:/projweb.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
<target name="build-war-sin-libs">
<war destfile="c:/projweb_sl.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
<exclude name="**/*.jar"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
</project>
The correct way to exclude a jar file is given in the documentation. If anyone face same issue, they can refer to this link.
This example is taken from the documentation, here we are removing jdbc1.jar from lib
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
You may want to read again about the war Ant task: https://ant.apache.org/manual/Tasks/war.html
The correct syntax would be:
<war destfile="..." webxml="...">
<lib dir="WebContent/WEB-INF/lib">
<include name="fnet*.jar"/>
</lib>
<classes dir="bin"/>
</war>

Compiling j2me using Ant

I have created a J2ME project after referring to this article J2MEUsingAntwithJ2ME. Now I am having problems in adding resources (such as images) and libraries (such as jar and zip files).
I have copied the resources in the res folder as shown in this article but when I extract the .jar file, it does not have any resources.
From the sample:
<jar basedir="${build}/preverifiedobf"
jarfile="${build}/bin/${program_name}.jar"
manifest="bin/MANIFEST.MF">
<fileset dir="${top}/${res}">
<include name="${package_name}/*.png"/>
</fileset>
</jar>
This will only include *.png files which are in /res folder. If you want to include more types, add more <include> lines or include "${package_name}/**".
If you want to include the content of existing .jar files, you can unjar them like this:
<mkdir dir="${build}/libs"/>
<unjar src="yourlibrary.jar" dest="${build}/libs" />
Then you can jar them up again:
<jar basedir="${build}/preverifiedobf"
jarfile="${build}/bin/${program_name}.jar"
manifest="bin/MANIFEST.MF">
<fileset dir="${top}/${res}">
<include name="${package_name}/*.png"/>
</fileset>
<fileset dir="${build}/libs">
<include name="**/*"/>
</fileset>
</jar>
The Apache Ant manual contains a lot of examples for all the supported tags.

Categories