How can I use Ant to build a single executable .jar that has dependency .jars in it in a /lib dir in the .jar?
I have a /lib directory in the project root file that contains all the binary dependency .jars.
In java an executable jar has a specially formatted manifest, that includes the following attributes:
Main-Class
Class-Path
In ANT this is easily accomplished as follows with the jar task:
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
<attribute name="Class-Path" value="${jar-classpath}" />
</manifest>
</jar>
For extra credit one can use the very useful manifestclasspath task to help assemble the correct classpath string.
<manifestclasspath property="jar-classpath" jarfile="${jar.dir}/${ant.project.name}.jar">
<classpath>
<fileset dir="/path/to/lib/dir" includes="*.jar"/>
</classpath>
</manifestclasspath>
I need to include some third party jar file to my project jar. I mentioned it in my build.xml and include this to MANIFEST.MF. Now i get thirdparty1.jar thirdparty2.jar file into inside the project jar. But still i can't able to use the jars. Is it need any addition configuration
Here is my build.xml
<manifest>
<attribute name="Class-Path" value="thirdparty1.jar thirdparty2.jar thirdparty3.jar"/>
If i copy the two jar separately it works well. But i don't understand what is the need for copy these separate. How it solve with out copying jar separately.
If the dependency jar is packaged inside the project jar, you need a solution to load it from there. The standard class-path handling in Java won't access jar files located inside other jar files.
See this answer: Classpath including JAR within a JAR. Specifically the One Jar solution: http://one-jar.sourceforge.net/.
It's also possible to use zipgroupfileset for that.given is the sample ant task for that.
<!-- Build JAR file -->
<target name="jar" depends="init-build-dir,compile-main">
<!--creating a temp jar contains all jar -->
<jar jarfile="${project.build.lib.dir}/external-libs.jar">
<zipgroupfileset dir="${project.lib.redist.dir}">
<include name="**/*.jar" />
</zipgroupfileset>
</jar>
<sleep seconds="1" />
<!-- creating main jar with temp jar-->
<jar jarfile="${project.build.lib.dir}/${ant.project.name}.jar" manifest="MANIFEST.MF">
<fileset dir="${project.build.main.classes.dir}" includes="**/*.*" />
<zipfileset src="${project.build.lib.dir}/external-libs.jar">
<exclude name="*" />
</zipfileset>
</jar>
<!--removing temp jar -->
<delete>
<fileset dir="${project.build.lib.dir}">
<include name="external-libs.jar" />
</fileset>
</delete>
</target>
I'm using Eclipse JUnit integration which includes the JUnit library automatically into my project. The problem is that when I export my project using the Runnable JAR file destination, it includes JUnit.
Is there any way to exclude JUnit (and ideally the tests too) from the exported JAR?
If you're creating your JAR by right clicking on your project and selecting export and then picking JAR File, you can remove your tests from the export by unchecking your test folder. See this related discussion and this example.
I've found a solution to the problem by using Ant within Eclipse and the following build.xml:
<project>
<target name="jar">
<jar destfile="out.jar" basedir="bin">
<zipgroupfileset dir="lib" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="com.example.Main" />
</manifest>
</jar>
</target>
</project>
You can remove the JUnit package from '.classpath' file.Then export the jar file again
I have some jar files into /libs/ and my project classes are located at /target/. To remove dependency on external folder. I want to make a consolidate jar file. Which contains all the classes from jar files found in all /libs/*.jar
I am using following Ant script:
<jar destfile="mytest.jar" basedir="target/classes">
<manifest>
<attribute name="Main-Class" value="com.mytest.MyProcessor"/>
</manifest>
<fileset dir="target/classes" includes="**/*.class"/>
<zipgroupfileset dir="libs" includes="*.jar"/>
</jar>
But, when I run the jar using "java -jar mytest.jar" command, it says:
Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry javax/mail/internet/ContentDisposition.class
at sun.security.util.SignatureFileVerifier.verifySection(Unknown Source)
at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
Any idea, how to make a consolidated jar that have no such issues, would be highly appreciated. I am using IntelliJ IDEA.
Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry
This error message tells exactly why bundled jar is not recommended for production release. The problem is some of your dependency jar is signed with signature files. repack it may violate some security policy on the dependency jar.
To make it work, you need remove all signature files from the signed jar, before copying the unpacked files into your final build jar file. Note that <zipgroupfileset> doesn't support exclude file within jar archive, try using a list of <zipfileset> instead:
<zipfileset src="libs/mail.jar">
<exclude name="**/*.RSA, **/*.SF, **/*.DSA"/>
</zipfileset>
... ...
Something like this might work for you.
<jar destfile="mytest.jar" basedir="target/classes">
<restrict>
<not>
<or>
<name name="**/*.RSA"/>
<name name="**/*.SF"/>
<name name="**/*.DSA"/>
</or>
</not>
<archives>
<zips>
<fileset dir="libs" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Main-Class" value="com.mytest.MyProcessor"/>
</manifest>
</jar>
I adapted this from the example at http://ant.apache.org/manual/Tasks/jar.html under the "Merging archives" section.
Use Maven and the shade plugin. Have a look at http://maven.apache.org/plugins/maven-shade-plugin/
I would suggest to unjar all your jar files into a lib folder and then jar together with your your source code, try it might work for you.
What are the best practices of creating war files (using eclipse) to run on tomcat? tutorials, links, examples are highly appreciated.
You can use Ant to set up, compile, WAR, and deploy your solution.
<target name="default" depends="setup,compile,buildwar,deploy"></target>
You can then execute one click in Eclipse to run that Ant target. Here are examples of each of the steps:
Preconditions
We'll assume that you have your code organized like:
${basedir}/src: Java files, properties, XML config files
${basedir}/web: Your JSP files
${basedir}/web/lib: Any JARs required at runtime
${basedir}/web/META-INF: Your manifest
${basedir}/web/WEB-INF: Your web.xml files
Set up
Define a setup task that creates the distribution directory and copies any artifacts that need to be WARred directly:
<target name="setup">
<mkdir dir="dist" />
<echo>Copying web into dist</echo>
<copydir dest="dist/web" src="web" />
<copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
</target>
Compile
Build your Java files into classes and copy over any non-Java artifacts that reside under src but need to be available at runtime (e.g. properties, XML files, etc.):
<target name="compile">
<delete dir="${dist.dir}/web/WEB-INF/classes" />
<mkdir dir="${dist.dir}/web/WEB-INF/classes" />
<javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
<classpath>
<fileset dir="${basedir}/../web/WEB-INF/lib">
<include name="*" />
</fileset>
</classpath>
</javac>
<copy todir="${dist.dir}/web/WEB-INF/classes">
<fileset dir="src">
<include name="**/*.properties" />
<include name="**/*.xml" />
</fileset>
</copy>
</target>
Build WAR
Create the WAR itself:
<target name="buildwar">
<war basedir="${basedir}/dist/web" destfile="My.war"
webxml="${basedir}/dist/web/WEB-INF/web.xml">
<exclude name="WEB-INF/**" />
<webinf dir="${basedir}/dist/web/WEB-INF/">
<include name="**/*.jar" />
</webinf>
</war>
</target>
Deploy
Finally, you can set up a task to deploy the WAR directly into your Tomcat deploy location:
<target name="deploy">
<copy file="My.war" todir="${tomcat.deploydir}" />
</target>
Click and go!
Once all this is set up, simply launching the default target from Eclipse will compile, WAR, and deploy your solution.
The advantage of this approach is that it will work outside Eclipse as well as within Eclipse and can be used to easily share your deployment strategy (e.g. via source control) with other developers who are also working on your project.
I've always just selected Export from Eclipse. It builds the war file and includes all necessary files. Providing you created the project as a web project that's all you'll need to do. Eclipse makes it very simple to do.
We use Maven (Ant's big brother) for all our java projects, and it has a very nifty WAR plugin. Tutorials and usage can be found there.
It's a lot easier than Ant, fully compatible with Eclipse (use maven eclipse:eclipse to create Eclipse projects) and easy to configure.
Maven's homepage
Maven WAR plugin
Sample Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
<configuration>
<outputDirectory>${project.build.directory}/tmp/</outputDirectory>
<workDirectory>${project.build.directory}/tmp/war/work</workDirectory>
<webappDirectory>${project.build.webappDirectory}</webappDirectory>
<cacheFile>${project.build.directory}/tmp/war/work/webapp-cache.xml</cacheFile>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>png</nonFilteredFileExtension>
<nonFilteredFileExtension>gif</nonFilteredFileExtension>
<nonFilteredFileExtension>jsp</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<webResources>
<resource>
<directory>src/main/webapp/</directory>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</webResources>
<warName>Application</warName>
</configuration>
</plugin>
A war file is simply a jar file with a war extension, but what makes it work is how the contents is actually structured.
The J2EE/Java EE tutorial can be a start:
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html
And the Servlet specification contains the gory details:
http://java.sun.com/products/servlet/download.html
If you create a new web project in Eclipse (I am referring to the Java EE version), the structure is created for you and you can also tell it where your Appserver is installed and it will deploy and start the application for you.
Using the "Export->WAR file" option will let you save the war file.
If you are not sure what to do and are starting from scratch then Maven can help get you started.
By following the the below steps you can get a new war project setup perfectly in eclipse.
Download and install Maven
Go the command line run: mvn archetype:generate
Follow the prompted steps - choosing the simple java web project (18) and a suitable name.
When it is finished run: mvn eclipse:eclipse
Start Eclipse. Choose File -> Import -> Existing project. Select the directory where you ran the mvn goals.
That's it you should now have a very good start to a war project in eclipse
You can create the war itself by running mvn package or deploy it by setting up a server in eclipse and simply adding adding the project to the server.
As some others have said the downside of using maven is that you have to use the maven conventions. But I think if you are just starting out, learning the conventions is a good idea before you start making your own. There's nothing to stop you changing/refactoring to your own preferred method at a later point.
Hope this helps.
Use the following command outside the WEB-INF folder. This should create your war file and is the quickest method I know.
(You will need JDK 1.7+ installed and environment variables that point to the bin directory of your JDK.)
jar -cvf projectname.war *
Reference Link
Use the Ant war task
Use ant build code
I use this for my project SMS
<property name="WEB-INF" value="${basedir}/WebRoot/WEB-INF" />
<property name="OUT" value="${basedir}/out" />
<property name="WAR_FILE_NAME" value="mywebapplication.war" />
<property name="TEMP" value="${basedir}/temp" />
<target name="help">
<echo>
--------------------------------------------------
compile - Compile
archive - Generate WAR file
--------------------------------------------------
</echo>
</target>
<target name="init">
<delete dir="${WEB-INF}/classes" />
<mkdir dir="${WEB-INF}/classes" />
</target>
<target name="compile" depends="init">
<javac srcdir="${basedir}/src"
destdir="${WEB-INF}/classes"
classpathref="libs">
</javac>
</target>
<target name="archive" depends="compile">
<delete dir="${OUT}" />
<mkdir dir="${OUT}" />
<delete dir="${TEMP}" />
<mkdir dir="${TEMP}" />
<copy todir="${TEMP}" >
<fileset dir="${basedir}/WebRoot">
</fileset>
</copy>
<move file="${TEMP}/log4j.properties"
todir="${TEMP}/WEB-INF/classes" />
<war destfile="${OUT}/${WAR_FILE_NAME}"
basedir="${TEMP}"
compress="true"
webxml="${TEMP}/WEB-INF/web.xml" />
<delete dir="${TEMP}" />
</target>
<path id="libs">
<fileset includes="*.jar" dir="${WEB-INF}/lib" />
</path>
Another option would be to build it automatically using Eclipse. Of course if you have continuous integration environment Ant or Maven is recommended. The export alternative is not very convenient because you have to configure every time the export properties.
STEPS:
Enable "Project Archives" support; this might depend on your project (I used it on Java EE/Web project). Right-click project root directory; Configure -> Add Project Archives Support.
Go and create a new archive in the "Project Archives" top dir. You have only jar option, but name you archive *.war.
Configure Fileset-s, i.e what files to be included. Typical is to configure two filesets similar how the Web Deployment Assembly (project property) is configured.
copy /WebContent to /
copy /build/classes to WEB-INF/classes (create this fileset after you define the WEB-INF/classes directory in the archive)
You might need to tweek the fileset exclude property depending where you placed some of the config files or you might need more filesets, but the idea is that once you configured this you don't need to change it.
Build the archive manually or publish directly to server; but is also automatically built for you by Eclipse
Another common option is gradle.
http://www.gradle.org/docs/current/userguide/application_plugin.html
To build your war file in a web app:
In build.gradle, add:
apply plugin: 'war'
Then:
./gradlew war
Use the layout from accepted answer above.
Simpler solution which also refreshes the Eclipse workspace:
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="default">
<target name="default">
<war destfile="target/MyApplication.war" webxml="web/WEB-INF/web.xml">
<fileset dir="src/main/java" />
<fileset dir="web/WEB-INF/views" />
<lib dir="web/WEB-INF/lib"/>
<classes dir="target/classes" />
</war>
<eclipse.refreshLocal resource="MyApplication/target" depth="infinite"/>
</target>
</project>
Simplistic Shell code for creating WAR files from a standard Eclipse dynamic Web Project. Uses RAM File system (/dev/shm) on a Linux platform.
#!/bin/sh
UTILITY=$(basename $0)
if [ -z "$1" ] ; then
echo "usage: $UTILITY [-s] <web-app-directory>..."
echo " -s ..... With source"
exit 1
fi
if [ "$1" == "-s" ] ; then
WITH_SOURCE=1
shift
fi
while [ ! -z "$1" ] ; do
WEB_APP_DIR=$1
shift
if [ ! -d $WEB_APP_DIR ] ; then
echo "\"$WEB_APP_DIR\" is not a directory"
continue
fi
if [ ! -d $WEB_APP_DIR/WebContent ] ; then
echo "\"$WEB_APP_DIR\" is not a Web Application directory"
continue
fi
TMP_DIR=/dev/shm/${WEB_APP_DIR}.$$.tmp
WAR_FILE=/dev/shm/${WEB_APP_DIR}.war
mkdir $TMP_DIR
pushd $WEB_APP_DIR > /dev/null
cp -r WebContent/* $TMP_DIR
cp -r build/* $TMP_DIR/WEB-INF
[ ! -z "$WITH_SOURCE" ] && cp -r src/* $TMP_DIR/WEB-INF/classes
cd $TMP_DIR > /dev/null
[ -e $WAR_FILE ] && rm -f $WAR_FILE
jar cf $WAR_FILE .
ls -lsF $WAR_FILE
popd > /dev/null
rm -rf $TMP_DIR
done
**Making War file in Eclips Gaynemed of grails web project **
1.Import project:
2.Change the datasource.groovy file
Like this: url="jdbc:postgresql://18.247.120.101:8432/PGMS"
2.chnge AppConfig.xml
3.kill the Java from Task Manager:
run clean comand in eclips
run 'prod war' fowllowed by project name.
Check the log file and find the same .war file in directory of workbench with same date.