Change default ant target by command line argument

I have recently been assigned the task of making ant able to create military packages for different environments. I am almost finished except for one function.

ant takes a parameter envlike -Denv=DEV, and uses different configuration files to create a war package. But the default goal is to startcreate, deploy, and run tomcat. I do not want ant to deploy the war did not start the server when I passed in arg -Denv=PROD. I only want ant to build ROOT.war. This is enough.

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

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

  • ant -Denv=DEV will create, deploy and run the server
  • ant -Denv=PROD will only build ROOT.war
+2
source share
3 answers

I suggest you define the targets in your file build.xmlcalled "DEV" and "PROD", and then call Ant as:

ant DEV

or

ant PROD

If you want to stick with your current approach to using a system property to select a target, then @krock's answer seems to be a way. (But I see no advantage in this approach.)

+4
source

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

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

and there, configure what purpose 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>

, .

+2

the ant -contrib collection contains an "if" -Task. Using this, you can define a default task that checks your parameter and invokes the required tasks. You can also define default behavior if the dev parameter is not specified.

0
source

Source: https://habr.com/ru/post/1539389/


All Articles