[英]Reading property names from a properties file before loading it (ANT)
在加载文件之前,我需要从属性文件中检索所有属性的名称(使用Ant)
我将详细解释整个过程:
读取第一个属性文件(将其命名为a.properties),并将其所有属性加载为项目的属性。
#a.properties's contents myvar1=1 myvar2=someTextHere
必须在项目上加载第二个文件(例如b.properties)。 一些已经设置的属性也可以包含在第二个文件中,因此我们要做的就是使用在其上找到的值更新此类变量(通过ant-contrib
的var
目标)
#b.properties's contents myvar1=2 #updated value for a property that's is already set on the project myvar3=1,2,3,4,5,6
因此,属性/值对的预期子集(从ANT项目的属性角度来看)为:
myvar1=2 myvar2=someTextHere myvar3=1,2,3,4,5,6
我们无法更改这些文件在项目中的加载顺序 ,这是解决问题的最简单方法(因为Ant在设置属性时采用的行为)
任何反馈将不胜感激。
问候
我假设您需要在构建源代码之前从其他文件中读取属性
<target name=-init-const-properties description="read all properties required">
<propertyfile file="AbsolutePathToPropertyFile" comment="Write meaningfull
about the properties">
<entry value="${myvar1}" key="VAR1"/>
<entry value="${myvar2}" key="VAR2"/>
</propertyfile>
</target>
注意:您需要添加适当的AbsolutePathToPropertyFile
并根据需要添加注释
在目标-init-const-properties
您可以添加要读取的尽可能多的文件,并将该目标用作要使用这些属性值的从属目标。 希望这能回答您的问题
我建议为构建默认文件创建一个名为“ build.properties”的标准文件。 如果需要覆盖任何设置,请创建一个名为“ build-local.properties”的可选文件。
我的建议是使构建逻辑保持简单。 根据我的经验,很少需要使用ant-contrib扩展来使属性像变量一样起作用。
├── build-local.properties
├── build.properties
└── build.xml
运行项目将产生以下输出,其中将值“ two”替换为:
$ ant
build:
[echo] Testing one, dos, three
删除可选文件,它将恢复为默认值:
$ rm build-local.properties
$ ant
build:
[echo] Testing one, two, three
秘密是属性文件的加载顺序。 如果它们不存在,则不会创建属性。
<project name="demo" default="build">
<property file="build-local.properties"/>
<property file="build.properties"/>
<target name="build">
<echo message="hello ${myvar1}, ${myvar2}, ${myvar3}"/>
</target>
</project>
myvar1=one
myvar2=two
myvar3=three
myvar2=dos
最后,我遵循的方法是b.properties
指定第二个属性文件( b.properties
):
ant <my_target> -propertyfile b.properties
所以这对我来说很好
谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.