Ant war and ear is not generating - ant

I am new to this ant I wrote athe following build.xml file for generating war and ear. it is showing build successfull. but it not generating any war/ear file. I mentioned my script below. please help me what changes i have to do..
Thanks in advance....
<property name="src" value="src"/>
<property name="dst" value="web"/>
<property name="classes" value="WEB-INF/classes"/>
<property name="archiveName" value="medcardets"/>
<property name="archive" value="BuildArchive"/>
<fileset id="lib" dir="${dst}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset id="war.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="ear.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="lib.rules" dir="${dst}/WEB-INF/lib/rules">
<include name="*.jar"/>
</fileset>
<fileset id="lib.j2ee" dir="j2ee">
<include name="*.jar"/>
</fileset>
<target name="clear">
<delete dir="${dst}/${classes}"/>
<delete dir="${archive}"/>
</target>
<target name="build" depends="clear">
<mkdir dir="${dst}/${classes}"/>
<javac srcdir="${src}" destdir="${dst}/${classes}" debug="on" debuglevel="lines,vars,source">
<classpath>
<fileset refid="lib"/>
<fileset refid="lib.rules"/>
<fileset refid="lib.j2ee"/>
</classpath>
</javac>
<copy todir="${dst}/${classes}">
<fileset dir="${src}">
<exclude name="**/CVS"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>

Your question implies that you have written code that you expect to generate a WAR, but you have no instruction to do so. I see from your comment you are actually asking what the command should be.
Most important: I hope we are all new to things, after 35 years in the industry I learn something every day. Why did you ask the question rather than google? Learning to search is the most important skill. Google ant task WAR gives this task with an example:
<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>
and a similar entry for EAR:
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war"/>
</ear>

Related

How exclude in fileset checking if property is set?

I need to create a fileset but I need to exclude a folder if some property is set (project.name).
<fileset dir="${build.folder}">
<include name="**/*"/>
<exclude name="${project.name}/**/*"/>
</fileset>
How can I do it?
Assuming you are using a recent version of ANT (1.9.1+), try this:
<project name="tryit" xmlns:if="ant:if" xmlns:unless="ant:unless">
<target name="try">
<fileset id="my-fileset" dir="${build.folder}">
<include name="**/*"/>
<exclude if:set="project.name" name="${project.name}/**/*"/>
</fileset>
<echo message="My fileset is ${toString:my-fileset}" />
</target>
</project>

how to reuse the definition of a number of filesets?

I have a number of tasks in my build.xml that all utilize the same set of filesets. E.g. something like the following (I've retain the actual names of the tasks - related to the cobertura coverage tool - but the nature of the enveloping task is immaterial to this question).
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="summary-coverage-report">
<cobertura:cobertura-report destdir="${coverage.summaryxml.dir}" format="summaryXml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="alternate-coverage-report">
<cobertura:cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
What I would like to do is to be able to define once this recurring set of filesets and reuse it. Now, there's a related SO question on how to use the same fileset in multiple places but that's not working here as I have a number of filesets, not just one. I also tried wrapping the filesets inside a macrodef and expanding the macrodef when needed but I get the message that the these tasks "don't support the nested [name of macrodef] element". So I guess macrodefs in Ant can only be used as high-level tasks and cannot be expanded in arbitrary places.
So is there a way to reuse a definition of a number of filesets in Ant?
This would be easy if the cobertura tasks accepted arbitrary resource collections instead of being hard-coded to just fileset - it may be worth submitting a feature request to this end. For example with a copy task:
<resources id="filesets">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</resources>
<copy todir="dest">
<resources refid="filesets"/>
</copy>
You can fall back to the purely XML-level and use an entity reference:
<!DOCTYPE project [
<!ENTITY cobFilesets '<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>'>
]>
<project ....>
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
&cobFilesets;
</cobertura:cobertura-report>
</target>
</project>

ant unable to make a war, it stops while compiling classes

I have a build.xml file from which i was building war for three months. but currently it stops when it reaches at compling the code. my version of ant is 1.8. Last time i made war was on last friday evening and it was working fine that time.
here is my build.xml file
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Written By Waqas Sadiq on 14062011 -->
<project name="smi-core" default="all" basedir="..">
<property name="name" value="EchelonSurvey"/>
<property name="common.deploy" value="../deploy"/>
<property name="home.dir" value="./WebRoot"/>
<property name="src.dir" value="./src"/>
<property name="oi.dir" value="./openInviter"/>
<property name="as.dir" value="./about-us"/>
<property name="rr.dir" value="./ResponsesReport"/>
<property name="lib.dir" value="./WebRoot/WEB-INF/lib"/>
<property name="images.dir" value="./WebRoot/images"/>
<property name="tabcontent.dir" value="./WebRoot/tabcontent"/>
<property name="css.dir" value="./WebRoot/css"/>
<property name="js.dir" value="./WebRoot/js"/>
<property name="jsp.dir" value="./WebRoot/jsp"/>
<property name="config.web" value="./WebRoot/WEB-INF"/>
<property name="config.web.class" value="./WebRoot/WEB-INF/classes"/>
<property name="build.dir" value="build/buildoutput"/>
<property name="build.lib" value="${build.dir}/lib"/>
<property name="build.dest" value="${build.dir}/classes"/>
<property name="build.war.dir" value="${build.dir}/EchelonSurvey.war"/>
<property name="sampleapp.name" value="EchelonSurvey"/>
<property name="build.ship.dir" value="build/ship"/>
<property name="build.deploy.dir" value="./deploy"/>
<property name="option.debug" value="on"/>
<property name="option.optimize" value="off"/>
<property name="option.deprecation" value="on"/>
<property name="version" value="1.0"/>
<property name="vendor.name" value="Stafona"/>
<property name="deploy.dir" value="./deploy"/>
<target name="eclipse" if="eclipse.running">
<property name="build.compiler"
value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
</target>
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${build.lib}"/>
<mkdir dir="${build.dest}"/>
</target>
<target name="compile" depends="prepare">
<delete dir="${config.web.class}"/>
<mkdir dir ="${config.web.class}"/>
<copy todir="${config.web.class}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
<javac
destdir="${config.web.class}"
debug="${option.debug}"
optimize="${option.optimize}"
deprecation="${option.deprecation}"
includeantruntime="false"
>
<src path="${src.dir}"/>
<classpath refid="classpath"/>
</javac>
<javac
destdir="${build.dest}"
debug="${option.debug}"
optimize="${option.optimize}"
deprecation="${option.deprecation}"
>
<src path="${src.dir}"/>
<classpath refid="classpath"/>
</javac>
</target>
<target name="package-EchelonSurvey"
depends="compile" description="Generates the EchelonSurvey implementation JAR">
<jar destfile="${build.lib}/${name}.jar">
<fileset dir="${build.dest}">
<include name="**/**.class"/>
</fileset>
<manifest>
<attribute name="Built-By" value="${vendor.name}"/>
</manifest>
</jar>
</target>
<target name ="package-web-war" depends ="package-EchelonSurvey" description ="Generates war package">
<mkdir dir ="${build.war.dir}"/>
<mkdir dir ="${build.war.dir}/WEB-INF/classes"/>
<mkdir dir ="${build.war.dir}/WEB-INF/lib"/>
<mkdir dir ="${build.war.dir}/js"/>
<mkdir dir ="${build.war.dir}/jsp"/>
<mkdir dir ="${build.war.dir}/images"/>
<mkdir dir ="${build.war.dir}/css"/>
<copy todir="${build.war.dir}/WEB-INF">
<fileset dir="${config.web}">
<include name="*.xml"/>
<include name="*.log"/>
<include name="*.properties"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/images">
<fileset dir="${images.dir}">
<include name="**/**.*"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/tabcontent">
<fileset dir="${tabcontent.dir}">
<include name="*.*"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/css">
<fileset dir="${css.dir}">
<include name="*.*"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/js">
<fileset dir="${js.dir}">
<include name="*.*"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/jsp">
<fileset dir="${jsp.dir}">
<include name="**/**.jsp"/>
<include name="*.html"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/WEB-INF/classes">
<fileset dir="${config.web.class}">
<include name="**/**.class"/>
<include name="**/**.xml"/>
<include name="**/**.properties"/>
</fileset>
</copy>
<copy todir="${build.war.dir}/WEB-INF/lib">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
<exclude name="EchelonSurvey.jar"/>
</fileset>
</copy>
<copy todir="${build.war.dir}">
<fileset dir="${home.dir}">
<include name="*.jsp"/>
<include name="*.properties"/>
<include name="*.html"/>
<include name="*.xml"/>
<include name="*.txt"/>
<include name="*.do"/>
</fileset>
</copy>
<jar jarfile ="${build.lib}/${sampleapp.name}.war" basedir ="${build.war.dir}">
<manifest>
<attribute name="Built-By" value="${vendor.name}"/>
</manifest>
</jar>
<copy todir="${deploy.dir}">
<fileset dir="${build.lib}">
<include name="*.war"/>
<include name="*.jar"/>
</fileset>
</copy>
<copy todir="${common.deploy}">
<fileset dir="${build.lib}">
<include name="*.war"/>
<include name="*.jar"/>
</fileset>
</copy>
</target>
<target name="all" depends="package-web-war">
</target>
<target name="ship" depends="all" description="* Generates a .zip file">
</target>
<target name="cleanagain">
<delete dir="${build.dir}" />
</target>
</project>
and here is the output from this. I am running this on Ubunto with java 6 installed on it.
Buildfile: /var/tempproject/suleman/EchlonSurvey/build/build.xml
clean:
[delete] Deleting directory /var/tempproject/suleman/EchlonSurvey/build/buildoutput
prepare:
[mkdir] Created dir: /var/tempproject/suleman/EchlonSurvey/build/buildoutput/lib
[mkdir] Created dir: /var/tempproject/suleman/EchlonSurvey/build/buildoutput/classes
compile:
[delete] Deleting directory /var/tempproject/suleman/EchlonSurvey/WebRoot/WEB-INF/classes
[mkdir] Created dir: /var/tempproject/suleman/EchlonSurvey/WebRoot/WEB-INF/classes
[copy] Copying 39 files to /var/tempproject/suleman/EchlonSurvey/WebRoot/WEB-INF/classes
[copy] Copied 13 empty directories to 9 empty directories under /var/tempproject/suleman/EchlonSurvey/WebRoot/WEB-INF/classes
[javac] Compiling 225 source files to /var/tempproject/suleman/EchlonSurvey/WebRoot/WEB-INF/classes
Killed

Ant for EJB project

I am trying to make an ant script for an EJB project. I am using Jboss for this.
I am new to both EJB and Ant and am having problems in getting the beans to compile from ant. It gives me number of errors of the kind
package javax.persistence does not exist
#MappedSuperclass - Cannot find symbol
I created it as an eclipse project initially, and added the jboss runtime through eclipse. Do I need to copy all the jars in a lib folder and include them in the classpath for the beans to compile or is there a better way to do this?
Try setting classpath in Ant appropriately. It ignores any system CLASSPATH or Eclipse setting that you have.
Here's an Ant build.xml that you can start with:
<?xml version="1.0" encoding="UTF-8"?>
<project name="spring-finance" basedir="." default="package">
<property name="version" value="1.6"/>
<property name="haltonfailure" value="no"/>
<property name="out" value="out"/>
<property name="production.src" value="src/main/java"/>
<property name="production.lib" value="src/main/webapp/WEB-INF/lib"/>
<property name="production.resources" value="src/main/resources"/>
<property name="production.classes" value="${out}/production/${ant.project.name}"/>
<property name="test.src" value="src/test/java"/>
<property name="test.lib" value="src/test/lib"/>
<property name="test.resources" value="src/test/resources"/>
<property name="test.classes" value="${out}/test/${ant.project.name}"/>
<property name="exploded" value="out/exploded/${ant.project.name}"/>
<property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
<property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>
<property name="reports.out" value="${out}/reports"/>
<property name="junit.out" value="${reports.out}/junit"/>
<property name="web.src" value="src/main/webapp"/>
<property name="web.lib" value="${web.src}/WEB-INF/lib"/>
<property name="web.classes" value="${web.src}/WEB-INF/classes"/>
<path id="production.class.path">
<pathelement location="${production.classes}"/>
<pathelement location="${production.resources}"/>
<fileset dir="${production.lib}">
<include name="**/*.jar"/>
<exclude name="**/junit*.jar"/>
<exclude name="**/*test*.jar"/>
</fileset>
</path>
<path id="test.class.path">
<path refid="production.class.path"/>
<pathelement location="${test.classes}"/>
<pathelement location="${test.resources}"/>
<fileset dir="${test.lib}">
<include name="**/junit*.jar"/>
<include name="**/*test*.jar"/>
</fileset>
</path>
<available file="${out}" property="outputExists"/>
<target name="clean" description="remove all generated artifacts" if="outputExists">
<delete dir="${out}" includeEmptyDirs="true"/>
</target>
<target name="create" description="create the output directories" unless="outputExists">
<mkdir dir="${production.classes}"/>
<mkdir dir="${test.classes}"/>
<mkdir dir="${junit.out}"/>
<mkdir dir="${exploded.classes}"/>
<mkdir dir="${exploded.lib}"/>
</target>
<target name="compile" description="compile all .java source files" depends="create">
<!-- Debug output
<property name="production.class.path" refid="production.class.path"/>
<echo message="${production.class.path}"/>
-->
<javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
<classpath refid="production.class.path"/>
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</javac>
<javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
<classpath refid="test.class.path"/>
<include name="**/*Test.java"/>
</javac>
</target>
<target name="test" description="run all unit tests" depends="compile">
<!-- Debug output
<property name="test.class.path" refid="test.class.path"/>
<echo message="${test.class.path}"/>
-->
<junit printsummary="yes" haltonfailure="${haltonfailure}">
<classpath refid="test.class.path"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${junit.out}">
<fileset dir="${test.src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${junit.out}">
<fileset dir="${junit.out}">
<include name="TEST-*.xml"/>
</fileset>
<report todir="${junit.out}" format="frames"/>
</junitreport>
</target>
<target name="exploded" description="create exploded deployment" depends="test">
<copy todir="${exploded}">
<fileset dir="${web.src}"/>
</copy>
<copy todir="${exploded}/WEB-INF">
<fileset dir="${web.src}/WEB-INF"/>
</copy>
<copy todir="${exploded.classes}">
<fileset dir="${production.classes}"/>
</copy>
<copy todir="${exploded.lib}">
<fileset dir="${production.lib}"/>
</copy>
</target>
<target name="jar" description="create jar file" depends="test">
<jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
</target>
<target name="war" description="create war file" depends="exploded">
<war basedir="${exploded}" webxml="${exploded}/WEB-INF/web.xml" destfile="${out}/${ant.project.name}.war"/>
</target>
<target name="package" description="create package for deployment" depends="test">
<antcall target="war"/>
</target>
</project>

Generate manifest class-path from <classpath> in Ant

In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path
There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.
Edit:
It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.
<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<target name="prepare">
<mkdir dir="build"/>
</target>
<target name="compile" depends="prepare" description="Compile core sources">
<javac srcdir="src"
includes="**"
destdir="build"
debug="true"
source="1.5">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="jar" depends="compile" description="Generates executable jar file">
<jar jarfile="higgins.jar">
<manifest>
<attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
<attribute name="Class-Path" value="${jar.class.path}"/>
</manifest>
<fileset dir="build" includes="**/*.class"/>
<fileset dir="src" includes="**/*.properties"/>
</jar>
</target>
</project>
<path id="build.classpath">
<fileset dir="${basedir}">
<include name="lib/*.jar"/>
</fileset>
</path>
<pathconvert property="manifest.classpath" pathsep=" ">
<path refid="build.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
<target depends="compile" name="buildjar">
<jar jarfile="${basedir}/${test.jar}">
<fileset dir="${build}" />
<manifest>
<attribute name="Main-Class" value="com.mycompany.TestMain"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
For further information check out this article.
Assuming Ant 1.7 or above, you can use the manifestclasspath task.
<path id="dep.runtime">
<fileset dir="./lib">
<include name="**/*.jar" />
</fileset>
</path>
<property name="dep_cp" value="${toString:dep.runtime}" />
<target name="default">
<manifestclasspath property="manifest_cp" jarfile="myjar.jar">
<classpath refid="dep.runtime" />
</manifestclasspath>
<echo message="Build Classpath: ${dep_cp}" />
<echo message="Manifest Classpath: ${manifest_cp}" />
</target>
If you just want a common subpath shared between two (or more) paths, that is easy to do:
<path id="lib.path>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<path id="project.class.path">
<pathelement location="build"/>
<path refid="lib.path"/>
</path>
<property name="jar.class.path" refid="lib.path"/>
EDIT Sorry, I misunderstood the question. Try this:
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="." includes="${jar.class.path}"/>
</path>
You can use <pathconvert> to convert a path (which can contain a fileset) into a plain string. You'll likely need to <echo> that string to a file, use either <replace> or <replaceregexp> to chop the leading path bits, then finally use <loadfile> to load the manipulated string into the final property.
Implementation left as an exercise to the reader.

Resources