I'm trying to following apache's ANT tutorial:
http://ant.apache.org/manual/index.html
I'm currently at the JUnit portion, but I keep getting errors related to not being able to find the junit.framework package.
I believe I've followed the tutorial exactly as follows - Here are the details:
-----My file structure-----
build.xml
/build
../classes
/lib
..log4j-1.2.8.jar
myManifest
/src
..HelloWorldTest.java
..log4j.properties
../oata
....HelloWorld.java
-----Sources-----
myManifest:
Main-Class: oata.HelloWorld
build.xml:
<project name ="HelloWorld" basedir="." default="main">
<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="jar.dir" value="${build.dir}/jar" />
<property name="lib.dir" value="lib" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<property name="main-class" value="oata.HelloWorld" />
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
<copy todir="${classes.dir}" >
<fileset dir="${src.dir}" excludes="**/*.java" />
</copy>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path id="application" location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="junit" depends="jar">
<junit printsummary="yes">
<classpath>
<path refid="classpath"/>
<path refid="application"/>
</classpath>
<batchtest fork="yes">
<fileset dir="${src.dir}" includes="*Test.java"/>
</batchtest>
</junit>
</target>
<target name="clean-build" depends="clean,jar" />
<target name="main" depends="clean,run" />
</project>
HelloWorldTest.java:
public class HelloWorldTest extends junit.framework.TestCase {
public void testNothing() {
}
public void testWillAlwaysFail() {
fail("An error message");
}
}
HelloWorld.java
package oata;
import org.apache.log4j.Logger;
public class HelloWorld {
static Logger logger = Logger.getLogger(HelloWorld.class);
public static void main(String [] args){
logger.info("Hello World!");
}
}
log4j.properties:
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
---Output---
ant -verbose junit
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
Trying the default build file: build.xml
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml
Detected Java version: 1.6 in: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Detected OS: Mac OS X
parsing buildfile /Users/jtyler/Projects/AntHelloWorld/build.xml with URI = file:/Users/jtyler/Projects/AntHelloWorld/build.xml
Project base dir set to: /Users/jtyler/Projects/AntHelloWorld
parsing buildfile jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
Build sequence for target(s) `junit' is [compile, jar, junit]
Complete build sequence is [compile, jar, junit, clean, run, main, clean-build, ]
compile:
[mkdir] Skipping /Users/jtyler/Projects/AntHelloWorld/build/classes because it already exists.
[javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] HelloWorldTest.java added as HelloWorldTest.class doesn't exist.
[javac] /Users/jtyler/Projects/AntHelloWorld/src/log4j.properties skipped - don't know how to handle it
[javac] oata/HelloWorld.java added as oata/HelloWorld.class doesn't exist.
[javac] Compiling 2 source files to /Users/jtyler/Projects/AntHelloWorld/build/classes
[javac] Using modern compiler
[javac] Compilation arguments:
[javac] '-d'
[javac] '/Users/jtyler/Projects/AntHelloWorld/build/classes'
[javac] '-classpath'
[javac] '/Users/jtyler/Projects/AntHelloWorld/build/classes:/Users/jtyler/Projects/AntHelloWorld/lib/log4j-1.2.8.jar:/usr/share/ant/lib/ant-launcher.jar:/usr/share/ant/lib/ant-antlr.jar:/usr/share/ant/lib/ant-jmf.jar:/usr/share/ant/lib/ant-junit.jar:/usr/share/ant/lib/ant-junit4.jar:/usr/share/ant/lib/ant-swing.jar:/usr/share/ant/lib/ant-testutil.jar:/usr/share/ant/lib/ant.jar'
[javac] '-sourcepath'
[javac] '/Users/jtyler/Projects/AntHelloWorld/src'
[javac] '-g:none'
[javac]
[javac] The ' characters around the executable and arguments are
[javac] not part of the command.
[javac] Files to be compiled:
[javac] /Users/jtyler/Projects/AntHelloWorld/src/HelloWorldTest.java
[javac] /Users/jtyler/Projects/AntHelloWorld/src/oata/HelloWorld.java
[javac] /Users/jtyler/Projects/AntHelloWorld/src/HelloWorldTest.java:1: package junit.framework does not exist
[javac] public class HelloWorldTest extends junit.framework.TestCase {
[javac] ^
[javac] /Users/jtyler/Projects/AntHelloWorld/src/HelloWorldTest.java:7: cannot find symbol
[javac] symbol : method fail(java.lang.String)
[javac] location: class HelloWorldTest
[javac] fail("An error message");
[javac] ^
[javac] 2 errors
BUILD FAILED
/Users/jtyler/Projects/AntHelloWorld/build.xml:20: Compile failed; see the compiler error output for details.
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1150)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:912)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: 0 seconds
Is there something I'm missing that's needed to be included that was (or wasn't) mentioned in the tutorial?
EDIT: Based on Alex's inquiries, I tested to see if I had ant's junit jars in my shared directories (which I do):
$ ls /usr/share/ant/lib/ant-junit*.jar
/usr/share/ant/lib/ant-junit.jar /usr/share/ant/lib/ant-junit4.jar
I also tried to manually put ant-junit, ant-junit4.jar, and junit.jar into my project's lib directory, with the following results (all errors) after running ant clean junit:
junit.jar - /Users/jtyler/Projects/AntHelloWorld/build.xml:45: Reference application not found.
ant-junit.jar: package junit.framework does not exist
ant-junit.jar and ant.junit4.jar: package junit.framework does not exist
ant-junit4.jar: package junit.framework does not exist
EDIT The whole error when only junit.jar is included in the lib folder and calling ant clean junit is:
$ ant clean junit
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml
clean:
[delete] Deleting directory /Users/jtyler/Projects/AntHelloWorld/build
compile:
[mkdir] Created dir: /Users/jtyler/Projects/AntHelloWorld/build/classes
[javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to /Users/jtyler/Projects/AntHelloWorld/build/classes
[copy] Copying 1 file to /Users/jtyler/Projects/AntHelloWorld/build/classes
jar:
[mkdir] Created dir: /Users/jtyler/Projects/AntHelloWorld/build/jar
[jar] Building jar: /Users/jtyler/Projects/AntHelloWorld/build/jar/HelloWorld.jar
junit:
BUILD FAILED
/Users/jtyler/Projects/AntHelloWorld/build.xml:45: Reference application not found.
The tutorial is misleading. The junit files that are included in ant do not work. I downloaded juinit and copied the jar file to the lib folder of the project and it now works fine.
On top of including the junit.jar file within my lib dir, I added the following line to my build.xml file right above the junit task declaration:
<path id="application" location="${jar.dir}/${ant.project.name}.jar"/>
which is a copy of the 'application' path id declaration within the 'run' task definition.
The application builds successfully, and I get the following output with ant junit (test failure is expected):
ant junit
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml
compile:
[javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
jar:
junit:
[junit] Running HelloWorldTest
[junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.001 sec
[junit] Test HelloWorldTest FAILED
BUILD SUCCESSFUL
Total time: 1 second
Comments the following questions would be greatly appreciated:
Is the fact I actually needed to include the junit.jar file in my lib folder something I should be concerned about? I would assume this is not necessary if ant's tutorial explicitly states "Because Ant has a built-in JUnit 3.8.2 you could start directly using it."
Is the fact I needed to define the 'application' path id outside of the run task definition a typo within the apache ant tutorial? Or are there other things I missed or should consider?
Thanks to all those that helped.
You must specify junit.jar itself in your compile and in your <junit> task's classpath. This is not automatically included in your compile:
<!-- Contains junit.jar and other needed test classes -->
<path id="junit.classpath">
<fileset dir="${junit.directory}"/>
</path>
<!-- Contains third party jars your code is dependent upon -->
<path id="main.classpath">
<fileset dir="${dependent.jar.dir}"/>
</path>
<!-- Your main Java source code directory -->
<property name="main.srcdir" value="${basedir}/src/main/java"/>
<!-- Your main Junit test directory source code -->
<property name="test.srcdir" value="${basedir}/src/test/java"/>
<!-- Where you're compiling your main classes to -->
<property name="main.destdir" value="${basedir}/target/classes"/>
<!-- Where you're compiling your test classes to -->
<property name="test.destdir" value="${basedir}/target/test-classes"/>
[...]
<!-- Compile Test Classes -->
<!-- Notice you have three classpath elements:
* Your main classes you compiled before
* Your test main classpath that your main classes
were dependent up
* The junit.jar classpath
-->
<javac destdir="${test.destdir}"
srcdir="${test.srcdir}">
<classpath>
<!-- Your non-test classes you compiled before -->
<pathelement path="${main.destdir}/>
</classpath>
<classpath refid="junit.classpath"/>
<classpath refid="main.classpath"/>
</javac>
<!-- Now run your unit tests: Note how you have the same
three classpath elements as before -->
<junit fork="yes">
<classpath>
<pathelement path="${main.destdir}"/>
<pathelement path="${test.destdir}"/>
</classpath>
<classpath refid="main.classpath"/>
<classpath refid="junit.classpath"/>
[...]
</junit>
Related
I have a project which used to compile and run fine. However, it complains when I try to use switch with Strings, with a java 7 compiler. I build this project using Ant.
The Ant config file (projectBuilder.xml) :
<?xml version="1.0"?>
<project name="TutoMigLayout" 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="lib" />
<property name="build.dir" location="bin" />
<!--
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>
</path>
<!-- Deletes the existing build directory-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!-- Creates the build directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<echo message="Using Java version ${ant.java.version}."/>
<javac target="1.7" srcdir="${src.dir}" destdir="${build.dir}" debug="true" classpathref="build.classpath" />
</target>
<target name="Main" depends="compile">
<description>Main target</description>
</target>
</project>
I get this in the console :
Buildfile: C:\Users\workspace\TutoMigLayout\projectBuilder.xml
clean:
[delete] Deleting directory C:\Users\workspace\TutoMigLayout\bin
makedir:
[mkdir] Created dir: C:\Users\workspace\TutoMigLayout\bin
compile:
[echo] Using Java version 1.7.
[javac] C:\Users\workspace\TutoMigLayout\projectBuilder.xml:31: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 5 source files to C:\Users\workspace\TutoMigLayout\bin
[javac] javac: invalid target release: 1.7
[javac] Usage: javac <options> <source files>
[javac] use -help for a list of possible options
BUILD FAILED
C:\Users\workspace\TutoMigLayout\projectBuilder.xml:31: Compile failed; see the compiler error output for details.
Total time: 481 milliseconds
When I remove the compiler version from the compile target, I get that instead :
[javac] C:\Users\invite01.acensi\workspace\TutoMigLayout\src\components\EqualsAction.java:26: incompatible types
[javac] found : java.lang.String
[javac] required: int
[javac] switch(operator) {
[javac] ^
I thought we could switch with Strings in Java 7 ?
If I click run one more time, I get a completely different message :
Error : could not find or load the main class test.Test
Edit : javac -version
javac 1.6.0_32
OK how do I change that ? I thought setting compiler compliance level to 1.7 and selecting jre7 was enough ?
Try adding a source="1.7" attribute. This controls the language features. However, in most cases both target and source attributes should be set to the same version. And, as mentioned by others, the compiler must support this version.
I am trying to write my first build.xml file for an established java project that has just been moved from Netbeans.
A. The objectives that I'm trying to meet are pretty simplistic:
Using the "dest" target below, copy all the source files (4 in all from 1 package) to src/test that I am trying to create. The source files were copied to the "src/test" directory but then a "test" directory was also getting created in the "src/test" directory, why I'm not sure.
Using the "jar" target below, create a jar that has all the class files under the package name directory - DID NOT WORK AT ALL!
Using the "compile" target to ensure that all the code is compiled successfully but I got a lot of errors. The code does CLEAN and BUILD successfully in Eclipse so I'm not sure what I did wrong in the ANT script and one thing I noticed was that it was trying to compile "8" files when there are only "4". Not sure where the other 4 are coming from though it indicates a duplication. The errors show with regard to a missing symbol seem to refer to import statements regarding required projects that are included in the build path so I'm not sure how to address the issues ANT raises in its compile.
B. Here is my first attempt at creating my first build.xml file but I"m experiencing the problems shown below:
<project name="ThalesDataGenerator" basedir="." default="clean-build">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="${src.dir}/test"/>
<property name="dist.dir" value="dist"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/${ant.project.name}"/>
<property name="main-class" value="thalesdatagenerator.ThalesDataGenerator"/>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dest.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="dest">
<mkdir dir="${dest.dir}"/>
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" includes="**"/>
</copy>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/ThalesDataGenerator.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,dest,jar,run"/>
</project>
Here are the errors I got:
> Buildfile: C:\ATMSwitch\ThalesDataGenerator\build.xml clean:
> [delete] Deleting directory C:\ATMSwitch\ThalesDataGenerator\build
> dest:
> [mkdir] Created dir: C:\ATMSwitch\ThalesDataGenerator\src\test
> [copy] Copying 4 files to C:\ATMSwitch\ThalesDataGenerator\src\test
> [copy] Copied 2 empty directories to 1 empty directory under C:\ATMSwitch\ThalesDataGenerator\src\test
> compile:
> [mkdir] Created dir: C:\ATMSwitch\ThalesDataGenerator\build\classes
> [javac] C:\ATMSwitch\ThalesDataGenerator\build.xml:22: warning: 'includeantruntime' was not set, defaulting to
build.sysclasspath=last; set to false for repeatable builds
> [javac] Compiling 8 source files to C:\ATMSwitch\ThalesDataGenerator\build\classes
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\thalesdatagenerator\ISOUtil.java:36:
duplicate class: thalesdatagenerator.ISOUtil
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesDataGenerator.java:13:
package common.database does not exist
> [javac] import common.database.Database;
> [javac] ^
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesSystem.java:13:
package com.sharpbancsystems.atmterminals.thales does not exist
> [javac] Note: C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesDataGenerator.java
uses unchecked or unsafe operations.
> [javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 18 errors
BUILD FAILED
C:\ATMSwitch\ThalesDataGenerator\build.xml:22: Compile failed; see the compiler error output for details.
Total time: 874 milliseconds
Any help/direction would be greatly appreciated. Regards.
Let's take this one at a time:
A few hints:
Use ${basedir} when defining properties. For example, <property name="src.dir" value="${basedir}/src"/> instead of simply value="src"/>
Don't force a clean as part of your default target. The standard default target should build the jar and that's it. Doing a clean makes you duplicate work that may not be needed. You can use a target named all to clean, build, and execute.
As mentioned above, use the default target names clean to clean up your build, and all to run all targets in your build. Neither of these should be the default target.
Now back to your issue. Your <javac> target looks like this:
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
Are you saying that there are no third party jars that your source depends upon? You need to create a compile classpath that includes all of the third party jars your source depends upon. In Eclipse, there's a built in classpath you're using. In Ant, you have to specify this.
Let's assume that all jars you need for your source to compile are stored in ${basedir}/lib. Your compile target needs to look like this:
<target name="compile">
<mkdir dir="${classes.dir}"/>
<path id="compile.classpath">
<fileset dir="${basedir}/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<javac srcdir="${src.dir}"
classpathref="compile.classpath"
destdir="${classes.dir}"/>
</target>
There are many ways of doing this, but this is the easiest syntactically. I use <path> to define a compile.claasspath that contains all of the jars I need. Since they all live in the lib directory, it was pretty easy.
Next, I use the classpathref parameter to specify this classpath when I compile my Java code.
This will get the classes compiled (which isn't happening now). Since compile target fails, the Ant build ends there before the <jar> task is called. Getting the compile to work should allow the rest of your build to work.
happened to me. was because i hadn't copied selenium-server-standalone-3.4.0.jar to my C:\jars dir (ws.jars)
I'm totally new with Ant, and I don't know how classpath works in Ant. I have wrote a Java class to do some scripting for me, and I wanted to deploy it as a task in Ant. The class uses commons-codec-1.2.jar as an external library, in other to do some Unicode and MD5 tasks for me. I've posted my build.xml file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="TcZip" basedir="." default="jar">
<property name="src.dir" value="src" />
<property name="classes.dir" value="classes" />
<property name="library.dir" value="library" />
<path id="external.classpath">
<pathelement location="${library.dir}/commons-codec-1.2.jar" />
</path>
<target name="clean" description="Delete all generated files">
<delete dir="classes" />
<delete file="TcZip.jar" />
</target>
<target name="compile" description="Compiles the task">
<mkdir dir="${classes.dir}" />
<javac srcdir="src" destdir="classes" />
<classpath>
<path refid="external.classpath" />
</classpath>
</target>
<target name="jar" description="JARs the Task" depends="compile">
<jar destfile="TcZip.jar" basedir="classes" />
</target>
<target name="use" description="Use the task" depends="jar">
<taskdef name="tczip" classname="ZipComparision" classpath="${ant.project.name}.jar" />
<tczip />
</target>
</project>
In my task directory, I have :
+task
- build.xml
- classes
- library
- src
library and classes contain the commons-codec-1.2.jar file. However, as seen in the build.xml file I have added a <property> tag for the library.
When I execute ant compile I get:
compile:
[javac] C:\workspace\task\build.xml:19: warning: 'includeantr
untime' was not set, defaulting to build.sysclasspath=last; set to false for rep
eatable builds
[javac] Compiling 3 source files to C:\workspace\task\classes
[javac] C:\workspace\task\src\Tczip.java:18: error: package o
rg.apache.commons.codec.binary does not exist
[javac] import org.apache.commons.codec.binary.Hex;
[javac] ^
[javac] C:\workspace\task\src\Tczip.java:190: error: cannot f
ind symbol
[javac] mdEnc = new String( Hex.encodeHex( digest ));
[javac] ^
[javac] symbol: variable Hex
[javac] location: class Tczip
[javac] C:\workspace\task\src\Tczip.java:254: error: cannot f
ind symbol
[javac] mdEnc = new String( Hex.encodeHex( digest ));
[javac] ^
[javac] symbol: variable Hex
[javac] location: class Tczip
[javac] 3 errors
BUILD FAILED
C:\workspace\task\build.xml:19: Compile failed; see the compiler
error output for details.
Could someone please help me with that?
Thank you
replace location in pathelement directive with path.
I'm trying to figure out how to set up a project using ant. For some reason, I can't get the junit tests to run. I set up a simple dummy project using ant to try and figure this out. All it has is a single unit test that should pass trivially.
My project structure looks like this.
.
|-- build.xml
|-- src
`-- test
|-- foo
| `-- MainTest.java
`-- junit-4.10.jar
MainTest.java looks like this.
package foo;
import org.junit.*;
import static org.junit.Assert.*;
public class MainTest {
#Test
public void passes() {
System.out.println("It works!");
}
}
And here is build.xml.
<project name="Nes" default="build" basedir=".">
<target name="build-test">
<javac srcdir="test">
<classpath>
<pathelement location="test/junit-4.10.jar" />
</classpath>
</javac>
</target>
<target name="test" depends="build-test">
<junit>
<classpath>
<pathelement location="test/junit-4.10.jar" />
</classpath>
<batchtest>
<fileset dir="test" includes="foo/MainTest.class" />
</batchtest>
</junit>
</target>
</project>
Here's the output I get from running ant test.
Buildfile: /home/hayden/dev/nes/build.xml
build-test:
[javac] /home/hayden/dev/nes/build.xml:4: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
test:
[junit] Test foo.MainTest FAILED
BUILD SUCCESSFUL
Total time: 0 seconds
I'm running ant 1.8.2 and Java 6.
What am I doing wrong?
You need to add a path to the actual .class file so JUnit can actually run the test. Since you are fully qualifying the test class name, you need to include the directory that contains the foo package:
<target name="test" depends="build-test">
<junit>
<classpath>
<pathelement location="test/junit-4.10.jar" />
<pathelement location="test" />
</classpath>
But I'd recommend that you change the javac task to not output build artifacts (i.e. .class files) into the same directory as your source files. Create a top-level "build" directory and put all your build output in there.
You should also quiet the first warning by adding a includeantruntime attribute to the javac task:
<javac srcdir="test" includeantruntime="false">
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.