English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
빌드 구성 파일은 설정 항목의 값의 시리즈로, Maven 빌드 기본 값을 설정하거나 덮어쓸 수 있습니다.
빌드 구성 파일을 사용하여, 생산 환경(Production)과 개발 환경(Development)과 같은 다른 환경에 대해 빌드 방식을 정의할 수 있습니다.
구성 파일은 pom.xml 파일에서 activeProfiles 또는 profiles 요소를 통해 지정되며, 여러 가지 방법으로 트리거될 수 있습니다. 구성 파일은 빌드 시 POM을 수정하고, 다른 목표 환경(예: 개발(Development), 테스트(Testing), 생산 환경(Production)) 중 데이터베이스 서버의 주소를 설정하는 데 사용됩니다.
빌드 구성 파일은 대략적으로 세 가지 유형이 있습니다:
유형 | 어디에 정의됨 |
---|---|
프로젝트 단계(Per Project) | 프로젝트의 POM 파일 pom.xml에 정의됨 |
사용자 단계(Per User) | Maven 설정 xml 파일(%USER_HOME%)에 정의됨/.m2/settings.xml) |
전역(Global) | Maven 전역 설정 xml 파일(%M)에 정의됨2_HOME%/conf/settings.xml) |
Maven의 빌드 구성 파일은 여러 가지 방법으로 활성화할 수 있습니다.
명령 프롬프트를 통해 명시적으로 활성화.
Maven 설정을 통해.
환경 변수(사용자 또는 시스템 변수) 기반으로.
운영 체제 설정(예를 들어, Windows 시리즈).
파일의 존재 또는 부재。
프로젝트 구조는 다음과 같이 가정됩니다:
src/main/resources 폴더에는 세 개의 테스트 파일이 있습니다:
파일 이름 | 설명 |
---|---|
env.properties | 구성 파일을 지정하지 않았을 때 기본적으로 사용되는 구성。 |
env.test.properties | 테스트 구성 파일 사용 시의 테스트 구성。 |
env.prod.properties | 생산 구성 파일 사용 시의 생산 구성。 |
주의:이 세 가지 구성 파일은 빌드 구성 파일의 기능을 대표하는 것이 아니라, 이번 테스트의 목적을 위해 사용됩니다. 예를 들어, 빌드 구성 파일을 prod로 지정하면, 프로젝트는 env.prod.properties 파일을 사용합니다.
주의:아래의 예제는 여전히 AntRun 플러그인을 사용합니다. 왜냐하면 이 플러그인은 Maven 생명주기 단계를 바인딩하고, Ant 태그를 사용하여 코드를 작성하지 않고도 정보를 출력하고 파일을 복사할 수 있기 때문입니다. 이 외의 것들은 이번 빌드 구성 파일과 관련이 없습니다.
profile은 일련의 구성 정보를 정의하고, 그 활성화 조건을 지정할 수 있습니다. 이렇게 하면 여러 개의 profile을 정의하고, 각 profile에 대해 다른 활성화 조건과 구성 정보를 지정하여, 다른 환경에서 다른 구성 정보를 사용할 수 있습니다.
다음 예제에서는 Maven-antrun-plugin:run 목표를 테스트 단계에 추가합니다. 이렇게 하면 다양한 profile에서 텍스트 정보를 출력할 수 있습니다. 우리는 pom.xml을 사용하여 다양한 profile을 정의하고, 명령줄 컨솔에서 Maven 명령을 사용하여 profile을 활성화합니다。
pom.xml 파일은 다음과 같습니다:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
주의:빌드 구성 파일사용됩니다 <profiles> 노드가 생성되었습니다。
설명:위에 세 개의 <profiles>중에서 <id> 다른 것들과 구분됩니다 <profiles> 다양한 AntRun 작업을 실행합니다; AntRun의 작업은 이렇게 이해할 수 있습니다. AntRun은 test의 Maven 생명주기 단계를 감지하고, Maven이 test를 실행할 때마다 AntRun 작업을 실행하며, 작업 내에서 텍스트를 출력하고 파일을 지정된 위치로 복사합니다. 그리고 실행할 AntRun 작업은 이 때빌드 구성 파일지정된 전송 역할을 합니다. 예를 들어, 명령행 파라미터를 통해 입력된 지정된 값을 <id>。
명령어 실행:
mvn test -Ptest
안내: 첫 번째 test는 Maven 생명주기 단계이며, 제 2 개의 test가빌드 구성 파일지정된 <id> 파라미터는 다음과 같이 통해 전달됩니다: -P를 통해 전송됩니다.当然了, prod나 normal과 같은 사용자 정의된 값이 될 수 있습니다.<id>。
실행 결과는 다음과 같습니다:
AntRun 작업이 성공적으로 트리거되었음을 확인할 수 있습니다. 또한 일치합니다.빌드 구성 파일의 <id> test 인 작업을 테스트하세요。
그런 다음 나머지 두 명령어를 테스트해 보면 결과가 다음과 같습니다:
을 열어 %USER_HOME%/.m2 디렉토리 아래의 settings.xml 파일을 복사합니다. %USER_HOME% 대표 사용자 메인 디렉토리입니다. setting.xml 파일이 존재하지 않으면 그대로 복사합니다. %M2_HOME%/conf/settings.xml 로 이동합니다.2 로 이동하여 .m %M2_HOME% 디렉토리에서
Maven의 설치 디렉토리를 나타냅니다。
setting.xml 파일을 구성하려면 <activeProfiles> 속성을 추가하세요://maven.apache.org/POM/4<settings xmlns="http: .0.0"//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4xmlns:xsi="http: .0.0//maven.apache.org/http:/xsd-1settings .0.0.xsd"> ... <activeProfiles>/<activeProfile>test< </activeProfile> </activeProfiles>
명령어 실행:
mvn test
알림 1:settings> -이제 사용하지 않아도 됩니다
알림 2:Ptest를 통해 입력 매개변수를 입력할 수 있습니다. 위의 setting.xml 파일의 <activeprofile>은 test 매개변수를 대체했습니다. %M2_HOME%/conf/settings.xml 의 파일을 구성하면 같은 효과가 나타납니다。
실행 결과:
이전 단계에서 테스트한 setting.xml 값들을 모두 제거하세요。
그런 다음 pom.xml 의 <id> test 인 <profile> 점에 <activation> 점을 추가하세요:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <activation> <property> <name>env</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
명령어 실행:
mvn test -Denv=test
알림 1:위에서 사용 -D 전달 환경 변수, evn은 언제나 설정된 <name> 값에 해당하며, test는 <value>에 해당합니다.
알림 2:Windows 10 시스템 환경 변수를 테스트했지만, 효과가 없어서, 다음으로 전달할 수밖에 없습니다 -D 전달.
실행 결과:
activation 요소가 다음 운영 체제 정보를 포함합니다. 운영 체제가 Windows XP일 때, test 프로필이 활성화됩니다.
<profile> <id>test</id> <activation> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> </profile>
현재 명령 프롬프트를 엽니다. pom.xml이 있는 디렉토리로 이동한 후 아래 mvn 명령어를 실행합니다. 다음과 같은 것을 사용하지 마세요 -P 옵션은 프로필 이름을 지정합니다. Maven은 활성화된 test 프로필의 결과를 표시합니다.
mvn test
이제 activation 요소를 사용하여 다음 운영 체제 정보를 포함합니다. target/generated-sources/axistools/wsdl2java/com/companyname/group이 누락되었을 때, test 프로필이 활성화됩니다.
<profile> <id>test</id> <activation> <file> <missing>target/generated-sources/axistools/wsdl2java/ com/companyname/group</missing> </file> </activation> </profile>
현재 명령 프롬프트를 엽니다. pom.xml이 있는 디렉토리로 이동한 후 아래 mvn 명령어를 실행합니다. 다음과 같은 것을 사용하지 마세요 -P 옵션은 프로필 이름을 지정합니다. Maven은 활성화된 test 프로필의 결과를 표시합니다.
mvn test
참조:https://www.cnblogs.com/EasonJim/p/6828743.html