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'}"/>
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>
제가 이런 종류의 작업을 수행한 방식은 다음을 사용하여 빌드 유형에 따라 별도의 빌드 파일을 포함하는 것입니다. 낸트 작업.가능한 대안은 nantcontrib의 iniread 작업.