views:

44

answers:

3

I'm recently assigned a task to make ant be able to build war packages for different environments.

And I'm almost done. Except one feature.

The ant accepts an env parameter by like -Denv=DEV, and use different configuration files to make the war package. But the default target is start which will build, deploy and start the tomcat. I don't want ant to deploy the war neither start the server when I pass in the -Denv=PROD arg. I only want ant to build the ROOT.war. It's enough.

I know I can just type one more word to achieve this goal, but you know we are all lazy. :D

Does anyone know how to change the default target according to the command line argument? My requirements are:

  1. ant -Denv=DEV will build, deploy, and start the server
  2. ant -Denv=PROD will only build the ROOT.war
+1  A: 

You could also load different property files based on the env property:

<property file="${env}.properties"/>

and in there configure which target to call:

in DEV.properties:

default.target=dev.build

in PROD.properties:

default.target=prod.build

and then call the target based on the property:

<target name="default">
    <antcall target="${default.target}"/>
</target>

<target name="dev.build">
    ....
</target>

<target name="prod.build">
    ....
</target>

by specifying separate property files for each build type you will make it easy to configure other aspects of the build.

krock
Though Stephen C suggest your solution as an alternative, I think this is similar with his solution. I still need to define different targets for those ENVs.
James Ni
+1  A: 

I suggest that you define targets in your build.xml file called "DEV" and "PROD" and then invoke Ant as:

ant DEV

or

ant PROD

If you want to stick with your current approach of using a system property to select the target, then @krock's answer seems to be the way to go. (But I don't see any advantage in that approach though.)

Stephen C
Actually, this is what I did in my previous version. But my TL says we may have more ENVs later, like QA and test. So, we'd better not define them in the build.xml.
James Ni
Hmmm ... well I don't see how (just) using a system property will do what you want. If you want to be able to build for "envs" that are not known to the `build.xml` file, you'll need to do funky stuff with `<import>` or something similar.
Stephen C
Also - note that this is not just "changing the default target". You actually want to *define* the default target separately from the main `build.xml`.
Stephen C
Right. Maybe one build.xml is not enough.
James Ni
A: 

there is an "if"-Task in the ant-contrib collection. Using this you can define a default task, that tests for your parameter and calls the required tasks. You could also define a default behaviour if the dev param is not set.

Nikolaus Gradwohl