I am having trouble just creating something simple, and it does not seem that any website is clear on how to do this, and honestly I think it should be simple.
I have a bunch of java files for a project. I want to compile all of them, and then run each file with specific arguments.
Basically I want the order of operations to be something like this
javac prob1.java
javac prob2.java
java prob1 parameter
java prob2 parameter
But I want that in ant (build.xml).
I can do the compile part just fine with
<project default="compile">
<target name="compile">
<javac srcdir="." />
</target>
</project>
I just can not get it to run say prob1 with an argument. I imagine this is extremely easy, but every solution I have found, does not seem to work. Also note prob1.class and prob2.class are in the same directory.
This should work:
<target name="run">
<java classname="prob1">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
<java classname="prob2">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
</target>
Related
Is there a way to set the value of the javac classpath property in Apache Ant (v1.9.6) so that I don't have to literally specify all the jars that I want to include e.g. a directory or file glob.
So, if I have something like:
<javac classpath="./lib/one.jar":./lib/two.jar:./lib/three.jar..."
...is there anyway to just specify my ./lib directory once, like the way you can do when you run a java application like:
java -cp ./lib/'*'
I've tried that, and just using ./lib, or./lib/* or ./lib/*.jar but they don't work.
And another solution using a reusable classpath reference:
<path id="compile.path">
<fileset dir="lib" includes="*.jar"/>
</path>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="compile.path"/>
<pathelement path="${classes.dir}"/>
</classpath>
..
..
Try putting a <classpath> element under <javac>:
<javac ...>
<classpath>
<fileset dir="lib" includes="*.jar"/>
</classpath>
</javac>
I would like to start Server and the ProxyServer class simultaneously, using Ant tag, is it possible to run the wo classes?
Here is the code I tried but Ant only starts the Server class and does not do anything there after, not sure if there is away in ant to achieve this.
Appreciate your help.
<target name="pxyServer" depends="server">
<echo>Executing Target - Run ProxyServer</echo>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</target>
<target name="server">
<echo>Executing Target - RunServer</echo>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
</target>
Your targets are executed sequentially, and since the first one keeps running, the second one never gets the chance to start.
For parallel execution, you can use ant's "parallel" task:
http://ant.apache.org/manual/Tasks/parallel.html
Your modified script should probably look something like this:
<target name="startServerAndProxy">
<echo>Running server and proxy...</echo>
<parallel>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</parallel>
</target>
(Of course, if you're trying to start some third application in parallel, a client for example, then you should also include that one in the "parallel".)
UPDATE:
To start the server and the proxy each in its own console, I don't know if it can be done with the "java" Ant task, but I just tested that it can be done with "exec":
<target name="doit">
<parallel>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.Server"/>
</exec>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.ProxyServer"/>
</exec>
</parallel>
</target>
I am using the following demonstration script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
When I run this build script (with target default), the error is
build.xml:9: Problem: failed to create task or type antlib:net.sf.antcontrib:if
The pathelement lib/ant-contrib-1.0b3.jar exists, and ant is picking it up. I'm thinking the problem is how I'm using the xmlns. I'm taking this from another example that I have that also doesn't work for me (it works on a particular server, though!), and trying to figure out what the magic sauce is.
Your taskdef where you're adding ant-contrib needs to declare a URI the same as the namespace you're defining and prefixing in the project. Similar to how the taskdef over here works.
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
Well, the error has if on the end, and it's talking about line 9. I think it's a problem with the syntax of this tag:
<deploy:if>
I can't find any documentation on a "deploy:if" tag, or even a "deploy" tag. I think there is no 'deploy' task in Ant - you need to make a 'deploy' target.
How about trying this:
<if>
<isset property="defaultprop"/>
<then>
<antcall target="deploy" />
</then>
</if>
As I read it, this will check the isset, then call the "deploy" target if it's set.
Of course, you need to make the 'deploy' target now :)
I have a jar file with multiple executable classes, how can I run the main method of a using an ant target ?
Thanks
Take a look at the Ant Java Task. You should be able to create a target that looks like this:
<target name="mytarget" description="runs my class" >
<java classname="test.Main">
<classpath>
<pathelement location="dist/test.jar"/>
</classpath>
</java>
</target>
Alternative, using Ant Exec Task:
<target name="mytarget" description="runs my class">
<exec executable="java">
<arg line="-classpath dist/test.jar test.Main"/>
</exec>
</target>
Using ant's java task:
<java fork="yes" classname="com.example.Class" failonerror="true">
<classpath>
<pathelement path="path/to/jar/containing/the/com.example.Class"/>
...
</classpath>
...
</java>
First you have to decide which class is used as entry point.
Let's assume that the class is com.mycompany.Main
in this case if you wish to run application from command line say
java -cp my.jar com.mycompany.Main
Now you can either run it as java program:
<java classname="com.mycompany.Main">
<classpath>
<pathelement location="myjar.jar"/>
</classpath>
</java>
(see http://ant.apache.org/manual/Tasks/java.html)
or run it as an generic external process:
(see http://ant.apache.org/manual/Tasks/exec.html).
I think that using java target is preferable.
I have an Ant script with a junit target where I want it to start up the VM with a different working directory than the basedir. How would I do this?
Here's a pseudo version of my target.
<target name="buildWithClassFiles">
<mkdir dir="${basedir}/UnitTest/junit-reports"/>
<junit fork="true" printsummary="yes">
<classpath>
<pathelement location="${basedir}/UnitTest/bin"/>
<path refid="classpath.compile.tests.nojars"/>
</classpath>
<jvmarg value="-javaagent:${lib}/jmockit/jmockit.jar=coverage=:html"/>
<formatter type="xml" />
<test name="GlobalTests" todir="${basedir}/UnitTest/junit-reports" />
</junit>
</target>
Have you tried:
<junit fork="true" printsummary="yes" dir="workingdir">
I think the other answers might be overlooking the fact that you want the working directory to be specified, not just that you want to run junit on a particular directory. In other words, you want to make sure that if a test creates a file with no path information, it is from the base directory you are specifying.
Try to pass in the directory you want as a JVM arg to junit, overriding user.dir:
<junit fork="true" ...>
<jvmarg value="-Duser.dir=${desired.current.dir}"/>
....