I'm wanting use ant to run a class and then the debugger (jdb) or the other way round
Whichever way round I do it I need one to return immediately as the other needs to attach...
here's the two tasks I'm working on at the moment... (where debug is the target run)
<target
name="run-debug-target"
depends="compile" >
<java
fork="true"
classname="uk.co.bedroomcoders.ple.desktop.DesktopLauncher"
classpath="bin:libs/gdx-backend-lwjgl.jar:libs/gdx-backend-lwjgl-natives.jar:libs/gdx.jar:libs/gdx-natives.jar" >
<jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:6000,server=y,suspend=y" />
</java>
</target>
<target
name="debug"
depends="run-debug-target"
description="debugs the project compiling if needed" >
<exec spawn="true" executable="jdb">
<arg value="-listen" />
<arg value="localhost:6000"/>
</exec>
</target>
https://ant.apache.org/manual/Tasks/java.html
See the spawn property:
if enabled allows to start a process which will outlive ant.
Requires fork=true, and not compatible with timeout, input, output, error, result attributes.
So..
<java
fork="true"
spawn="true"
classname="uk.co.bedroomcoders.ple.desktop.DesktopLauncher"
classpath="bin:libs/gdx-backend-lwjgl.jar:libs/gdx-backend-lwjgl-natives.jar:libs/gdx.jar:libs/gdx-natives.jar" >
<jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:6000,server=y,suspend=y" />
</java>
In this way, <java> task will start a new java process running the java class and return immediately without waiting for the process to return.
Related
i have a taskdef pointing to a class
<taskdef name="configjar" classname="com.bea.alsb.tools.configjar.ant.ConfigJarTask" classpathref="configjar.path">
</taskdef>
Inside this i want to pass java system property. Like how we do in the java task
<java >
<sysproperty key="" value""/>
</java>
The problem is the jar is some library which I can't modify .I can't use a command to set in a build environment we have.
I know i can do this by setting ANT_OPTS,but can i do this from build.xml.How can i make this happen
Not sure it's exact way or not. But found a work around for this
<java classname="org.apache.tools.ant.launch.Launcher" fork="true" failonerror="true">
<sysproperty key="weblogic.home" value="${weblogic.home}"/>
<sysproperty key="osb.home" value="${osb.home}"/>
<arg value="test"/>
</java>
<target name="test">
<configjar debug="${task.debug}"
failonerror="${task.failonerror}"
errorProperty="${task.errorproperty}"
settingsFile="${settingsFile}" >
</configjar>
</target>
I have invoked ant using java command and set the two system properties as shown above.
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>
On running the following command:
ant targetname -Dk1=v1 -Dk2=v2
I want the command line parameters passed down to java, like java whatever -Dk1=v1 -Dk2=v2.
I need to access these parameters from Java code with System.getProperty or System.getenv.
What do I need to write in my ant build script to make this happen?
Or should I take some other approach altogether?
I'm not sure exactly how you want to pass these values, but there are several mechanisms:
Use <sysproperty> to pass system properties you need to set:
Use <arg> to pass command line arguments to your Java class
Use <jvmarg> to pass arguments to your Java command itself
If you fork your Java task, you can also set environment variables too. These are ignored if you don't fork the Java task
This:
$ foo=bar; java -Xlingc com.example.foo.bar -Dsys1=fu -Dsys2=barfu -arg1 -arg2 bar
Becomes:
<java classname="com.example.foo.bar"
fork="true">
<env key="foo" value="bar"/>
<sysproperty key="sys1" value="fu"/>
<sysproperty key="sys2" value="barfu"/>
<jvmarg value="-Xlingc"/>
<arg value="-arg1"/>
<arg value="-arg2"/>
<arg value="bar"/>
</java>
Hope that example helps
Not good in Ant Script but I do something like below :
<target name="execute">
<echo> Running MyClass ......... </echo>
<java classname="pkg.MyClass" classpathref="libs">
<arg value="val1" /> <!-- command line args -->
<arg value="val2" />
<arg value="val3" />
<env key="k1" value="v1" /> <!-- set environmental value -->
</java>
</target>
If you are using Eclipse, you will get suggestions in popup under java tag. I got few more like : <sysproperty/>, <syspropertyset></syspropertyset>, <jvmarg/>
Use the nested <arg> elements in your <java> task:
<java classname="test.Main">
<arg value="${k1}"/>
<arg value="${k2}"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
I am attempting to write unit tests against a series of RMI interfaces / implementations and am relying on Ant + Junit to facilitate these tests. The main problem is that I need to have the rmiregistry start up at the beginning of my Junit task and close down at the end of execution whether that be failure,error, or success. Ant script:
<project>
<target name="test">
<javac srcdir="Test/src" destdir="Test/bin" />
<junit fork="true" printsummary="true">
<batchtest>
<fileset dir="Test/test">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
Basically, just set haltonerror="false" and failonerror="false" for the junit task.
This way junit won't fail due to tests. Starting and stopping is done via the exec task. I used taskkill here to kill the rmiregistry.exe.
<exec executable="start"> <--your start command here -->
<arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures">
...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
<arg value="/IM"/>
<arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>
Another option is to use try/catch from ant-contrib, but I don't think this is needed here.
I have a compiler (and language) I am building that is normally invoked thus:
java -jar nc.jar \
-p some/referenced/package.nc \
-p framework.nc \
source1.ns source2.ns sourceN.ns \
-o output/package.nc
I'd like to include a task in my ANT build file that invokes the compiler to compile the standard library and all test cases, but specifying each separate compiler invocation as a <java> task is painful:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<arg path="../nframework/build/n.core.nc"/>
<arg path="../nframework/n/debug/DebugPrint.ns"/>
<arg path="../nframework/n/debug/Trace.ns"/>
<arg value="-o"/>
<arg path="../nframework/build/n.debug.nc"/>
</java>
<!-- More compile steps -->
</target>
I would like to create an ANT task of some sort that can simplify this into something like:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<nc output="../nframework/build/n.debug.nc">
<link-package path="../nframework/build/n.core.nc"/>
<src>
<fileset dir="../nframework/n/debug" includes="**/*.ns"/>
</src>
</nc>
<!-- More compile steps -->
</target>
To this end, I tried macrodef:
<macrodef name="nc">
<attribute name="output"/>
<element name="link-package"/>
<element name="src"/>
<sequential>
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<!-- This doesn't do what I want -->
<link-package/>
<!-- Neither does this -->
<src/>
<arg value="-o"/>
<arg path="#{output}"/>
</java>
</sequential>
</macrodef>
I've tried several variations on the above, but each errors out with something like:
/home/jwarner/code/nlang/nc/build.xml:55: java doesn't support nested "fileset" element.
Is there a way to do this without extending ANT itself? Alternatively, would it be fairly easy to add an ant task to my compiler? I'm not terribly picky about the syntax of the final <nc> task.
I have had a similar problem in the past where the out-of-the-box Ant tasks didn't quite do what I wanted them to do. I found that it was very easy to write my own Ant task.
The documentation is concise but does a good job of explaining what you need to do.
http://ant.apache.org/manual/develop.html#writingowntask