【问题标题】:How to load Ant properties from property files on the command line?如何在命令行上从属性文件加载 Ant 属性?
【发布时间】:2012-07-06 01:14:45
【问题描述】:

我有两个属性文件 [one.propertiestwo.properties]。我想从命令行将属性文件动态加载到我的 Ant 项目中。

我的构建文件名为 build.xml。

命令行:

> ant build [How do I pass the property file names here?]

【问题讨论】:

  • 您要创建文件还是要加载文件?
  • 我想使用 ant build 运行属性文件。我创建了两个属性文件,但不知道如何从 cmd 运行。我现在正在运行-> ant build。这将运行 build.xml 文件和我在构建文件中提到的属性文件,但是我有多个属性文件我需要在 ant 命令构建中动态传递属性文件名。

标签: ant properties build


【解决方案1】:

Loading property files from the command line

ant -propertyfile one.properties -propertyfile two.properties 

可以使用-D 标志在命令行上定义各个属性:

ant -Dmy.property=42


从 Ant 项目中加载属性文件

LoadProperties Ant task

<loadproperties srcfile="one.properties" />
<loadproperties srcfile="two.properties" />

Property Ant task

<property file="one.properties" />
<property file="two.properties" />

Match property files using a pattern

JB Nizet's 解决方案结合了concatfileset

<target name="init" description="Initialize the project.">
  <mkdir dir="temp" />
  <concat destfile="temp/combined.properties" fixlastline="true">
    <fileset dir="." includes="*.properties" />
  </concat>
  <property file="temp/combined.properties" />
</target>

【讨论】:

  • 感谢您的回复。我觉得您误解了我的问题。我需要通过'cmd'运行并且需要动态传递属性文件名。请指教!
  • @Muthukumar,我扩展了我的答案以显示如何在命令行上传递属性文件的名称[在命令行上指定属性文件]。
  • 在 windows 中您可能需要添加引号,“-Dmy.property=42”
【解决方案2】:

进行构建condition,这样如果为构建提供了必需的系统参数,则只允许下一个目标,否则构建会失败。

 Pass CMD: ant -DclientName=Name1 -Dtarget.profile.evn=dev
 Fail CMD: ant
<project name="MyProject" default="myTarget" basedir=".">
    <target name="checkParams">
        <condition property="isReqParamsProvided">
            <and>
                <isset property="clientName" /> <!-- if provide read latest else read form property tag -->
                <length string="${clientName}" when="greater" length="0" />
                <isset property="target.profile.evn" /> <!-- mvn clean install -Pdev -->
                <length string="${target.profile.evn}" when="greater" length="0" />
            </and>
        </condition>
        <echo>Runtime Sytem Properties:</echo>
        <echo>client              = ${clientName}</echo>
        <echo>target.profile.evn  = ${target.profile.evn}</echo>
        <echo>isReqParamsProvided = ${isReqParamsProvided}</echo>
        <echo>Java/JVM version: ${ant.java.version}</echo> 
    </target>

    <target name="failOn_InSufficentParams" depends="checkParams" unless="isReqParamsProvided">
        <fail>Invalid params for provided for Build.</fail>
    </target>

    <target name="myTarget" depends="failOn_InSufficentParams">
        <echo>Build Success.</echo>
    </target>
</project>

@另见:Replace all tokens form file

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 2012-08-04
    • 1970-01-01
    • 2016-09-08
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2014-03-01
    相关资源
    最近更新 更多