Apache Maven 構(gòu)建配置文件

2022-08-30 16:27 更新

什么是構(gòu)建配置文件?

構(gòu)建配置文件是一組配置的集合,用來設(shè)置或者覆蓋 Maven 構(gòu)建的默認(rèn)配置。使用構(gòu)建配置文件,可以為不同的環(huán)境定制構(gòu)建過程,例如 Producation 和 Development 環(huán)境。

Profile 在 pom.xml 中使用 activeProfiles / profiles 元素指定,并且可以用很多方式觸發(fā)。Profile 在構(gòu)建時修改 POM,并且為變量設(shè)置不同的目標(biāo)環(huán)境(例如,在開發(fā)、測試和產(chǎn)品環(huán)境中的數(shù)據(jù)庫服務(wù)器路徑)。


Profile 類型

Profile 主要有三種類型。

類型 在哪里定義
Per Project 定義在工程 POM 文件 pom.xml 中
Per User 定義在 Maven 設(shè)置 xml 文件中 (%USER_HOME%/.m2/settings.xml)
Global 定義在 Maven 全局配置 xml 文件中 (%M2_HOME%/conf/settings.xml)

Profile 激活

Maven 的 Profile 能夠通過幾種不同的方式激活。

  • 顯式使用命令控制臺輸入
  • 通過 maven 設(shè)置
  • 基于環(huán)境變量(用戶 / 系統(tǒng)變量)
  • 操作系統(tǒng)配置(例如,Windows family)
  • 現(xiàn)存 / 缺失 文件

Profile 激活示例

我們假定你的工程目錄像下面這樣:

Maven Build Profile

現(xiàn)在,在 src/main/resources 目錄下有三個環(huán)境配置文件:

文件名稱 描述
env.properties 沒有配置文件時的默認(rèn)配置
env.test.properties 使用測試配置文件時的測試配置
env.prod.properties 使用產(chǎn)品配置文件時的產(chǎn)品配置

顯式 Profile 激活

在接下來的例子中,我們將 attach maven-antrun-plugin:run 目標(biāo)添加到測試階段中。這樣可以我們在不同的 Profile 中輸出文本信息。我們將使用 pom.xml 來定義不同的 Profile,并在命令控制臺中使用 maven 命令激活 Profile。

假定,我們在 C:\MVN\project 目錄下創(chuàng)建了以下的 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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</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>
   </profiles>
</project>

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行以下 mvn 命令。使用 -P 選項指定 Profile 的名稱。

C:\MVN\project>mvn test -Ptest

Maven 將開始處理并顯示 test Profile 的結(jié)果。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\project\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\project\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] Using env.test.properties
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sun Jul 08 14:55:41 IST 2012
[INFO] Final Memory: 8M/64M
[INFO] ------------------------------------------------------------------

現(xiàn)在我們練習(xí)一下,你可以按照下面的步驟做:

  • 在 pom.xml 的 profiles 元素中添加另一個 profile 元素(拷貝已有的 profile 元素并粘貼到 profiles 元素結(jié)尾)。
  • 將此 profile 元素的 id 從 test 修改為 normal。
  • 將任務(wù)部分修改為 echo env.properties,以及 copy env.properties 到目標(biāo)目錄
  • 再次重復(fù)以上三個步驟,修改 id 為 prod,修改 task 部分為 env.prod.properties
  • 全部就這些了?,F(xiàn)在你有了三個構(gòu)建配置文件(normal / test / prod)。

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。使用 -P 選項指定 Profile 的名稱。

C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod

檢查構(gòu)建的輸出看看有什么不同。


通過 Maven 設(shè)置激活 Profile

打開 Maven 的 settings.xml 文件,該文件可以在 %USER_HOME%/.m2 目錄下找到,%USER_HOME% 表示用戶主目錄。如果 settings.xml 文件不存在則需要創(chuàng)建一個。

像在下面例子中展示的一樣,使用 activeProfiles 節(jié)點添加 test 配置作為激活的 Profile。

<settings 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/xsd/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal Artifactory Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。不要使用 -P 選項指定 Profile 的名稱。Maven 將顯示被激活的 test Profile 的結(jié)果。

C:\MVN\project>mvn test

通過環(huán)境變量激活 Profile

現(xiàn)在從 maven 的 settings.xml 中刪除激活的 Profile,并更新 pom.xml 中的 test Profile。將下面的內(nèi)容添加到 profile 元素的 activation 元素中。

當(dāng)系統(tǒng)屬性 “env” 被設(shè)置為 “test” 時,test 配置將會被觸發(fā)。創(chuàng)建一個環(huán)境變量 “env” 并設(shè)置它的值為 “test”。

<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>test</value>
      </property>
   </activation>
</profile>

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。

C:\MVN\project>mvn test

通過操作系統(tǒng)激活 Profile

activation 元素包含下面的操作系統(tǒng)信息。當(dāng)系統(tǒng)為 windows XP 時,test Profile 將會被觸發(fā)。

<profile>
   <id>test</id>
   <activation>
      <os>
         <name>Windows XP</name>
         <family>Windows</family>
         <arch>x86</arch>
         <version>5.1.2600</version>
      </os>
   </activation>
</profile>

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。不要使用 -P 選項指定 Profile 的名稱。Maven 將顯示被激活的 test Profile 的結(jié)果。

C:\MVN\project>mvn test

通過現(xiàn)存 / 缺失的文件激活 Profile

現(xiàn)在使用 activation 元素包含下面的操作系統(tǒng)信息。當(dāng) target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失時,test Profile 將會被觸發(fā)。

<profile>
   <id>test</id>
   <activation>
      <file>
         <missing>target/generated-sources/axistools/wsdl2java/
         com/companyname/group</missing>
      </file>
   </activation>
</profile>

現(xiàn)在打開命令控制臺,跳轉(zhuǎn)到 pom.xml 所在目錄,并執(zhí)行下面的 mvn 命令。不要使用 -P 選項指定 Profile 的名稱。Maven 將顯示被激活的 test Profile 的結(jié)果。

C:\MVN\project>mvn test


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號