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.
Related
This is not truly a YUI-related question.
I am trying to use YUI Compressor(in fact a .jar application) for multiple JS files. I would like to get those files automatically(as opposed to specifying every file's name in build.xml).
Here is my build.xml file:
<?xml version="1.0" encoding="utf-8"?>
<project name="CUIProject" default="prod">
<target name="-load.properties">
<loadproperties>
<file file="minify.properties"/>
</loadproperties>
</target>
<!--Minify JS files-->
<target name="-js.minify">
<apply executable="java" parallel="false" dest="${build.dir}">
<fileset dir="${src.dir}"/>
<mapper>
<globmapper from="*.js" to="*-min.js" handledirsep="yes"/>
</mapper>
<arg value="-jar"/>
<arg path="${minifier.dir}"/>
<srcfile/>
<arg value="-o"/>
<targetfile/>
</apply>
</target>
<!--Build-->
<target name="prod"
depends="
-load.properties,
-js.minify
">
</target>
</project>
The problem is that I get a FileNotFound exception because for some reason the <targetdir/> is somehow escaped: the \ separator from Windows is used as an escaping character, so my path because just one very large word.
How can I avoid such a behavior?
I had the same issue with YUI. The easiest way I found is to use the Ant-Contrib <for> task.
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${antlib.dir}/antcontrib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<!-- Minimize Concatinated JavaScript Files -->
<for param="concat.dir">
<dirset dir="${work.js.dir}">
<include name="*"/>
</dirset>
<sequential>
<echo message="[java] Minimized #{concat.dir}/concat-${build.id}-min.js>"/>
<java jar="${yui.compressor.jar}"
failonerror="true"
fork="true">
<arg value="-o"/>
<arg value="#{concat.dir}/concat-${build.id}-min.js"/>
<arg value="#{concat.dir}/concat-${build.id}.js"/>
</java>
</sequential>
</for>
Also take a look at the <pathconvert> task. This will convert the path separator from one OS to another. This may fix your issue. The File Mapper page in Ant's manual contains some examples using <pathconvert>.
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
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.
I have my own build.xml file consisting of few targets to compile and run my Java project with Ant. Here is the relevant part of this one:
<path id="libpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<patternset id="resources">
<include name="relative/path/to/resources/" />
</patternset>
<path id="resourcespath">
<fileset dir="${src.dir}">
<patternset refid="resources" />
</fileset>
</path>
<target name="compile">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libpath" includeantruntime="false" debug="true" debuglevel="lines,vars,source" />
<copy todir="${classes.dir}">
<path refid="resourcespath" />
</copy>
</target>
<target name="debug" depends="compile">
<java fork="true" classname="${main-class}">
<sysproperty key="java.library.path" path="${dist.dir}"/>
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
</java>
</target>
All I want to do is to debug the compiled code in ${classes.dir} using Netbeans by running the debug target of build.xml. At the moment, this target starts the compiled application without giving a chance to stop at breakpoints. I know that Netbeans generates default build-impl.xml file but this one is too large and is difficult to understand for me. That's why I want to know whether it's possible to use the Netbeans IDE to debug Java code compiled by Ant using my own build.xml file.
There is also the similar question on debugging Java code when using Ant script in Eclipse, but as I can see the solution proposed is Eclipse specific.
Method proposed by Sergey works but one have to do the following actions each time to debug a project:
Press Debug Main Project button (Ctrl+F5), Netbeans will wait for a debbuger to attach;
Choose "Debug" > "Attach Debugger..." from Netbeans main menu, specify debugger's parameters in appeared dialog and then press "OK" button or select previously used debbuger from dropdown list of toolbar's debug button (e.g. Attach to 5432);
Debug your application and then close it (Shift+F5 will not terminate the application as usual, it will only finish the debugging session).
In my opinion, the better way to use "Attach Debugger..." is to choose SocketListen connector:
Perform step 2 from the previous steps' chain (select Listen to 5432) - debugger will wait for an application to start (please note, that jvmarg's suboption server must be set to n - refer to Remote Debugging FAQ).
Press Debug Main Project button (Ctrl+F5).
Debug your application and then close it or terminate it simultaneously with debugging session by pressing Shift+F5.
Anyway, this method is also uncomfortable, especially if you were accustomed to start the debugging by simply pressing Ctrl+F5 or pressing corresponding toolbar button. Choosing required debugger from the dropdown menu everytime is annoying.
So here is the best solution - start the debugger directly from Ant's debug target. In my case it looks like this:
<target name="debug" depends="compile">
<nbjpdastart addressproperty="jpda.address" name="MyProjectName" transport="dt_socket">
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
</nbjpdastart>
<java fork="true" classname="${main-class}">
<sysproperty key="java.library.path" path="${dist.dir}" />
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
<jvmarg value="-Xdebug" />
<jvmarg value="-Xnoagent" />
<jvmarg value="-Djava.compiler=none" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}" />
</java>
</target>
To get more information, please, look at Creating a Target to Debug Your Java SE Application
Advises from Eclipse topic will work for you as well.
Use next JVM parameters (add them to java target call):
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
In netbeans use Debug->Attach Debugger with port 5432
I have been experiencing a weird problem that I can't find a solution to. When I am running a project through ant and there's an uncaught exception, ant simply freezes with absolutely no error. Here is my build file. This is a multi threaded environment, also
<fileset id="masterlibs" dir="${lib.dir}">
<patternset>
<include name="*.jar"/>
</patternset>
</fileset>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
</javac>
</target>
<target name="run" depends="compile">
<java classname="stockData.AlertHandler" fork="true">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="false"/>
<arg line="27_4"/>
<arg line="false"/>
</java>
</target>
Ctrl+Break will show you a dump of threads and indicate what each is doing (plus any deadlocks). I'm guessing it's your spawned AlertHandler process and it may be easier to debug this outside the Ant environment.
If Ctrl+Break doesn't work (from the article)
On UNIX platforms you can send a
signal to a program by using the kill
command. This is the quit signal,
which is handled by the JVM. For
example, on Solaris you can use the
command kill -QUIT process_id, where
process_id is the process number of
your Java program.
Ant can sometimes freeze when it runs a java class that asks for interactive input. Is it possible that your AlertData class asks for input?