Ant depends attribute leading to errant behavior - ant

I have two Ant tasks. If I run them, one after the other, from the command line -- no problems.
But if I include them in the depends attribute of another task like this depends='taskA,taskB' I get errant results.
Are the depends being run in series instead of in sequence? Without going into the specifics of the ant tasks -- any ideas?

ANT only executes dependent tasks once... The example below illustrates this strange behaviour.
This falls into the category of feature rather than bug. It's just the way ANT works. The thing to bear in mind is that ANT is not designed as a procedural programming language.
If your sub task is procedural in nature it would be best invoked as a macrodef (or better again create a re-usable antlib of build functions).
Example
$ ant
Buildfile: /path/to/my/home/build.xml
subtask1:
[echo] hello world
subtask2:
[echo] hello world
build1:
[echo] hello world
build2:
[echo] hello world
build:
BUILD SUCCESSFUL
build.xml
<project name="demo" default="build">
<target name="build" depends="build1,build2"/>
<target name="build1" depends="subtask1,subtask2">
<echo message="hello world"/>
</target>
<target name="build2" depends="subtask1,subtask2">
<echo message="hello world"/>
</target>
<target name="subtask1">
<echo message="hello world"/>
</target>
<target name="subtask2">
<echo message="hello world"/>
</target>
</project>

Related

Ant calling targets several times

I set up a simple Ant script to understand why my dependency targets are being called several times when I specified several targets to execute (in Eclipse):
<project name="test">
<macrodef name="mkjar" description="Build a jar from 'tgt'">
<attribute name="tgt" />
<sequential>
<echo message="mkjar #{tgt}" level="info" />
</sequential>
</macrodef>
<target name="Common">
<mkjar tgt="Common" />
</target>
<target name="Net" depends="Common">
<mkjar tgt="Net" />
</target>
<target name="DB" depends="Common">
<mkjar tgt="DB" />
</target>
<target name="FooBar" depends="Common,DB">
<mkjar tgt="FooBar" />
</target>
<target name="FooBar2" depends="Common,Net,DB">
<mkjar tgt="FooBar2" />
</target>
</project>
Output:
Buildfile: buildtest.xml
Common:
[echo] mkjar Common
Net:
[echo] mkjar Net
DB:
[echo] mkjar DB
FooBar2:
[echo] mkjar FooBar2
Common:
[echo] mkjar Common
DB:
[echo] mkjar DB
FooBar:
[echo] mkjar FooBar
BUILD SUCCESSFUL
Total time: 283 milliseconds
Why is Ant going twice into targets Common and DB? I thought macrodef would be executed inside the same Ant flow and project scope. I tried uglier things with antcall
<target name="mkjar" unless="jar-${tgt}">
<property name="jar-${tgt}" value="true" />
...
</target>
But it's not better (it's even creating several jar-<tgt> properties!).
I thought macrodef would be executed inside the same Ant flow and project scope
It does. When you invoke ant FooBar2 FooBar, both targets are executed in the same Ant project. But in this case, Ant does not "track" that a dependency target has already executed when it is also a dependency for the second target.
A dependency target is invoked only once when it is in the same chain of dependencies of only one target, for example:
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
When running ant D, the following chain of targets is run: A --> B --> C --> D.
See https://ant.apache.org/manual/targets.html for more info.
One way to solve the issue here is to add a condition on the target to skip running it when a property is set:
<target name="Common" unless="common.already.executed">
<mkjar tgt="Common" />
<property name="common.already.executed" value="true" />
</target>

How do I prevent a dependency from executing in Ant?

When debugging a build.xml file, or an Ant task, I often want to execute one task without executing its dependencies. Is there a way to do this from the command line?
For example, with this build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="A" />
<target name="B" depends="A" />
</project>
is there a command that will execute task B but not task A?
You can make execution of any target conditional on a property using if or unless.
<project default="B">
<target name="A" unless="no.a">
<echo>in A</echo>
</target>
<target name="B" depends="A" >
<echo>in B</echo>
</target>
</project>
Output with no condition specified:
$ ant
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
[echo] in A
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Output with condition specified on command line:
$ ant -Dno.a=any
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Notes:
Ant console output will show that the target was "hit" even if entry was blocked by the condition.
The if and unless conditions do not do boolean check. They just check whether the property is defined or not.
You will have to restructure your Ant script to achieve this:
<target name="B">
<if>
<isset property="some.property"/>
<then>
<antcall target="A">
</then>
</if>
<!-- execute task B here -->
</target>
If some.property is set, then it will first execute A followed by B. Otherwise, it will skip task A and execute B by itself.

Ant resourcecount , how can I count the number of commas delimited existing in property

My xml does not work:
When I run in command line
ant compile -Dmodules=a,b,c
My build file need to count how many modules in modules parameters, compile them one by one using for loop
<target name="count_modules">
<resourcecount property="count">
<tokens>
<concat>
<filterchain>
<tokenfilter>
<stringtokenizer delims=","/>
</tokenfilter>
</filterchain>
<propertyresource name="modules" />
</concat>
</tokens>
</resourcecount>
<echo message="count is ${count}" />
</target>
count will always return 1
[echo] count is 1
The propertyresource will return a single resource to the concat task which is designed to act on resources like files.
This complex piece of logic is best replaced by an in-line script.
<project name="myproject" default="count_modules">
<property name="modules" value="a,b,c"/>
<target name="count_modules">
<script language="javascript"><![CDATA[
modules = project.getProperty("modules");
project.setProperty("count", modules.split(",").length);
]]></script>
<echo message="Number of modules: ${count}"/>
</target>
</project>
Running sub-module builds
The for task is not part of core ant, it's part of an extension called ant-contrib. My advice is to use the subant task when invoking sub-module builds. The following answer has some simple and advanced examples of its use:
Ant Script to Automate the build process

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.

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