I've got an ant file which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project name="p" default="compile" basedir=".">
<path id="compile.cliClasspath">
<fileset dir="./WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp />
</target>
<target name="compile" description="compile the source" >
<javac srcdir="./src/" destdir="C:\TEMP\build">
<classpath refid="compile.cliClasspath" />
</javac>
</target>
<target name="createWar" depends="compile" description="create web archive">
<war destfile="C:\TEMP\client.war"
webxml="./WebContent/WEB-INF/web.xml"
needxmlfile="true"
basedir="./WebContent"
excludesfile="./WebContent/WEB-INF/application.xml">
<lib dir="./WebContent/WEB-INF/lib" />
<classes dir="C:\TEMP\build" />
</war>
</target>
<target name="createEar" depends="compile, createWar" description="create enterprise archive">
<ear destfile="C:\TEMP\Client.ear"
appxml="./WebContent/WEB-INF/application.xml"
includes="C:\TEMP\Client.war" />
</target>
<target name="cleanUp" depends="compile, createWar, createEar" description="clean up">
<delete includeemptydirs="true">
<fileset dir="C:\TEMP\build" includes="**/*" />
</delete>
</target>
</project>
The idea being to create an ear with the content of my web app. When I run this (run as with order as in the file, from galileo) I get this:
Buildfile: C:\Client-was7.xml
<snip>init:
compile:
[javac] Compiling 47 source files to C:\TEMP\build
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
createEar:
compile:
[javac] Compiling 47 source files to C:\TEMP\build
createWar:
[war] Building war: C:\TEMP\Client.war
createEar:
cleanUp:
BUILD SUCCESSFUL
Total time: 15 seconds
Why isn't the output:
init:
compile:
createWar:
createEar:
cleanUp:
BUILD SUCCESSFUL
Total time: 15 seconds
?
Thanks
I think it may have to do with the depends targets. when you run create ear, you first run compile, and then create war, which also depends on compile and so on.
Related
I have an ant script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntTest" basedir="." default="clean">
<property name="src.dir" value="src"/>
<property name="classes.dir" value="classes"/>
<target name="clean" description="delete all generated files">
<delete dir="${classes.dir}" failonerror="false"/>
<echo message="Hello" />
<delete dir="${ant.project.name}.jar"/>
</target>
<target name="compile" description="compile the task">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" description="create jars of task" depends="compile">
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}" />
</target>
<target name="use" description="use the created jars" depends="jar">
<taskdef name="ntTest" classname="AntTest" classpath="${ant.project.name}.jar" />
<ntTest/>
</target>
</project>
And output is
Buildfile: D:\Work\D3000\AntTest\Build.xml
clean:
[delete] Deleting directory D:\Work\D3000\AntTest\classes
[echo] Hello
compile:
[mkdir] Created dir: D:\Work\D3000\AntTest\classes
[javac] Compiling 1 source file to D:\Work\D3000\AntTest\classes
compile:
jar:
[jar] Building jar: D:\Work\D3000\AntTest\AntTest.jar
compile:
jar:
use:
BUILD FAILED
D:\Work\D3000\AntTest\Build.xml:24: taskdef class AntTest cannot be found
using the classloader AntClassLoader[D:\Work\D3000\AntTest\AntTest.jar]
Total time: 758 milliseconds
Can anybody tell me why this error is coming:
BUILD FAILED D:\Work\D3000\AntTest\Build.xml:24: taskdef class AntTest
cannot be found using the classloader
AntClassLoader[D:\Work\D3000\AntTest\AntTest.jar]
My class file contains class named AntTest
Seems like the classname is wrong, you need the full qualified classname.
instead of :
<taskdef name="ntTest" classname="AntTest" classpath="${ant.project.name}.jar"/>
try :
<taskdef name="ntTest" classname="com.yourdomain.AntTest" classpath="${ant.project.name}.jar"/>
I'm trying to compile a project using ant. I guess the "javac" task was successful as i can see the following message on the console:
[javac] Compiling 151 source file to /var/lib/jenkins/jobs/project1/workspace/build
Here is the complete output of the command "ant compile"
clean:
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/build
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/docs
[delete] Deleting directory /var/lib/jenkins/jobs/project1/workspace/dist
makedir:
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/build
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/docs
[mkdir] Created dir: /var/lib/jenkins/jobs/project1/workspace/dist
compile:
[javac] /var/lib/jenkins/jobs/project1/workspace/build.xml:43: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 151 source file to /var/lib/jenkins/jobs/project1/workspace/build
BUILD SUCCESSFUL
Total time: 1 second
The problem is there is nothing produced in the folder "build" !
I'm using the same build.xml template file in other projects and it works very well, but here i can't understand why i can't find the compiled sources in the build folder.
Please help.
Here is my build.xml file:
<?xml version="1.0"?>
<project name="project1" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="lib.dir" location="WebContent/WEB-INF/lib" />
<property name="server.common.lib.dir" location="/home/ghali/jboss-5.1.0.GA/common/lib" />
<property name="server.lib.dir" location="/home/ghali/jboss-5.1.0.GA/lib" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${server.common.lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${server.lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac destdir="${build.dir}" classpathref="build.classpath" debug="true">
<src path="${src.dir}" />
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="package" depends="compile">
<war destfile="${dist.dir}/project1.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent" />
<lib dir="WebContent/WEB-INF/lib" />
<classes dir="${build.dir}" />
</war>
</target>
<target name="main" depends="compile, package, docs">
<description>Main target</description>
</target>
</project>
I am trying to generate the Code coverage report from jacoco.exec file using ant.
My ant build is:
<?xml version="1.0"?>
<project xmlns:jacoco="antlib:org.jacoco.ant" name="Example Ant Build with JaCoCo" default="rebuild">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report can be itegrated into an existing build in three simple steps.
</description>
<property name="src.dir" location="./java"/>
<property name="result.dir" location="./target"/>
<property name="result.classes.dir" location="./classes"/>
<property name="result.report.dir" location="${result.dir}/site/jacoco"/>
<property name="result.exec.file" location="./jacoco.exec"/>
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="./jacocoant.jar"/>
</taskdef>
<target name="compile">
<mkdir dir="${result.classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false"/>
</target>
<target name="test" depends="compile">
<!--
Step 2: Wrap test execution with the JaCoCo coverage task
-->
<jacoco:coverage destfile="${result.exec.file}">
<java classname="org.jacoco.examples.parser.Main" fork="true">
<classpath path="${result.classes.dir}"/>
<arg value="2 * 3 + 4"/>
<arg value="2 + 3 * 4"/>
<arg value="(2 + 3) * 4"/>
<arg value="2 * 2 * 2 * 2"/>
<arg value="1 + 2 + 3 + 4"/>
<arg value="2 * 3 + 2 * 5"/>
</java>
</jacoco:coverage>
</target>
<target name="report" depends="test">
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!--
This task needs the collected execution data and ...
-->
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}"/>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}"/>
<csv destfile="${result.report.dir}/report.csv"/>
<xml destfile="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
<target name="rebuild" depends="compile,test,report"/>
</project>
But while compiling the ant build I get the errors related to undefined symbols in the code. How do I remove them?
An example of the error is:
compile:
[javac] Compiling 13 source files to C:\Documents and Settings\user\Desktop\jacoco\classes
[javac] C:\Documents and Settings\user\Desktop\jacoco\java\file.java:13: error: package org.abc.def.ghi.primitives does not exist
[javac] import org.abc.def.ghi.primitives.Request;
These imports are from my code and internally defined but does ant doesn't recognize them. I have already copied all the .java and .class files.
Any pointers will be much appreciated.
This is not a code coverage problem. Your code is failing at the compile step. It's the javac command throwing the error.You need to solve this problem first.
I don't understand what you mean by:
.... I have already copied all the .java and .class files.
Below is the Ant script i am trying in GAE project but when i look into the war file i see two class for each java file.
Just a fyi, WAR file created i open in winrar but even when i extract it winrar keeps on asking me to either replace existing file message. Not sure how even same file name & extension is present in one folder. Also i checked the "dist" & "classes" folder i does not have duplicate file.
<?xml version="1.0" ?>
<project name="AntExample1" default="war">
<path id="compile.classpath">
<fileset dir="war/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="build/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="dist/AntExample.war" webxml="war/WEB-INF/web.xml">
<fileset dir="war"/>
<lib dir="war/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
<target name="clean">
<delete dir="dist" />
<delete dir="build" />
</target>
</project>
I have a requirement where I need to build multiple jar files from each of the project that exists in my workspace. Can you please let me know how do I write a single build.xml to create multiple jar files ? I have tried similar code and know its possible but would like to know what are the best practices that needs to be followed. In my case, there are 5 projects in my workspace and I would have to create 5 different jars. Also there are external dependency jars, that needs to be included in some projects to build the jar file. Below is the sample code that I have written to create two jar files. The code that I have written to generate the jar file is below
<target name="compile" depends="compileVal, compileHib " description="compile the source for all">
</target>
<!-- =============================================
target: compile the source for Valueobjects
============================================= -->
<target name="compileVal" depends="clean" description="description">
<echo message="Creating directory '${target}' if not present "></echo>
<mkdir dir="${target}"/>
<mkdir dir="${Classfiles}"/>
<mkdir dir="${JarLocation}"/>
<javac srcdir="../ValueObjects/src" destdir="${Classfiles}" />
</target>
<!-- =================================
target: compile
================================= -->
<target name="compileHib" depends="clean" description="description">
<echo message="Creating directory '${target}' if not present "></echo>
<mkdir dir="${target}"/>
<mkdir dir="${Classfiles}"/>
<mkdir dir="${JarLocation}"/>
<javac srcdir="src" destdir="${Classfiles}" >
<classpath>
<pathelement location="${libloc}/hibernate3.jar"/>
<pathelement location="${libloc}/hsqldb.jar"/>
<pathelement location="${libloc}/jta.jar"/>
<pathelement location="${libloc}/ojdbc5.jar"/>
<pathelement location="${libloc}/commons-logging-1.1.1.jar"/>
<pathelement location="${libloc}/postgresql-9.1-902.jdbc4.jar"/>
</classpath>
</javac>
</target>
<!-- =================================
target: name
================================= -->
<target name="copy-non-java-files" depends="" description="description">
<copy todir="${Classfiles}" includeemptydirs="false">
<fileset dir="src" excludes="**/*.java" />
</copy>
</target>
<!-- =================================
target: compress
================================= -->
<target name="build_jar" depends="buildValobj, buildHib" description="description">
</target>
<!-- =================================
target: buildValobject jar
================================= -->
<target name="buildValobj" depends="compileVal" description="creates the valueObject jar">
<echo message="Creating the jar to Folder '${JarDest}'"> </echo>
<jar destfile="${JarDest}/${RetValObName}" basedir="${Classfiles}" />
</target>
<!-- =================================
target: buildHibjar
================================= -->
<target name="buildHib" depends="compileHib, copy-non-java-files" description="description">
<echo message="Creating the jar to Folder '${JarDest}'"> </echo>
<jar destfile="${JarDest}/${RetHibName}" basedir="${Classfiles}" />
</target>
<!-- =================================
target: clean
================================= -->
<target name="clean">
<echo message="Cleaning '${target}' folder"></echo>
<delete includeemptydirs="true">
<fileset dir="${target}" includes="**/*"/>
</delete>
</target>
In the above code, the issue im facing is while trying to compileHib though I have specified the depends="clean". The clean does not happen. A jar is created that includes, both the files that were already existing as well as the new files that were moved.Not sure why? Though I have specified clean, the clean action does not seem to happen. Can you please let me know what im missing in the above.
Please Assist. THanks in Advance