How to specify lib directory for ant task? - ant

I'm currently running an exec task in my build like this:
<target name="bar">
<exec executable="ant">
<arg value="-f"/>
<arg value="/path/to/my/build.xml"/>
<arg value="-lib"/>
<arg value="/path/to/my/libs"/>
</exec>
</target>
I don't really like it and want to replace the exec task with an ant task:
<target name="bar">
<ant antfile="/path/to/my/build.xml"/>
</target>
However, I don't know how to specify the lib directory in this case. Is this possible somehow?

What are you trying to achieve, by launching ANT from within ANT in this manner?
For example if you need custom ANT extensions, the path to these jars can be specified at runtime within the code as follows:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${lib.dir}/ant-contrib-0.3.jar"/>
</classpath>
</taskdef>
Better again, you could integrate a dependency management system like Apache ivy to manage 3rd party jar dependencies.

You can call ant script inside an ant script like below.
If you use the attribute inheritrefs="true" any Ids that are set in the parent build script will be passed to child build script as well.
Eg:
<ant antfile="subbuild.xml" inheritrefs="true"/>

Related

Writing Ant build file for several Jenkins jobs

I'm using Jenkins with Ant plug-in to run PHPUnit/Selenium tests. I'm trying to set up several Jenkins jobs (I've only had one job previously).
Tests for these jobs are in the same GitHub repo, but different folders.
So, I could create different Ant targets in my build.xml, but do I need
separate phpunit.xml files for each job (and if so, how do I specify file names in Ant build script?) Or is there a way to make Ant
distinguish between tests in the same phpunit.xml file? Any other good way to go about this? Any examples would be appreciated.
Ant build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build">
<target name="build" depends="clean,prepare,phpunit"/>
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/build"/>
</target>
<target name="prepare" description="Make log and coverage directories">
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/coverage_selenium"/>
</target>
<target name="phpunit" description="MyTests">
<exec dir="${basedir}" executable="phpunit" failonerror="true"/>
</target>
</project>
phpunit.xml:
<phpunit>
<testsuites>
<testsuite name="MyTests">
<file>path/to/test.php</file>
</testsuite>
</testsuites>
</phpunit>
Thanks!
You can specify the test configuration file using -c or --configuration. The Ant exec task lets you specify arguments for the process you want to run, something like:
<exec dir="${basedir}" executable="phpunit" failonerror="true">
<arg value="-c" />
<arg value="php_unit_1.xml"/>
<exec>
I recommend creating a separate build.xml and phpunit.xml for each project. You can define common targets in a central build-base.xml that you include in each to avoid duplication. Unfortunately, there's no equivalent mechanism for phpunit.xml that I know of.

Is there a way to call the ant 'ant' target with '-lib' option

I'm developing an ant script which is calling another ant script using the <ant> task. This ant script is an installer a Java product and is to be used by our customers, who will have ant installed separately.
The script being called uses the antlr task <antlr:ant-antlr3>. To do this I must place the ant-antlr3.jar file in the ant lib directory, as well as adding antlr-3.2.jar to the classpath.
But I don't want to have this dependency of having ant-antl3.jar file in the client's own installed version of ant.
Is there a way of providing the equivalent to ant's command-line '-lib' option to specify other paths for jars to be added to antlib using the <ant> task itself?
I've taken a look at the online docs and there doesn't seem to be a way.
Thanks
I believe the accepted way to do this is to manually set up your classpath in the build file rather than implicitly including it via the global ant lib directory. i.e.
<path id="master-classpath">
<fileset dir="${lib}" />
<fileset file="${findbugs-base}/lib/annotations.jar" />
<pathelement location="${build-classes}" />
</path>
You can then use this path element in any task that can accept classpath args such as javac
<javac
destdir="${out}"
source="1.5"
target="1.5"
debug="true">
<src path="${src}" />
<classpath refid="master-classpath" />
</javac>
This way, the global ant set up isn't a dependency, and you can specify any files you might need for any build, as specifically as you need to (down to a given call or target).
Obviously, this is all to be carried out in the build file you're calling from the clients' build file. This way, when you call out to yours, the classpath will be set up exactly as you desire.
Another far less idiomatic possibility would be to literally shell out with the Exec Task and call ant that way. Obviously, with the provision of the Ant task, the developers of ant don't recommend you doing that. It is an option, nonetheless.
Tim's answer gives most of the story, but in order to run Ant and set JVM options, you'd need to invoke it via the java task.
There is an example of running this way in the Ant docs, here slightly modified to include -lib:
<java
classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="${sub.builddir}"
timeout="4000000"
taskname="startAnt"
>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-lib"/>
<arg value="${path.to.your.antlr.jar}"/>
<arg value="-buildfile"/>
<arg file="${sub.buildfile}"/>
<arg value="${sub.target}"/>
</java>

ant java jar classpath problem

<target name="results">
<echo message="Calculating QI" />
<java jar="jmt.jar" fork="true" failonerror="true" maxmemory="1024m" classpath="jmt/jmt">
<arg value="-name:KIS"/>
<arg value="-results:CONSOLE"/>
<arg value="../allJavas.jar"/>
</java>
</target>
i want from folder tmp run jar file in folder jmt/jmt. It must be run inside jmt/jmt folder becouse of dependencies files.
i can run it like <java jar="jmt/jmt/jmt.jar" but then dependencies files are not ok. I try to use classpath but not working. What i am doing wrong?
Use the dir="jmt/jmt" attribute to specify the folder to launch the java process in, and use jar="jmt/jmt/jmt.jar" to specify the jar. You probably don't need to classpath attribute at all.
See http://ant.apache.org/manual/Tasks/java.html
The java ant task takes an option parameter dir="jmt/jmt" that will tell the forked VM where to execute.

How to execute the JAXB compiler from ANT

I am using JAXB on a project. the attraction of JAXB is that it is bundled with the JDK, I have been to use xjc.exe on the command line to generate the .java files from a schema. I can't seem to find the JAXB ant task, sure there is a download at http://jaxb.java.net however i want to use the JAXB that is bundled into the JDK is there some way to call JAXB from ant what class does the xjc.exe call on?
<target name="generate-jaxb-code">
<java classname="com.sun.tools.internal.xjc.XJCFacade">
<arg value="-p" />
<arg value="com.example"/>
<arg value="xsd/sample.xsd" />
</java>
</target>
Just went hunting in the tools.jar and found the XJCFacade.class in com.sun.tools.internal tested the above code it works it produces the output as xjc.exe It seems that XJC.exe calls this code com.sun.tools.internal.xjc.XJCFacade
One of my key requirements was that the ant file had work within eclipse without having to include a path name to the JDK that way the file would be portable across operating systems. I am assuming that tools.jar is included on the classpath via the installed JRE preferences options.
Here is a helpful link:
https://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html
Java SE 6 does not ship the Ant task (see 7.1.3):
https://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html
Essentially they do the following:
<target name="xjc" description="....">
<exec executable="${jdk.dir}/bin/xjc.exe">
<arg value="-d"/>
<arg value="${src.dir}"/>
<arg value="-p"/>
<arg value="com.mydomain.jaxb"/>
<arg value="${etc.dir}/myschema.xsd"/>
</exec>
</target>

How do I pass an argument to an Ant task?

I'm not very good with Ant, but we're using it as a build tool. Right now, we can run "ant test" and it'll run through all the unit tests.
However, I'd love to be able to do something like ant test some_module and have it accept some_module as a parameter, and only test that.
I haven't been able to find how to pass command line args to Ant - any ideas?
One solution might be as follows. (I have a project that does this.)
Have a separate target similar to test with a fileset that restricts the test to one class only. Then pass the name of that class using -D at the ant command line:
ant -Dtest.module=MyClassUnderTest single_test
In the build.xml (highly reduced):
<target name="single_test" depends="compile" description="Run one unit test">
<junit>
<batchtest>
<fileset dir="${test.dir}" includes="**/${test.module}.class" />
</batchtest>
</junit>
</target>
You can also define a property with an optional default value that can be replaced via command line, e.g.
<target name="test">
<property name="moduleName" value="default-module" />
<echo message="Testing Module: ${moduleName}"/>
....
</target>
and run it as:
ant test -DmoduleName=ModuleX
What about using some conditional in your test target and the specifying -Dcondition=true?
<target name="test" depends="_test, _test_if_true>
...
</target>
<target name="_test_if_true" if="condition">
...
</target>
<target name="_test" unless="condition">
...
</target>
Adapted a bit from the ant faq.
You can define a property on commandline when invoking ant:
ant -Dtest.module=mymodulename
Then you can use it as any other ant property:
...
<fileset dir="${test.dir}" includes="**/${test.module}.class" />
...
Have a look at Ant's manual.
I tried the solutions posted here for the very same original question. Yes just use ant -D<arg_name>. THe -D is a "keyword" I guess. I'm no ant expert and have not read the manuals in detail. Then inside the ant XML files can be accessed like: ${arg_name}
For instance you can have an argument name like: arg.myarg, so in XML ${arg.myarg}.
Ant really doesn't have parameters_ for the build file. I can think of a few ways to do this:
Use a special target to specify the tests. You can use the <for/> task from AntContrib to allow you to specify multiple tests. You'll need to download the Ant-Contrib jar file. I recommend placing it inside your project under the `${basedir}/antlib/antcontrib" directory. That way, when others checkout your project, they get the needed Ant-Contrib jar file.
<property name="antlib.dir" value="${basedir}/antlib"/>
<property name="antcontrib.dir" value="${antlib}/antcontrib"/>
<!-- Set up the ant contrib tasks for your use -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${antcontrib.dir}"/>
</classpath>
</taskdef>
<target name="select-test"
description="Select the tests to run"
depends="test-compile"
if="junit-tests">
<for parameter="module"
list="${junit-tests}"
delimiter=" ">
<sequential>
<junit
fork="true"
...>
<batchtest todir="$target/unit-tests">
<fileset dir="${test.destdir}">
<include name="**/#{module}.class"/>
</fileset>
</junit>
</sequential>
</for>
</target>
You cab now run multiple tests like this:
$ ant -D"test-one test-two test-three" select-test
You could try this to access one target at a time. Add these lines to your build.xml file :
<project name="whatever" default="default">
<input message="Please select module:" addproperty="mod" />
<target name="default" depends="${mod}/>
...
</project>
This allows you to enter the module you want to execute and execute that itself instead of running the whole build.xml
You might need to make a few more changes to your build.xml for this to work perfectly.
For the arguments , there is Facility called property. You need to set the property. As in ANT plain arguments is taken as target name.
Lest say you have two modules in your project ModuleX and ModuleY where ModuleX has 2 testcases to run and ModuleY with 10 testcases.
You could do something like this :
ant runTestsOnModule -Dtestmodule="ModuleX"
OR to test all modules by calling
ant tests
<target name="runTestsOnModule">
<antCall target="testcase${testmodule}"/>
</target>'
<! -- run single module -->
<target name="runTestsOnModule">
<antCall target="testcase${testmodule}"/>
</target>
<!--run all tests-->
<target name="tests">
<antcall target="testcaseModuleX">
<antcall target="testCaseModuleY">
</target>
<target name="testcaseModuleX">
..run junit task to call 2 testcase
</target>
<target name="testcaseModuleY">
....run junit task to call 10 testcase
</target>

Resources