How do I specify the files to include in a WAR file? - ant

I need to create a WAR file that just contains static content files (gifs, jpgs, html, js, etc). I have a directory structure that contains all the files and need to create the WAR file via an ANT (1.5.1) build task. Right now I just have this in the ANT task:
<war destfile="${output.file}" webxml="WEB-INF/web.xml" basedir="${basedir}">
<fileset dir="${basedir}/sales" />
</war>
The files I want to include are in C:/basedir/sales and its subdirectories. When I try to run this task I get "A zip file cannot include itself". So clearly putting that fileset in there is not the right way to do it. I am unclear as to what I need to put in the task and in the web.xml file to specify what files to include within the archive.

I think the basedir="${basedir}" is causing you the problems. Also, I think the way you have it written will require that web.xml exist inside WEB-INF dir relative to where you run ant from.
So, try creating /WEB-INF/web.xml as follows:
<?xml version="1.0" encoding="utf-8" ?>
<web-app>
</web-app>
Then try updating /build.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project name="yourproject" basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="war" description="--> build war file">
<war destfile="./mywar.war" webxml="WEB-INF/web.xml">
<fileset dir="C:/basedir/sales" />
</war>
</target>
</project>
Then you should be able to run "ant war" from command line and it should create "mywar.war" in your current directory.

Related

is there a way to make a jar with the Eclipse generated ant script?

To my surprise the build.xml file generated by Eclipse (Neon) for Java has no element containing an invocation of a jar task. As often is the case with code generation I think you have to use it and make no edits so that you can regenerate - or - avoid code generation completely. A comment in the generated file suggests it might be possible to avoid edits by extending the capabilities by importing.
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
I thought I would be able to use the <?eclipse.ant.import?> element in an second file called export.xml. In ant scripting there is supposed to be one project per buildfile so now there is a second project with a dependency on a target in the first project.
Regenerating build.xml reveals that it contains an "import" as expected.
<import file="export.xml"/>
Unfortunately this does not work. Running ant, which I do from the command line, just seems to result in the export/jar project being ignored.
The generated script with the import element (nested on the 7th line)...
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="ohana1">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<import file="export.xml"/>
<path id="ohana1.classpath">
<pathelement location="bin"/>
<pathelement location="../export/ohana1/commons-collections-3.2.1.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="ohana1.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
The export.xml file meant to make a jar...
<?eclipse.ant.import?>
<project basedir="." default="export" name="ohana1Export">
<target depends="build,make-jar" name="export"/>
<target name="make-jar">
<jar destfile="../export/ohana1/${ant.project.name}.jar" basedir="bin"/>
</target>
</project>
Note that the Eclipse Ant editor complains about this export.xml file because the target named build, which is a dependency, does not exist in this project/buildfile. The build target is in the generated build.xml. That error might be coming from a "dumb" editor so I went ahead to do a run of ant. Invoking ant from the command line I find that there is no jar file made.
Should I conclude that Eclipse's ant script generator is useless if you need to export a .jar file and that a human should maintain the ant script that meets all the requirements?
Yes, in my opinion the exported build.xml is useless, as of Eclipse Neon, if the intention is to make a .jar.
Specifically do the following.
Manually write the trivial ant script that exports a .jar. The link at the bottom of this post has verbatim text on what the script might look like. You can use the built-in Xml Editor via New > Other > XML > XML File to create this new file which might be called makeJar.xml and save it. If the icon shown in the Package Explorer is still a plain XML file icon refreshing the project may change the icon to an Ant file icon. In the future, you can use Open With to get the Ant Editor instead of the XML Editor. This script will replace the manual exporting of a .jar that the user would otherwise perform via Eclipse.
This script can be added to Project > Properties > Builders. It would be placed second in the list of Builders. First in the list of Builders is the Java Builder which should already exist. When an Eclipse build is invoked the entire list of Builders will be processed in the order shown in the list of Builders. Thus not only will .class files be generated but also the .jar.
What is achieved is greater automation since the .class generation and .jar generation are now integrated, which arguably was the point of using the exported build.xml in a failed attempt to generate the .jar.
Here is the dialog at Project > Properties > Builders that you can use to create a new Builder. Select New then select Ant Builder. I gave the name makeJar to the new Builder.
Here is the dialog for the new Ant Builder that will allow you to browse to your buildfile which is your manually written Ant script that creates a .jar file. In this example the script is makeJar.xml. It also allows you to browse to the base directory to be used when the script is run.
After setting up the new Builder, a project "clean" or project "build" will create .class files and also the .jar.
Eclipse's documentation on this subject is at the link. Note that it seems impossible to link the exact page that contains the instructions so you have to browse down the documentation tree to the section about "Ant buildfiles as project builders".
Link to Eclipse and Ant

Add non-class files (includes folders and files) to your jar using build.xml

I have a java project (MyProject) with the below mentioned structure
src->package1(read as com.test.Atrribute)->File1.java,File2.java
src->package2(read as com.test.Objects)->obj1.java,obj2.java
src->directory(read as Webcontent.Objects)-> Folder1 -> application.properties file and some more files
Currently the build.xml creates a jar for the above project and copies the class files from package1 and package2.
However, my jar should also include the folder(Webcontent.Objects) with all the content's within it (i.e folders and files).
How can I do this in the build.xml ?
I have never created a build.xml before and pretty much new to all this.
Following is the jar task in the build.xml to include the class file's in the jar.
<target name="MyProject-jar" depends="compile"
description="Jar for the Project">
<jar destfile="${output.dir}/MyProject.jar" basedir="${output.dir}/">
<include name="com/test/Attribute/*.class"/>
<include name="com/test/Objects/*.class"/>
</jar>
</target>
Appreciate if anybody could help.Thanks.
You could add
<include name="Webcontent/Objects/**/*"/>
to your jar task
Note: The ** recursively considers directories under its parent

Liferay SDK portlet plugin: use ant target to modify .war file after being created

In a liferay portlet, the autogenerated build.xml (created with the SDK in eclipse, autogenerated by the wizard) always looks like this
<?xml version="1.0"?>
<project name="my-service-portlet" basedir="." default="deploy">
<import file="../build-common-portlet.xml" />
</project>
Is it advisable to add custom targets to this? I want to modify the .war file after it is created. Like this:
Is there a way with Apache Ant to update a jar file after it's been built?
My war-file is huge because it is autocreated by Liferay's service builder, and it seems to be in need of a few optimizations. I want to remove the WEB-INF/src/.java files (and those WEB-INF/classes/.class files in the .war) that are also in the WEB-INF/lib/.jar. They seem to be duplicated. Can I do this to save space during deployment and to simplify and speed-up the deployment process.
How would such an ant-target look like?
Sure, that's totally possible:
<project name="MyPortlet" basedir="." default="updateWarAndDeploy">
<import file="../build-common-portlet.xml" />
<target name="updateWarAndDeploy">
<antcall target="war"/>
-- modify war file --
<copy file="${plugin.file}" todir="${auto.deploy.dir}" />
</target>
</project>

How can you tell an ant build file to recursively add all files in the bin directory to a jarfile?

I have the following ant build file which is supposed to package all class files in the bin directory into a jarfile:
<?xml version="1.0" encoding="utf-8"?>
<project name="RemoteJunitXletServer.makejar" default="makejar" basedir=".">
<target name="makejar" description="Build a jarfile based on the JunitServer project">
<jar jarfile="JunitServer.jar" includes="**.class" basedir="bin" />
</target>
</project>
Unfortunately, including "**.class" only goes two directories deep, and does not copy any files that are deeper than two directories inside of the bin folder. Do these directories HAVE to be explicitly declared? Or is there a way to tell Ant to just copy all class files inside of the bin folder regardless of location while preserving the folder structure?
Try includes="**/**.class" ...

ant war - putting file parallel to web.xml

I am trying to use spring webservice with ant. spring webservice requires spring-ws-servlet.xml and sample.xsd file to be put in war file parallel to web.xml.
I am trying this war command and it is putting parallele to web-inf folder instead inside of it. Please let me know what should i modify to put it inside web-inf folder.
<copy file="config/ws/spring-ws-servlet.xml" tofile="lps/WEB-INF/spring-ws-servlet.xml" />
<war warfile="dist/ODG.war" webxml="config/web.xml">
<lib dir="lps/WEB-INF/lib" />
<classes dir="lps/WEB-INF/classes" />
<fileset dir="lps/ws"/>
</war>
As the ant manual states, add a webinf element to the war task to add files into the WEB-INF directory.
<war warfile="dist/ODG.war" webxml="config/web.xml">
...
<webinf>
<fileset dir="config/ws" includes="spring-ws-servlet.xml"/>
</webinf>
</war>

Resources