有一种方式来动态加载一性质的文件中NAnt?
-
09-06-2019 - |
题
我想要负载一个不同性能的文件基于一个变量。
基本上,如果做了开发建立使用这种性质的文件,如果做一个试验建立使用这种其他性质的文件,并且如果做一个生产建立使用,但第三个特性的文件。
解决方案
步骤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:环境=dev
- nant D:环境=测试
- nant D:环境=生产
其他提示
你可以使用 include
任务包括建立另一个文件(包含性)内主要的建立的文件。的 if
属性的 include
任务可以测试对一个变量,以确定是否建立文件应包括:
<include buildfile="devPropertyFile.build" if="${buildEnvironment == 'DEV'}"/>
<include buildfile="testPropertyFile.build" if="${buildEnvironment == 'TEST'}"/>
<include buildfile="prodPropertyFile.build" if="${buildEnvironment == 'PROD'}"/>
我有一个类似的问题,这个问题的回答从斯科特.caligan部分地解决了,但是我想人们能够设定的环境负荷的适当性文件仅仅是通过指定一个目标,像这样:
- nant dev
- nant测试
- nant阶段
你可以通过增加一个目标,将环境可变的。例如:
<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>
我已经做过这种事情是要包括建立独立的文件中根据类型的建立一个使用的 nant任务.一个可能的替代办法可能是使用的 iniread任务在nantcontrib.
不隶属于 StackOverflow