在加载属性文件之前从属性文件中读取 属性 个名称 (ANT)

Reading property names from a properties file before loading it (ANT)

我需要在加载属性文件之前从属性文件中检索所有属性的名称(使用 Ant)

我会详细解释整个过程:

  1. 读取第一个属性文件(我们将其命名为a.properties)并 它的所有属性都作为项目的属性加载。

    #a.properties's contents
    myvar1=1
    myvar2=someTextHere
    
  2. 第二个文件(比方说 b.properties)必须加载到 项目。一些已经设置的属性也可以包含在这个 第二个文件,所以我们要做的是更新这些变量 在其上找到的值(通过 ant-contribvar 目标)

    #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
    
  3. 所以预期的子集(从 ANT 项目的属性角度来看) property/value 对将是:

    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

build.xml

秘密是 属性 文件的加载顺序。如果它们不存在,则它们不会创建属性。

<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>

build.properties

myvar1=one
myvar2=two
myvar3=three

构建-local.properties

myvar2=dos

最后,我采用的方法是从命令行指定第二个属性文件(b.properties):

ant <my_target> -propertyfile b.properties

所以这对我来说很好...

感谢大家的帮助。