Ant command line arguments - java

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.

Related

Adding autoprefix-cli to ANT build

I am trying to add autoprefix-cli to my ANT build. Below is my code.
<target name="auto">
<apply executable="autoprefixer-cli.bat" verbose="true" force="true" failonerror="true">
<arg value="-d" /> <!-- Turn on verbose -->
<arg value="prefix" />
<arg value="*.css" />
</apply>
</target>
When i do a ant build, it gives me an error saying resource not specified.
BUILD FAILED
D:\tempTest\AntTestProject\build.xml:25: no resources specified
Note: I can access autoprefix-cli from command line, its installed with -g flag and also it works when i directly use it from commandline.
The apply task basically loops the exec task on a batch of resources (files, directories, URLs, etc). If you only want to run one command, use exec instead.
However, you will likely also need to alter your command. From Ant's exec task documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch.
https://ant.apache.org/manual/Tasks/exec.html
So instead you should have:
<exec executable="cmd" verbose="true" force="true" failonerror="true">
<arg value="/c" />
<arg value="autoprefixer-cli.bat" />
<arg value="-d" />
<arg value="prefix" />
<arg value="*.css" />
</exec>

Creating arguments for an ant task with lines from file

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.

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

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>

Categories