【问题标题】:Reading property names from a properties file before loading it (ANT)在加载之前从属性文件中读取属性名称 (ANT)
【发布时间】:2017-12-19 05:16:52
【问题描述】:

我需要在加载之前从属性文件中检索所有属性的名称(使用 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 项目的属性角度来看) 的属性/值对将是:

    myvar1=2
    myvar2=someTextHere
    myvar3=1,2,3,4,5,6
    

我们无法更改这些文件在项目中加载的顺序,这将是解决问题的最简单方法(因为 Ant 在设置属性时采用的行为)

我们将非常感谢任何反馈。

问候

【问题讨论】:

  • 请显示您拥有的相应构建脚本。
  • 我不明白为什么您不能更改加载属性文件的顺序。您说这是由于 Ant 的属性不变性,但您可以简单地利用它来发挥自己的优势(这是 Ant 的设计特性,而不是缺点)。

标签: ant properties-file


【解决方案1】:

我假设您需要在构建源代码之前从不同文件中读取属性

<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 中,您可以添加要读取的任意数量的文件,并将此目标用作您要在其中使用这些属性值的依赖目标。希望这能回答你的问题

【讨论】:

    【解决方案2】:

    我建议为构建默认设置一个名为“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
    

    build-local.properties

    myvar2=dos
    

    【讨论】:

      【解决方案3】:

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

      ant <my_target> -propertyfile b.properties
      

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

      感谢大家的帮助。

      【讨论】:

        猜你喜欢
        • 2016-09-08
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        • 2011-05-09
        • 2015-11-25
        • 2017-07-20
        • 2015-03-15
        • 1970-01-01
        相关资源
        最近更新 更多