Default value for a property in Ant - java

I have an Ant target which is triggering a Java class and few properties are set as part of this target.
<target name='test-target'>
<java fork="true" classname="MyClass" failonerror="true">
<sysproperty key="numOfEntries" value="${numOfEntries}" />
</java>
</target>
If the user is not using -DnumOfEntries when running Ant, the value is being set to the literal string ${numOfEntries}. How can I set a default value for the numOfEntries property in case user is not passing it?

I am able to solve this problem:
<property name="numOfEntries" value=10/>
<target name='test-target'>
<java fork="true" classname="MyClass" failonerror="true">
<sysproperty key="numOfEntries" value="${numOfEntries}" />
</java>
</target>
If user is not setting numOfEntries default value 10 will be used.

Related

ant jdb return immediately from a task

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.

Setting sys properties in ant taskdef

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.

Using <java> tag in Apach Ant, can I run both Server and the ProxyServer class

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>

Passing command line arguments to Java via ant build script

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>

How to set the working directory for a <junit> task to something other than the basedir?

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}"/>
....

Categories

Resources