代码之家  ›  专栏  ›  技术社区  ›  WildJoe

有没有办法在NAnt中动态加载属性文件?

  •  20
  • WildJoe  · 技术社区  · 16 年前

    我想基于一个变量加载不同的属性文件。

    4 回复  |  直到 16 年前
        1
  •  26
  •   scott.caligan    16 年前

    <property name="environment" value="local" />
    

    步骤2 :如果您还没有所有目标都依赖的配置或初始化目标,请创建一个配置目标,并确保其他目标依赖它。

    <target name="config">
        <!-- configuration logic goes here -->
    </target>
    
    <target name="buildmyproject" depends="config">
        <!-- this target builds your project, but runs the config target first -->
    </target>
    

    步骤3 :更新配置目标以基于环境属性拉入适当的属性文件。

    <target name="config">
        <property name="configFile" value="${environment}.config.xml" />
        <if test="${file::exists(configFile)}">
            <echo message="Loading ${configFile}..." />
            <include buildfile="${configFile}" />
        </if>
        <if test="${not file::exists(configFile) and environment != 'local'}">
            <fail message="Configuration file '${configFile}' could not be found." />
        </if>
    </target>
    

    注意,我喜欢允许团队成员定义他们自己的本地.config.xml文件,这些文件不会提交给源代码管理。这为存储本地连接字符串或其他本地环境设置提供了一个很好的位置。

    步骤4 :在调用NAnt时设置环境属性,例如:

    • nant-D:environment=dev
    • nant-D:环境=测试
    • nant-D:环境=生产
        2
  •  5
  •   Tim    16 年前

    include 任务以在主生成文件中包含另一个生成文件(包含属性)。这个 if 属性 包括

    <include buildfile="devPropertyFile.build" if="${buildEnvironment == 'DEV'}"/>
    <include buildfile="testPropertyFile.build" if="${buildEnvironment == 'TEST'}"/>
    <include buildfile="prodPropertyFile.build" if="${buildEnvironment == 'PROD'}"/>
    
        3
  •  5
  •   Ryan Taylor    16 年前

    我遇到了一个类似的问题,scott.caligan的答案部分解决了这个问题,但是我希望人们能够通过指定如下目标来设置环境并加载适当的属性文件:

    • 南特试验
    • 南特期

    <target name="dev">
      <property name="environment" value="dev"/>
      <call target="importProperties" cascade="false"/>
    </target>
    
    <target name="test">
      <property name="environment" value="test"/>
      <call target="importProperties" cascade="false"/>
    </target>
    
    <target name="stage">
      <property name="environment" value="stage"/>
      <call target="importProperties" cascade="false"/>
    </target>
    
    <target name="importProperties">
      <property name="propertiesFile" value="properties.${environment}.build"/>
      <if test="${file::exists(propertiesFile)}">
        <include buildfile="${propertiesFile}"/>
      </if>
      <if test="${not file::exists(propertiesFile)}">
        <fail message="Properties file ${propertiesFile} could not be found."/>
      </if>
    </target>
    
        4
  •  0
  •   Andy Whitfield    16 年前

    我这样做的方式是根据使用 nant task iniread task in nantcontrib .