having several directories for libs in ant - ant

How can I show Ant that libraries exist in two different directories?
Right now I have
<property name="lib" value="C:/apache-ant-1.8.4/lib" />
<target name="compile" depends="init">
<javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" encoding="UTF-8" >
<classpath>
<pathelement path="${bin}">
</pathelement>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
It's fine if I copy all the libs to the ant \lib folder. How can I show Ant that there are two directories with libraries?

Just add another fileset:
<property name="lib" value="C:/apache-ant-1.8.4/lib" />
<property name="otherLib" value="C:/somePath/lib" />
<target name="compile" depends="init">
<javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" encoding="UTF-8" >
<classpath>
<pathelement path="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${otherLib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>

Related

build.xml:43 compile failed see compiler error output for details

I am trying to compile and build the source files into a jar file using ant from the command line. (windows 7)
However I am getting the following error:
....\build.xml:43: Compile failed see the copiler error output for details
My build file is given below:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="jar.client">
<!--Auto generated ant build file-->
<property environment="env"/>
<property name="axis2.home" value="${env.AXIS2_HOME}"/>
<property name="project.base.dir" value="."/>
<property name="maven.class.path" value=""/>
<property name="name" value="addtaxihire_14cabs"/>
<property name="src" value="${project.base.dir}/src"/>
<property name="test" value="${project.base.dir}/test"/>
<property name="build" value="${project.base.dir}/build"/>
<property name="classes" value="${build}/classes"/>
<property name="lib" value="${build}/lib"/>
<property name="resources" value="${project.base.dir}/resources"/>
<property value="" name="jars.ok"/>
<path id="axis2.class.path">
<pathelement path="${java.class.path}"/>
<pathelement path="${maven.class.path}"/>
<fileset dir="${axis2.home}">
<include name="lib/*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${build}"/>
<mkdir dir="${classes}"/>
<mkdir dir="${lib}"/>
</target>
<target depends="init" name="pre.compile.test">
<!--Test the classpath for the availability of necesary classes-->
<available classpathref="axis2.class.path" property="stax.available" classname="javax.xml.stream.XMLStreamReader"/>
<available classpathref="axis2.class.path" property="axis2.available" classname="org.apache.axis2.engine.AxisEngine"/>
<condition property="jars.ok">
<and>
<isset property="stax.available"/>
<isset property="axis2.available"/>
</and>
</condition>
<!--Print out the availabilities-->
<echo message="Stax Availability= ${stax.available}"/>
<echo message="Axis2 Availability= ${axis2.available}"/>
</target>
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="compile.src" name="compile.test" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}">
<src path="${test}"/>
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="pre.compile.test" name="echo.classpath.problem" unless="jars.ok">
<echo message="The class path is not set right! Please make sure the following classes are in the classpath 1. XmlBeans 2. Stax 3. Axis2 "/>
</target>
<target depends="jar.server, jar.client" name="jar.all"/>
<target depends="compile.src,echo.classpath.problem" name="jar.server" if="jars.ok">
<copy toDir="${classes}/META-INF" failonerror="false">
<fileset dir="${resources}">
<include name="*.xml"/>
<include name="*.wsdl"/>
<include name="*.xsd"/>
</fileset>
</copy>
<jar destfile="${lib}/${name}.aar">
<fileset excludes="**/Test.class" dir="${classes}"/>
</jar>
</target>
<target if="jars.ok" name="jar.client" depends="compile.src">
<jar destfile="${lib}/${name}-test-client.jar">
<fileset dir="${classes}">
<exclude name="**/META-INF/*.*"/>
<exclude name="**/lib/*.*"/>
<exclude name="**/*MessageReceiver.class"/>
<exclude name="**/*Skeleton.class"/>
</fileset>
</jar>
</target>
<target if="jars.ok" depends="jar.server" name="make.repo">
<mkdir dir="${build}/repo/"/>
<mkdir dir="${build}/repo/services"/>
<copy file="${build}/lib/${name}.aar" toDir="${build}/repo/services/"/>
</target>
<target if="jars.ok" depends="make.repo" name="start.server">
<java fork="true" classname="org.apache.axis2.transport.http.SimpleHTTPServer">
<arg value="${build}/repo"/>
<classpath refid="axis2.class.path"/>
</java>
</target>
<target if="jars.ok" depends="compile.test" name="run.test">
<path id="test.class.path">
<pathelement location="${lib}/${name}-test-client.jar"/>
<path refid="axis2.class.path"/>
<pathelement location="${classes}"/>
</path>
<mkdir dir="${build}/test-reports/"/>
<junit haltonfailure="yes" printsummary="yes">
<classpath refid="test.class.path"/>
<formatter type="plain"/>
<batchtest fork="yes" toDir="${build}/test-reports/">
<fileset dir="${test}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
</project>
and probably the 43rd-45th lines of the build file are:
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
Thanks in advance for any solution/suggestion.
The problem is not in the build.xml. It's in your java code. This implies the message:
Compile failed see the compiler error output for details

Creating Ant classpath out of project names

In an ant build script I have a list of projects we are depending on. I need to create a classpath for compilation.
I have:
included.projects=ProjectA, ProjectB
and I need:
included.project.classpath=../ProjectA/bin, ../ProjectB/bin
current code:
<echo message="${included.projects}" />
<pathconvert property="included.projects.classpath" dirsep="," >
<map from="" to="../"/>
<path location="${included.projects}"/>
</pathconvert>
<echo message="${included.projects.classpath}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath>
<pathelement path="${classpath}" />
<dirset includes="${included.projects.classpath}" />
</classpath>
</javac>
I've tried it with explicit declaration too, but didn't work:
<path id="modules.classpath">
<fileset dir="../ModuleA/bin" />
<fileset dir="../ModuleB/bin"/>
</path>
<path id="libraries.classpath">
<fileset dir="lib" includes="*.jar"/>
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
I'm curious, what is the problem with explicit declaration code, and is it possible to solve with the comma-separated-string to classpath solution.
I think it would be simpler to explicity declare the classpath at the top of your build as follows:
<path id="compile.path">
<fileset dir="../ProjectA/bin" includes="*.jar"/>
<fileset dir="../ProjectB/bin" includes="*.jar"/>
</path>
Used as follows:
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath>
<path refid="compile.path"/>
<pathelement path="${classpath}" />
</classpath>
</javac>
Note:
I read your question again and just realised that you're not using jar files built by the other projects, are you? .... Not a great idea....

HTML Report for JUNIT by ANT

How can I generate HTML reports from JUnit using Ant when there are test failures?
The reports are generated when there are no failures.
Also, how can we define our own XSLT for the report generation?
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Example" default="all" basedir=".">
<property name="project_name" value="junitSamples" />
<property name="src" location="src" />
<property name="build" location="build/classes" />
<property name="lib" location="lib" />
<property name="reports" location="reports" />
<target name="init" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${lib}" />
<mkdir dir="${reports}" />
<mkdir dir="${reports}/raw/" />
<mkdir dir="${reports}/html/" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" description="compile the source code ">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="clean">
<delete dir="build" />
<delete dir="${reports}" />
</target>
<target name="run-tests" depends="compile">
<junit printsummary="yes" haltonfailure="yes" showoutput="yes">
<classpath>
<pathelement path="${build}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml" />
<fileset dir="${src}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${reports}\html\" />
</junitreport>
</target>
<target name="all" depends="clean, test" />
</project>
To specify your own stylesheets, use the "styledir" attribute:
<report styledir="${resources}/junit" format="..." todir="..." />
As noted, you must use the "junit-noframes.xsl" stylesheet name.
JUnit report docs.
You set haltonfailure="yes" it means that if one test fails build operation will stop.
This is from the ant documentation:
"Stop the build process if a test fails (errors are considered failures as well)."
Read more here- https://ant.apache.org/manual/Tasks/junit.html

Ant is not recognizing Log4j.properties file

I have been struck in this issue for past two days.Please help me in this. I am running my JUNIT scripts by using ANT. Reports are being generated, but ANT is not able to locate my log4j.properites file. When I am running through eclipse, logs are being generated. My problem here is I want logs when I am running through ANT.DO I need to set any properties.
What is the mistake I am doing?
Please help me.
My Log:
#Application Logs
#log4j.logger.devpinoyLogger
log4j.rootLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=/Users/Application.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false
build.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>
<project name="Module_Junit_Ant" default="usage" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="${ws.home}/jars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="test.reportsDir" value="${ws.home}/reports"/>
<path id="testcase.path">
<pathelement location="${test.dest}"/>
<fileset dir="${ws.jars}">
<include name="*.jar"/>
</fileset>
</path>
<!--target name="start-selenium-server">
<java jar="${ws.home}/lib/selenium-server.jar"/>
</target-->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="*.jar"/>
<fileset dir="${test.src}" includes="*.properties"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath------: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.5"
classpath="${test.classpath}"
>
</javac>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="run" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.reportsDir}" includes="**/*"/>
</delete>
<java jar="${ws.jars}" fork="true" spawn="true" />
<junit fork="yes" haltonfailure="no" printsummary="yes">
<classpath refid="testcase.path" />
<!-- <classpath ="${test.classpath}"/> -->
<batchtest todir="${test.reportsDir}" fork="true">
<fileset dir="${test.dest}">
<include name="LogTest.class" />
<!--include name="tests/suite1/FirstSuiteRunner.class" />
<include name="tests/suite1/FirstSuiteRunner.class" /-->
</fileset>
</batchtest>
<formatter type="xml" />
<classpath refid="testcase.path" />
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reportsDir}" />
</junitreport>
</target>
<target name="email" >
<java classname="util.SendMail" classpath="${test.dest}" classpathref="testcase.path" />
</target>
</project>
Where are your log4j.properties located ?
What works for me it to put a
< pathelement location="x/y/z"/>
inside the
< path id="testcase.path">
which points to a folder which contains a log4j.properties!
So in this example your log4j.properties should be inside the 'z' folder!

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>

Resources