Intermodule dependency in ant - ant

I am using Ant 1.8
I have multiple modules in intelliJ IDEA. Each module has a build.xml and currently i need to browse till build.xml of that file and run ant for every module.
e.g. module B's build success depends on whether module A's build was successful.
Now, i want to update this process. It will be great if an option exists wherein i can write a single build process which will first build distribution for module A and then while building distribution for B, it will be checked if build for module A is successful.
Is there any way using current Ant mechanism. i could see something similar in ivy but i cant use it in my project.
Please suggest an approach using basic Ant features.

The subant task in ANT is the most flexible way to invoke a multi-module build, for example:
<project name="parent" default="build">
<target name="build">
<subant>
<filelist dir=".">
<file name="moduleA/build.xml"/>
<file name="moduleB/build.xml"/>
</filelist>
<target name="clean"/>
<target name="build"/>
</subant>
</target>
</project>
Project structure
|-- build.xml
|-- moduleA
| `-- build.xml
`-- moduleB
`-- build.xml
Note:
In my opinion the most powerful way to use this task is to combine it with the buildlist task from Apache ivy. Let the ivy inter-module dependency declarations automatically determine the module build order.

Thanks Mark!!
Your answer helped me a lot.
In addition to above answer I would like to add details, if properties are being loaded from properties file.
Project Structure:
|-- build.xml
|-- ProjectOne
-- build.xml
-- antbuilds.properties
|-- ProjectTwo
-- build.xml
-- antbuilds.properties
Common ANT build file:
<project name="Parent" default="all">
<target name="ProjectOne">
<subant>
<property file="ProjectOne/antbuilds.properties"/>
<filelist dir=".">
<file name="ProjectOne/build.xml"/>
</filelist>
<target name="deploy"/>
</subant>
</target>
<target name="ProjectTwo">
<subant>
<property file="ProjectTwo/antbuilds.properties"/>
<filelist dir=".">
<file name="ProjectTwo/build.xml"/>
</filelist>
<target name="deploy"/>
</subant>
</target>
<target name="all" depends="ProjectOne, ProjectTwo">
</target>

Related

Minimum config to use Ivy to set a path in an Ant build?

I want to use an Ant task that is defined in an artifact. The artifact exists in the main Maven repositories and has some dependencies.
I want to use Ivy and Ant to:
Declare the dependency on that artifact and its transitive dependencies, so that they are resolved when the script is run.
Retrieve all the jar files as an Ant path, that I can feed into Ant's taskdef.
Refer to that set of resolved jar files in some other part of the build script.
So far, the documentation I have found does not optimize for this use case. Instead, it suggests to write the files ivy.xml, ivysettings.xml; I don't like that, the dependencies are small enough that I would like to fit everything in a single build script.
Any ideas?
The ivy cachepath task is a resolve task that can be used to create an ANT path. What is perhaps not well known is that this resolving task can also be used inline, in other words, you can specify the dependencies directly without an ivy file.
<ivy:cachepath pathid="tasks.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
</ivy:cachepath>
For a related answer that utilizes an ivy file to manage multiple classpaths:
ivy:retrieve select which ivy.xml file to use
Example
The following example is a little contrived by demonstrates ivy downloading the jar associated with the groovy task. I have also included a utility target that I use to install the ivy jar as well.
build.xml
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<!--
============
Main targets
============
-->
<target name="resolve" depends="install-ivy">
<ivy:cachepath pathid="tasks.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
</ivy:cachepath>
</target>
<target name="build" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="tasks.path"/>
<groovy>
ant.echo "Hello world"
</groovy>
</target>
<!--
==================
Supporting targets
==================
-->
<target name="install-ivy" description="Install ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
<target name="clean" description="Cleanup build files">
<delete dir="${build.dir}"/>
</target>
<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
<ivy:cleancache/>
</target>
</project>

Ant Script to Automate the build process

I Want to automate the ANT build process for deploying the applicaiton.
I want to write a ANT script which will recurringly should look for the
build.xml files in the folder and run them, if the sub build is failed
it should skip and continue to other script by writing log.
Could any please post the idea which can help or a sample.
RootFolder
|
|-----Folder1
| |
| |--SubFolder1
| | build.xml
| |--SubFolder2
| | build.xml
|-----Folder2
| build.xml
|
|-----Folder3
build.xml
I'd recommend using the subant task
<project name="Subant demo" default="deploy-everything">
<target name="deploy-everything">
<subant>
<fileset dir="." includes="**/build.xml" excludes="build.xml"/>
<target name="clean"/>
<target name="deploy"/>
</subant>
</target>
</project>
This will find all "build.xml" files and call the "clean deploy" targets on each.
While it's neat to automatically pick up the sub folder builds, it rarely works in large projects unless the builds are independent of each other (build order is important).
The following example uses an explicit filelist, instead of a fileset which is unordered:
<project name="Subant demo" default="deploy-everything">
<target name="deploy-everything">
<subant>
<filelist dir=".">
<file name="Folder1/SubFolder1/build.xml"/>
<file name="Folder1/SubFolder2/build.xml"/>
..
</filelist>
<target name="clean"/>
<target name="build"/>
</subant>
</target>
</project>
Finally, the most advanced solution is to use a dependency manager like ivy to declare each module's dependencies in an "ivy.xml" file. Setup properly, this makes each sub module build more stand-alone. To solve the build "everything in only go problem" ivy provides a buildlist task that can automatically determine the correct build order:
<target name="deploy-everything">
<ivy:buildlist reference="build-path">
<fileset dir="." includes="**/build.xml" excludes="build.xml"/>
</ivy:buildlist>
<subant buildpathref="build-path">
<target name="clean"/>
<target name="build"/>
</subant>
</target>
I have solved my problem, thank you all for the reply, I used below technique to handle the situation.
<?xml version="1.0" ?>
<project name="MasterBuildPrj" default="MasterBuild">
<macrodef name="iterate">
<attribute name="target"/>
<sequential>
<subant target="#{target}">
<fileset dir="."
includes="**/build.xml"
excludes="build.xml"/>
</subant>
</sequential>
</macrodef>
<target name="MasterBuild" description="Build all sub projects">
<iterate target="build"/>
</target>
<target name="clean" description="Clean all sub projects">
<iterate target="clean"/>
</target>
</project>

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.

running specific target in different ant scripts in different directories

We have a large amount of apps. They all have a build.xml file located in the projects base directory. I am trying to create an ant script that will go through and call a specific target on each of the build.xml files in all the projects.
Here are the issues:
Some of the projects are in deeper directories than others.
Only some of the projects need to be built at a time.
I was trying to use subant + antfile and defining a CSV of file paths in a properties file, but this did not work. Below is what i have and the error i am getting.
If there is a better way to do this or you know what my problem is, please let me know! Thanks!
This is the property defined in a property file. I am wanting the person running the script to add the file paths in here that are relative to the current location of the script they are running.
projects.to.build=
This is the subant task i am trying to use in the main build script.
<filelist
id="projectNames"
dir="${basedir}"
files="${projects.to.build}"
/>
<target name="debugAll" description="Builds all the projects listed in the projectNames.properties file.">
<subant target="debug" antfile="${projects.to.build}">
</subant>
</target>
Here is the error i get when i try to run the build script when there are projects defined in the properties file. I am using the relative path. For example: ..\Apps\AnApp1\build.xml,..\Apps\AnApp2\build.xml,..\OtherApps\foo\AnotherApp1\build.xml
"No Build Path Specified" (at my subant task)
You specified the antfile attribute, so ANT was expecting to a single build.xml file.
The subant documentation describes how you can use a fileset as child parameter.
Here's an example:
<project name="Subant demo" default="run-debug-target">
<target name="run-debug-target">
<subant target="debug">
<fileset dir="." includes="**/build.xml" excludes="build.xml"/>
</subant>
</target>
</project>
Update
Alternatively a filelist could be used:
<project name="Dry run" default="run">
<target name="run">
<subant target="test">
<filelist dir="projects" files="one/build.xml,two/build.xml,three/build.xml,four/build.xml"/>
</subant>
</target>
</project>
Processing the following build files:
projects/one/build.xml
projects/two/build.xml
projects/three/build.xml
projects/four/build.xml
Is it possible to run the target in the all the build files concurrently ?
E.g.
<project name="Dry run" default="run">
<target name="run">
<subant target="test">
<filelist dir="projects" files="one/build.xml,two/build.xml,three/build.xml,four/build.xml"/>
</subant>
</target>
</project>
In this example, is there any way to run target "test" present in all the build files (one/build.xml,two/build.xml,three/build.xml,four/build.xml) concurrently ?

How can I specify the path of a JAR in an ant buildfile?

I am executing lot of scp and sshexec and other remote commands from an ant build script. These commands don't work if jsch.jar isn't in the ant lib directory. To make it work, I copied the JAR into the ant lib directory, but this is not a good solution, as anyone else wanting to run the script would have to do the same thing. To run the ant target from Teamcity, we will have to explicitly set the path of the lib file.
Is there a way I can specify the path of the JAR in the ant build XML itself?
Thanks all for your answers. I am managed to get it work with classloader task. This is what I did.
<project basedir="." >
<property environment="env"/>
<taskdef resource="net/jtools/classloadertask/antlib.xml">
<classpath>
<fileset dir="${basedir}/lib" includes="ant-classloader*.jar"/>
</classpath>
</taskdef>
<!--Add JSCH jar to the classpath-->
<classloader loader="system">
<classpath>
<fileset dir="${basedir}/lib" includes="jsch*.jar"/>
</classpath>
</classloader>
<target name="Test">
<scp todir="user1:pass1#server1:/tmp" trust="true" >
<fileset dir="dir1">
<include name="test.txt" />
</fileset>
</scp>
</target>
</project>
As you can see here, I didn't have to give any dependant target for my "Test" target, it just works. It uses classloader, which appends jsch.jar to the system classloader.
One possible work around would be to use the -lib command line option to tell ant where to look for additional jars. Perhaps you could create a wrapper script that calls ant with this option set.
Another way would be to move the ant-jsch.jar file (this is the jar that comes with ant that defines the tasks, not the jsch.jar file you need to download separately) out of your ant lib directory, and create a taskdef for your ssh task separate to the built in one, then set the classpath for this task to the jsch.jar and the ant-jsch.jar:
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
<classpath>
<pathelement location="jsch-0.1.44.jar"/>
<pathelement location="ant-jsch.jar" />
</classpath>
</taskdef>
I'm not sure this will help you though, since it also involves making changes to the lib directory.
As far as I'm aware, it's not currently possible to specify the extra jars required for the built in tasks in the build file itself in general. There are some special cases, like junit for instance.
To ensure your build is more cross platform I'd suggest using dependency management. The ivy plug-in can automatically install the version of your build's plugin at build-time.
This approach means the last jar you'll ever need to install into your ANT lib is ivy-2.2.0.jar :-)
First declare your project's dependencies in the file ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="anttask" description="Jars implementing ANT tasks"/>
</configurations>
<dependencies>
<dependency org="com.jcraft" name="jsch" rev="0.1.42" conf="anttask->default"/>
</dependencies>
</ivy-module>
Within your build.xml run ivy and use it to populate a custom classpath based on the ivy configuration:
<target name='init' description='Resolve project dependencies and set classpaths'>
<ivy:resolve/>
<ivy:cachepath pathid="anttask.path" conf="anttask"/>
</target>
Finally, elsewhere in your build declare your ANT tasks using the class path now automatically populated by ivy.
<target name='dosomething' depends="init">
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="anttask.path"/>
..
..
</target>
This approach works for all ANT plug-ins, most of which are available in the central Maven repository. The second benefit is that it's easy to upgrade the plug-in versions across all builds.

Resources