0

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.

Juano7894
  • 703
  • 1
  • 8
  • 21
  • 1
    Stop! Do you really have three projects? If so, then use [Ivy](http://ant.apache.org/ivy/) to manage them as separate projects and ditch your parent build file. If not, then have just one build file for the three parts of your one project (http://www.build-doctor.com/2008/03/19/ant-best-practices-prefer-a-single-buildfile/) – Tom Howard Mar 22 '12 at 00:02

1 Answers1

2

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">

Alex
  • 10,470
  • 8
  • 40
  • 62
  • hmm.. I tried that but is not acurate. That command "clean build" seems not to be working on my end. – Juano7894 Mar 21 '12 at 20:27
  • do you have `clean` and `build` defined targets in the 3 build.xml files? – Alex Mar 21 '12 at 20:30
  • yes.. If I do like I said in my update of my question, then is working. Do you know a better way? – Juano7894 Mar 21 '12 at 20:58
  • 1
    You should have a separate target for cleaning. This would allow `clean` to be invoked only when needed. Otherwise a clean-build will result in a slow code-build-test cycles. http://www.build-doctor.com/2008/07/23/ant-best-practices-the-clean-test/ – Tom Howard Mar 21 '12 at 23:55
  • @TomHoward executing both `clean` and `build` targets is part of OPs question. In my answer, `cleanBuild` would depend on `clean, build` so changing to a build without cleaning would only require changing the target in the main build.xml to `build` instead of `cleanBuild` – Alex Mar 22 '12 at 13:01
  • His didnt actually state that he wanted to do a clean before *every single build*, but even if that's what he wanted, you shouldn't be condoning bad practice. Yes, answer the question, but aslo explain why it's bad and what the correct approach is. – Tom Howard Mar 22 '12 at 23:49
  • 1
    From the first revision of the question: `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`. On the subject of correctness of a clean build, while I agree that repeated (or frequent) clean builds are a bad practice, I don't think that a question about a clean build should automatically include a correction of using an incremental build only. – Alex Mar 23 '12 at 01:13
  • I have tried the above way but it is giving me Target "build" does not exist in the project "TestActivity". Do I need to anything to the build.xml in TestActivity project ? – nilMoBile Aug 09 '12 at 15:20