简体   繁体   English

通过命令行参数更改默认蚂蚁目标

[英]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.我最近被分配了一项任务,让 ant 能够为不同的环境构建战争包。 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.蚂蚁通过 like -Denv=DEV接受一个env参数,并使用不同的配置文件来制作 war 包。 But the default target is start which will build, deploy and start the tomcat.但是默认目标是start ,它将构建、部署和启动 tomcat。 I don't want ant to deploy the war neither start the server when I pass in the -Denv=PROD arg.当我传入-Denv=PROD参数时,我不希望 ant 部署战争也不启动服务器。 I only want ant to build the ROOT.war.我只想用蚂蚁来构建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 :D

Does anyone know how to change the default target according to the command line argument?有谁知道如何根据命令行参数更改默认目标? My requirements are as below:我的要求如下:

  1. ant -Denv=DEV will build, deploy, and start the server ant -Denv=DEV将构建、部署和启动服务器
  2. ant -Denv=PROD will only build the ROOT.war ant -Denv=PROD只会构建 ROOT.war

I suggest that you define targets in your build.xml file called "DEV" and "PROD" and then invoke Ant as:我建议您在名为“DEV”和“PROD”的build.xml文件中定义目标,然后将 Ant 调用为:

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.如果您想坚持使用系统属性来选择目标的当前方法,那么@krock 的答案似乎是要走的路。 (But I don't see any advantage in that approach though.) (但我认为这种方法没有任何优势。)

You could also load different property files based on the env property:您还可以根据 env 属性加载不同的属性文件:

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

and in there configure which target to call:并在那里配置要调用的目标:

in DEV.properties:在 DEV.properties 中:

default.target=dev.build

in PROD.properties:在 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.通过为每个构建类型指定单独的属性文件,您可以轻松配置构建的其他方面。

there is an "if"-Task in the ant-contrib collection. ant-contrib 集合中有一个“if”-Task。 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.如果未设置 dev 参数,您还可以定义默认行为。

OK I give up.好吧我放弃了。

after using Ruby/Rails or other morden tools, I found ant is hard to use.在使用 Ruby/Rails 或其他现代工具后,我发现 ant 很难使用。

you don't need to learn how to run native command in Ant, just write a bash script to run ant and mixed command lines, such as:您不需要学习如何在 Ant 中运行 native 命令,只需编写一个bash脚本来运行 ant 和混合命令行,例如:

# my_run.sh

ant package  # assume you have this target in build.xml
cp target.war /opt/tomcat/webapps   # copy this file to tomcat
/opt/tomcat/bin/shutdown.sh   # restart tomcat 
/opt/tomcat/bin/startup.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM