You are on page 1of 2

3/24/2014

java - Change default ant target by command line argument - Stack Overflow
sign up log in tour help careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Take the 2-minute tour

Change default ant target by command line argument

I'm recently assigned a task to make ant be able to build war packages for different environments. I'm almost done except one feature. The ant accepts an e n v parameter by like D e n v = D E V, and use different configuration files to make the war package. But the default target is s t a r t 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 D e n v = P R O D 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 as below: 1. a n tD e n v = D E Vwill build, deploy, and start the server 2. a n tD e n v = P R O Dwill only build the ROOT.war
java ant build-process build build-automation

edited Mar 19 '13 at 11:26 Ripon Al Wasim 3,837 6 37 71 add comment

asked Jun 21 '10 at 4:04 James Ni 28 1 7

3 Answers
I suggest that you define targets in your b u i l d . x m l file called "DEV" and "PROD" and then invoke Ant as: a n tD E V or a n tP R O D 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.)
edited Jun 21 '10 at 4:18 answered Jun 21 '10 at 4:11 Stephen C 238k 12 172 434

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 Jun 21 '10 at 5:08 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 b u i l d . x m l file, you'll need to do funky stuff with < i m p o r t > or something similar. Stephen C Jun 21 '10 at 5:35

Also - note that this is not just "changing the default target". You actually want to define the default target separately from the main b u i l d . x m l. Stephen C Jun 21 '10 at 5:40 Right. Maybe one build.xml is not enough. James Ni Jun 21 '10 at 6:05

add comment

http://stackoverflow.com/questions/3082048/change-default-ant-target-by-command-line-argument

1/2

3/24/2014

java - Change default ant target by command line argument - Stack Overflow

You could also load different property files based on the env property: < p r o p e r t yf i l e = " $ { e n v } . p r o p e r t i e s " / > and in there configure which target to call: in DEV.properties: d e f a u l t . t a r g e t = d e v . b u i l d in PROD.properties: d e f a u l t . t a r g e t = p r o d . b u i l d and then call the target based on the property:

< t a r g e tn a m e = " d e f a u l t " > < a n t c a l lt a r g e t = " $ { d e f a u l t . t a r g e t } " / > < / t a r g e t > < t a r g e tn a m e = " d e v . b u i l d " > . . . . < / t a r g e t > < t a r g e tn a m e = " p r o d . b u i l d " > . . . . < / t a r g e t > by specifying separate property files for each build type you will make it easy to configure other aspects of the build.
edited Jun 21 '10 at 4:38 answered Jun 21 '10 at 4:11 krock 12.2k 3 27 49

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 Jun 21 '10 at 5:15 add comment

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.
answered Jun 21 '10 at 4:12 Nikolaus Gradwohl 7,541 15 32 add comment

Not the answer you're looking for? Browse other questions tagged java ant
build-process build build-automation or ask your own question.

http://stackoverflow.com/questions/3082048/change-default-ant-target-by-command-line-argument

2/2

You might also like