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

问题描述 投票:11回答:2

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

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

命令行:

> ant build [How do I pass the property file names here?]
ant properties build
2个回答
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>

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

© www.soinside.com 2019 - 2024. All rights reserved.