Child build failure not making parent build fail in Ant - ant

I am calling child build.xml from parent build.xml by using subant:
<target name="deploy-vc-orgstr">
<subant target="deploy" failonerror="true">
<fileset dir="${orgstr.vc.home}" includes="build.xml"/>
</subant>
</target>
When I get compilation errors from child build.xml, my parent build still shows BUILD SUCCESSFUL. In logs I can see compilation errors while building target "deploy-vc-orgstr".
How to make parent build fail on child build failure?
Actually I have two targets defined.
target: deploy-vc-orgstr
target: deploy
I want if target "deploy-vc-orgstr" get fails then target "deploy" should also get fail.
My build.xml is:
<?xml version="1.0" encoding="windows-1252" ?>
<!--Ant buildfile generated by Oracle JDeveloper-->
<!--Generated Nov 1, 2012 5:00:36 PM-->
<project name="SafetyExcellenceManagementSystem" default="deploy" basedir=".">
<property file="build.properties"/>
<!--Change to include model build Starts -->
<target name="deploy-vc-orgstr">
<subant target="deploy" failonerror="true">
<fileset dir="${orgstr.vc.home}" includes="*/build.xml"/>
</subant>
</target>
<target name="init">
<tstamp/>
<mkdir dir="${output.dir}"/>
</target>
<target name="deploy" description="Deploy JDeveloper profiles"
depends="init,deploy-vc-orgstr">
<taskdef name="ojdeploy"
classname="oracle.jdeveloper.deploy.ant.OJDeployAntTask"
uri="oraclelib:OJDeployAntTask"
classpath="${oracle.jdeveloper.ant.library}"/>
<ora:ojdeploy xmlns:ora="oraclelib:OJDeployAntTask"
executable="${oracle.jdeveloper.ojdeploy.path}"
ora:buildscript="${oracle.jdeveloper.deploy.dir}/ojdeploy-build.xml"
ora:statuslog="${oracle.jdeveloper.deploy.dir}/ojdeploy-statuslog.xml">
<ora:deploy>
<ora:parameter name="workspace"
value="${oracle.jdeveloper.workspace.path}"/>
<ora:parameter name="profile"
value="${oracle.jdeveloper.deploy.profile.name}"/>
<!--ora:parameter name="nocompile" value="true"/-->
<ora:parameter name="outputfile"
value="${oracle.jdeveloper.deploy.outputfile}"/>
</ora:deploy>
</ora:ojdeploy>
</target>
</project>

Related

How can I depend on a parent target from a nested ant project?

Given a build.xml file:
<project name="main" default="build">
<target name="main.init"/>
<extension-point name="init.tasks" depends="main.init"/>
<target name="init" depends="init.tasks"/>
<extension-point name="build.tasks" depends="init"/>
<target name="build" depends="build.tasks"/>
<include file="subsystem1.xml"/>
<include file="subsystem2.xml"/>
</project>
And a subsystemX.xml with something like:
<project name="subsystemX">
<task name="init" extensionOf="init.tasks"/>
<task name="build" depends="init" extensionOf="build.tasks"/>
</project>
I can now say ant build and all subsystems will go through init and build as expected.
However I would also like to be able to say ant subsystem.build and build only that subsystem. The problem is that each subsystem's build depends on all other subsystems' init. In this case however only the subsystem.init will be executed, as ant just looks at subsystem.build's dependencies.
Ideally I would like to add the main init as a dependency to each subsystem's build task, but include blindly prepends "subsystem." to every task name and depends entry so that I don't seem to be able to reference anything outside.
How can I make subsystem.build depend on the main init task?
I'm using a workaround:
In build.xml, I add additional targets for each included file:
<target name="subsystem1.global.init" depends="main.init"/>
<target name="subsystem1.all.init" depends="init"/>
<include file="subsystem1.xml" as="subsystem1"/>
<target name="subststem2.global.init" depends="main.init"/>
<target name="subsystem2.all.init" depends="init"/>
<include file="subsystem2.xml" as="subsystem2"/>
Then in subsystemX.xml, I can depend on "global.init" and "all.init", e.g.
<project name="subsystemX">
<task name="init" depends="global.init" extensionOf="init.tasks" />
<task name="build" depends="init, all.init" extensionOf="build.tasks"/>
</project>
As you can see this does not scale well and I'm still hoping to find a better solution.

Target Clean does not exist in the project dir

I have installed Ant in my centos 6.3 , installed location are
/opt/ant and also ANT_HOME env are same
I have created build.xml to test by deleting testdir. This directory exist in the /opt/ant/testdir like this.
build.xml
<?xml version="1.0"?>
<project name="testdir" default="all" basedir=".">
<property name="src" value="src"/>
<property name="build" value="build"/>
<property name="lib" value="lib"/>
<target name="all" depends="clean, compile" description="Builds the whole project">
<echo>Doing all</echo>
</target>
<target name="clean">
<echo message="Deleting bin/java ..." />
<delete dir="testdir/test" />
</target>
</project>
Using Command :-
ant -buildfile build.xml Clean
getting error:-
BUILD FAILED
Target "Clean" does not exist in the project "testdir".
Any suggestion to make it work?
You mis-spelt the target name ? 'Clean' as against 'clean' ??
I have found solution. I missed target="compile" block in build.xml.
<target name="compile">
<echo message="Compiling source code"/>
</target>
Run command :-
ant clean

How to execute Ant build in command line

I have an Ant build file, and I try to execute it in the command line with the following command:
$ C:\Program Files (x86)\.....>ant -f C:\Silk4J\Automation\iControlSilk4J\build.xml
But nothing happens, and the result is:
BUILD SUCCESSFUL
Total time: 0 seconds
My environment variable is correct.
What is the problem? Here is my build file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="iControlSilk4J">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../Program Files (x86)/Silk/SilkTest/eclipse"/>
<property name="junit.output.dir" value="junit"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="Silk Test JTF 13.5.0 Library.libraryclasspath">
<pathelement location="../../../Program Files (x86)/Silk/SilkTest/ng/JTF/silktest-jtf-nodeps.jar"/>
</path>
<path id="JUnit 4.libraryclasspath">
<pathelement location="${ECLIPSE_HOME}/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar"/>
<pathelement location="${ECLIPSE_HOME}/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar"/>
</path>
<path id="iControlSilk4J.classpath">
<pathelement location="bin"/>
<pathelement location="lib/apache-log4j.jar"/>
<pathelement location="lib/commons-io-2.4.jar"/>
<pathelement location="lib/commons-lang3-3.1.jar"/>
<pathelement location="lib/junit.jar"/>
<pathelement location="lib/org.hamcrest.core_1.1.0.v20090501071000.jar"/>
<pathelement location="lib/silktest-jtf-nodeps.jar"/>
<path refid="Silk Test JTF 13.5.0 Library.libraryclasspath"/>
<path refid="JUnit 4.libraryclasspath"/>
<pathelement location="../../../Users/Admin/Desktop/java-mail-1.4.4.jar"/>
<pathelement location="../../../Users/Admin/Desktop/javax.activation.jar"/>
<pathelement location="lib/joda-time-2.3.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="iControlSilk4J.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
...
Go to the Ant website and download. This way, you have a copy of Ant outside of Eclipse. I recommend to put it under the C:\ant directory. This way, it doesn't have any spaces in the directory names. In your System Control Panel, set the Environment Variable ANT_HOME to this directory, then pre-pend to the System PATHvariable, %ANT_HOME%\bin. This way, you don't have to put in the whole directory name.
Assuming you did the above, try this:
C:\> cd \Silk4J\Automation\iControlSilk4J
C:\Silk4J\Automation\iControlSilk4J> ant -d build
This will do several things:
It will eliminate the possibility that the problem is with Eclipe's version of Ant.
It is way easier to type
Since you're executing the build.xml in the directory where it exists, you don't end up with the possibility that your Ant build can't locate a particular directory.
The -d will print out a lot of output, so you might want to capture it, or set your terminal buffer to something like 99999, and run cls first to clear out the buffer. This way, you'll capture all of the output from the beginning in the terminal buffer.
Let's see how Ant should be executing. You didn't specify any targets to execute, so Ant should be taking the default build target. Here it is:
<target depends="build-subprojects,build-project" name="build"/>
The build target does nothing itself. However, it depends upon two other targets, so these will be called first:
The first target is build-subprojects:
<target name="build-subprojects"/>
This does nothing at all. It doesn't even have a dependency.
The next target specified is build-project does have code:
<target depends="init" name="build-project">
This target does contain tasks, and some dependent targets. Before build-project executes, it will first run the init target:
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
This target creates a directory called bin, then copies all files under the src tree with the suffix *.java over to the bin directory. The includeemptydirs mean that directories without non-java code will not be created.
Ant uses a scheme to do minimal work. For example, if the bin directory is created, the <mkdir/> task is not executed. Also, if a file was previously copied, or there are no non-Java files in your src directory tree, the <copy/> task won't run. However, the init target will still be executed.
Next, we go back to our previous build-project target:
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="iControlSilk4J.classpath"/>
</javac>
</target>
Look at this line:
<echo message="${ant.project.name}: ${ant.file}"/>
That should have always executed. Did your output print:
[echo] iControlSilk4J: C:\Silk4J\Automation\iControlSilk4J\build.xml
Maybe you didn't realize that was from your build.
After that, it runs the <javac/> task. That is, if there's any files to actually compile. Again, Ant tries to avoid work it doesn't have to do. If all of the *.java files have previously been compiled, the <javac/> task won't execute.
And, that's the end of the build. Your build might not have done anything simply because there was nothing to do. You can try running the clean task, and then build:
C:\Silk4J\Automation\iControlSilk4J> ant -d clean build
However, Ant usually prints the target being executed. You should have seen this:
init:
build-subprojects:
build-projects:
[echo] iControlSilk4J: C:\Silk4J\Automation\iControlSilk4J\build.xml
build:
Build Successful
Note that the targets are all printed out in order they're executed, and the tasks are printed out as they are executed. However, if there's nothing to compile, or nothing to copy, then you won't see these tasks being executed. Does this look like your output? If so, it could be there's nothing to do.
If the bin directory already exists, <mkdir/> isn't going to execute.
If there are no non-Java files in src, or they have already been copied into bin, the <copy/> task won't execute.
If there are no Java file in your src directory, or they have already been compiled, the <java/> task won't run.
If you look at the output from the -d debug, you'll see Ant looking at a task, then explaining why a particular task wasn't executed. Plus, the debug option will explain how Ant decides what tasks to execute.
See if that helps.
Try running all targets individually to check that all are running correct
run ant target name to run a target individually
e.g. ant build-project
Also the default target you specified is
project basedir="." default="build" name="iControlSilk4J"
This will only execute build-subprojects,build-project and init
is it still actual?
As I can see you wrote <target depends="build-subprojects,build-project" name="build"/>, then you wrote <target name="build-subprojects"/> (it does nothing). Could it be a reason?
Does this <echo message="${ant.project.name}: ${ant.file}"/> print appropriate message? If no then target is not running.
Take a look at the next link http://www.sqaforums.com/showflat.php?Number=623277

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>

build multiple projects and clean multiple projects in a single build file with ANT

I have a doubt, I made this build file in order to build 3 different projects
<?xml version="1.0" encoding="UTF-8"?>
<project name="Trinity" basedir="." default="buildall">
<target name="project1">
<ant dir="C:/work/project1"/>
</target>
<target name="project2" depends="project1">
<ant dir="C:/work/project2"/>
</target>
<target name="project3" depends="project1, project2">
<ant dir="C:/work/project3"/>
</target>
<target name="buildall" depends="project3"/>
</project>
This is working now. But I wan to also clean the project before doing the build.
In fact I want to acomplish this:
C:/work/project1 ant clean build
C:/work/project2 ant clean build
C:/work/project3 ant clean build
Thanks in advance.
update: Thanks to the quick response from Alex I did a new build.xml file with the following. And I believe is working well, what do you think?.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Trinity" basedir="." default="buildall">
<target name="project1">
<ant dir="C:/work/project1" target="clean"/>
<ant dir="C:/work/project1" target="build"/>
</target>
<target name="project2" depends="project1">
<ant dir="C:/work/project2" target="clean"/>
<ant dir="C:/work/project2" target="build"/>
</target>
<target name="project3" depends="project1, project2">
<ant dir="C:/work/project3" target="clean"/>
<ant dir="C:/work/project3" target="build"/>
</target>
<target name="buildall" depends="project3"/>
</project>
Thanks.
According to the ant task, you can specify the targets of the external ant build files
<ant dir="C:/work/project1" target="clean build">
Edit:
According to the ant documentation:
You can specify multiple targets using nested elements instead of using the target attribute. These will be executed as if Ant had been invoked with a single target whose dependencies are the targets so specified, in the order specified.
So you can list out multiple targets this way:
<ant dir="C:/work/project1">
<target name="clean" />
<target name="build" />
</ant>
Alternatively you can define a new target in the Project1,2,3 build.xml files called cleanBuild which will in turn call clean followed by build if you want to keep it as a single xml element <ant dir="C:/work/project1" target="cleanBuild">

Resources