如何从命令行上的属性文件加载Ant属性?

时间:2012-07-05 15:29:42

标签: ant properties build

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

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

命令行:

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

2 个答案:

答案 0 :(得分:21)

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>

答案 1 :(得分:0)

进行构建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