Creating arguments for an ant task with lines from file - java

I have an ant task, which takes multiple arguments. I would like to pass the arguments loaded from file/property.
I tried the below code, but got error.
<loadfile property="file" srcfile="names.properties"/>
<java classname="com.task.MyTask" fork="true" failonerror="true">
<for param="line" list="${file}">
<arg value="--names"/>
<arg value="${line}"/>
</for>
</java>
Thanks in advance for any help.

Related

Running One Server and Two Clients using ANT

I am a newbie into ANT world. My Application is based on Client-Server architecture which is using "RMI" for communication.
I need to write an ANT build script which automatically compiles the whole code, starts the server and run two clients connected to that server.
Here's structure of my current build.xml file.(I am not sure what's wrong)
<?xml version="1.0" encoding="UTF-8"?>
<project default="runClientOne" name="MyFirstAntProject">
<target name="compile">
<javac srcdir="./src" destdir="classfiles" />
</target>
<target name="runServer" depends="compile" >
<java classname="com.jain.RMIServer">
<classpath path="classfiles" />
</java>
</target>
<target name="runClientOne" depends="runServer">
<java classname="com.jain.RMIClient" fork="true" taskname="A" >
<classpath path="classfiles" />
<arg value="localhost"/>
<arg value="Sumit"/>
</java>
<java classname="com.jain.RMIClient" fork="true" taskname="B">
<classpath path="classfiles" />
<arg value="localhost"/>
<arg value="Sushil"/>
</java>
</target>
</project>
Thanks Guys,
I have figured out that I will have to use the <parallel>My CODE </parallel> .
I stumbled upon this question when I was trying to accomplish a similar thing, here is how I achieved a target that starts running the server and client. (My answer expands on Sushil's and this SO answer)
Here is the case: I have a server, and when I start up I want 3 clients to connect to the server right away, all in a separate process.
The trick is to nest parallel and sequential in the build.xml targets individually.
The sleep call can be changed to wait on the process too.
<target name="start-all" depends="build"
description="Starts 1 Server and 2 clients ">
<parallel>
<sequential>
<java classname="Server" fork="true">
<classpath refid="master-classpath"/>
<arg line="${port}"/>
</java>
</sequential>
<sequential>
<sleep milliseconds="500"/>
<antcall target="start-client">
<param name="server.address" value="localhost" />
<param name="server.port" value="${port}"/>
</antcall>
</sequential>
</parallel>
</target>
This would start the server, and then the clients, alternatively, using <daemon> to start the server and then immediately launching the clients right after.

Regarding ant: how i can give few aruments to main function?

Here is my "run" in my ant build file:
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
I want to run it like this:
ant run -Darg0=First.txt -Darg1=Second.txt -Darg2=Third.txt -Darg3=Fourth.txt -Darg4=Fifth.txt
What changes should i make in my "run"?
Many thanks for help!
Here's the ant task to run any program (including, but not limited to Java programs):
<target name="run">
<exec executable="name-of-executable-file">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</exec>
</target>
Here's the task to run a Java program from a .jar file:
<target name="run-java">
<java executable="path for jar">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</java>
</target>
You can invoke either from the command line like this:
ant -Darg0=Hello -Darg1=World run
Updated task
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
<arg value="${arg0}" />
<arg value="${arg1}" />
</java>
</target>
Check out the <java> task documentation
Use nested <arg> and <jvmarg> elements to specify arguments for the
Java class and the forked VM respectively.
and the <arg> subtask

Ant command line arguments

Program works fine when run with eclipse run configurations, but when run with ant, it is unable to parse int from args[0], which I do not understand. Full code is available here https://gist.github.com/4108950/e984a581d5e9de889eaf0c8faf0e57752e825a97
I believe it has something to do with ant,
target name="run" description="run the project">
java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6 in.txt"/>
/java>
/target>
the arg value will be changed via the -D flag, as in ant -Dargs="6 testData1.txt" run.
Any help would be much appreciated, it is very frustrating.
You need to supply the arguments as two different arg values:
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6" />
<arg value="in.txt" />
</java>
</target>
You can also use the line attribute; From the ANT docs:
<arg value="-l -a"/>
is a single command-line argument containing a space character, not separate commands "-> l" and "-a".
<arg line="-l -a"/>
This is a command line with two separate arguments, "-l" and "-a".
Expanding epoch 's answer.
java task supports sysproperty and jvmarg.
For example (from ant java task page)
<java classname="test.Main"
fork="yes" >
<sysproperty key="DEBUG" value="true"/>
<arg value="-h"/>
<jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/> </java>
So you could construct the args from the command line passed to ant.
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<sysproperty key="testarg" value="${testarg}"
<arg value="${arg1}" />
<arg value="${arg2}" />
</java>
</target>
Now if you call ant with ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt, then testarg will be available via property. Others will become normal arguments to the java program.

How to specify input parameters for <java jar="sth.jar" fork="true"/> in build.xml of Ant?

This may be a naive question for the people who are familiar with Ant. I am new to Ant. please do me a favor. Thanks!
I write something like this in build.xml.
<target name="run">
<java jar="build/jar/sth.jar" fork="true"/>
</target>
But, I want to put some parameters to it, like
java -jar build/jar/sth.jar input.txt
How to do that?
By the way, where could I find the specifications of build.xml grammar? like how many attributes are there? what are the other attributes rather than "jar" "fork"?
This should do what you want:
<exec executable="java">
<arg value="-jar" />
<arg value="build/jar/sth.jar" />
</exec>
Here is the source of documentation http://ant.apache.org to start with.
EDITED:
You can use java tag as well, simply specifying tag args="input.txt" or whatever parameters you would like to pass. More info on java command
<java jar="build/jar/sth.jar">
<arg value="input.txt" />
</java>

Ant & Mercurial

I am trying to add some tag information in my ant script using the following target but I get an error (Result=-1) and hg tells me it does not recognise the command:
<target name="-post-init">
<exec outputproperty="hg.tag" executable="hg">
<arg value="parents --template {latesttag}+{latesttagdistance}" />
</exec>
</target>
If I only include value="parents" it works fine.
If I run the command line hg parents --template {latesttag}+{latesttagdistance} it works fine too.
Any ideas on what is wrong in my syntax?
Just tried this and it works fine:
<exec outputproperty="hg.tag" executable="hg">
<arg value="parents" />
<arg value="--template" />
<arg value="{latesttag}+{latesttagdistance}" />
</exec>
I needed to split the arguments.

Categories