如何使用 ANT 目标从控制台覆盖 build.properties 值

How to override the build.properties values from console by using ANT targets

config.properties 的值如下:

username = system
password = ******

build.xml 具有如下默认属性值:

<target name="load-config-properties"
                <property file="${config.properties}"/>
                <echo>Default config params</echo>
                <property name="username" value=""/>
                <property name="password" value=""/>

现在我既不想使用 config.properties 也不想使用 build.xml properties.So 我已经编写了一些 ant 输入任务来覆盖来自控制台的 config.properties,如下所示

 <input message="Enter password for username: "                                                                     addproperty="input.password">
    <handler type="secure"/>
</input>
var name="password" value="${input.password}"/>

我可以从控制台提供密码,但它不会覆盖 config.properties。

有人可以帮我吗?

提前致谢。

如果我要这样做,请不要选择使用 input,而是选择系统参数。

这是示例构建脚本:

<project name="overrideProperty" basedir="." default="test">  
  <property name="username" value="system"/>
  <target name="test" description="check if the property can be overridden runtime">
    <echo message="Property username's value is ${username}"/>
  </target>
</project>

常规: 使用与 属性
中定义的相同的值 如果您想使用与 属性 username 中定义的值相同的值,请像平常一样调用 ant build,例如:

ant test

输出:

test:
     [echo] Property username's value is system

BUILD SUCCESSFUL
Total time: 0 seconds

使用系统参数覆盖 属性 值运行时
如果你想覆盖 username 属性 运行时(不修改构建脚本,然后调用 ant 命令如下:

ant test -Dusername="mysystem"

输出:

Buildfile: D:\Temp\build2.xml

test:
     [echo] Property username's value is mysystem

BUILD SUCCESSFUL
Total time: 0 seconds

希望这对您有所帮助。