【问题标题】:Is there a way to dynamically load a properties file in NAnt?有没有办法在 NAnt 中动态加载属性文件?
【发布时间】:2010-09-09 18:20:45
【问题描述】:

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

基本上,如果进行开发构建使用此属性文件,如果进行测试构建使用此其他属性文件,如果进行生产构建使用第三个属性文件。

【问题讨论】:

    标签: .net build automation nant


    【解决方案1】:

    第 1 步:在您的 NAnt 脚本中定义一个属性以跟踪您正在构建的环境(本地、测试、生产等)。

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

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

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

    • nant -D:environment=dev
    • nant -D:environment=test
    • nant -D:environment=production

    【讨论】:

    • 您的行 对我来说失败并显示以下消息:“目标中不允许任务。必须在项目级别。”
    【解决方案2】:

    您可以使用include 任务在主构建文件中包含另一个构建文件(包含您的属性)。 include 任务的 if 属性可以针对变量进行测试以确定是否应包含构建文件:

    <include buildfile="devPropertyFile.build" if="${buildEnvironment == 'DEV'}"/>
    <include buildfile="testPropertyFile.build" if="${buildEnvironment == 'TEST'}"/>
    <include buildfile="prodPropertyFile.build" if="${buildEnvironment == 'PROD'}"/>
    

    【讨论】:

      【解决方案3】:

      我有一个类似的问题,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】:

        我做这种事情的方式是根据使用nant task 的构建类型包含单独的构建文件。一种可能的替代方法是使用iniread task in nantcontrib

        【讨论】:

          猜你喜欢
          • 2017-05-06
          • 2012-05-23
          • 1970-01-01
          • 2021-07-27
          • 1970-01-01
          • 2021-10-22
          • 1970-01-01
          • 2022-09-22
          相关资源
          最近更新 更多