Maven的構建配置文件(Build Profiles)

在命令行使用構建配置文件時,是-P,好比:mvn -Pinput

注意:這裏的構建配置文件並非一個真正的文件,而是經過指定參數來作特定的事。html

如下內容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_profiles.htmljava

當時此教程的例子是在2.0版本,而新的3.0版本只是增長了一點,具體能夠參考官網http://maven.apache.org/guides/introduction/introduction-to-profiles.htmlgit

什麼是構建配置文件?github

構建配置文件(A Build profile) 是一系列的配置項的值,能夠用來設置或者覆蓋Maven構建默認值。使用構建配置文件,你能夠爲不一樣的環境,好比說生產環境(Producation)和開發(Development)環境,定製構建方式。數據庫

配置文件在pom.xml文件中使用activeProfiles或者profiles元素指定,而且能夠經過各類方式觸發。配置文件在構建時修改POM,而且用來給參數設定不一樣的目標環境(好比說,開發(Development)、測試(Testing)和生產環境(Producation)中數據庫服務器的地址)。apache

構建配置文件的類型服務器

構建配置文件大致上有三種類型maven

類型 在哪定義
項目級(Per Project) 定義在項目的POM文件pom.xml中
用戶級 (Per User) 定義在Maven的設置xml文件中 (%USER_HOME%/.m2/settings.xml)
全局(Global) 定義在Maven全局的設置xml文件中 (%M2_HOME%/conf/settings.xml)

配置文件激活ide

Maven的構建配置文件能夠經過多種方式激活。測試

  • 使用命令控制檯輸入顯式激活。
  • 經過maven設置。
  • 基於環境變量(用戶或者系統變量)。
  • 操做系統設置(好比說,Windows系列)。
  • 文件的存在或者缺失。

官方配置文件激活示例

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

http://maven.apache.org/ref/2.2.1/maven-profile/profiles.html

實踐配置文件激活示例

新建的項目結構以下:

其中在src/main/resources文件夾下有三個用於測試文件:

文件名 描述
env.properties 若是未指定配置文件時默認使用的配置。
env.test.properties 當測試配置文件使用時的測試配置。
env.prod.properties 當生產配置文件使用時的生產配置。

注意:這三個配置文件並非表明構建配置文件的功能,而是用於本次測試的目的;好比,我指定了構建配置文件爲prod時,項目就使用envprod.properties文件。

注意:下面的例子仍然是使用AntRun插件,由於此插件能綁定Maven生命週期階段,並經過Ant的標籤不用編寫一點代碼便可輸出信息、複製文件等,經此而已。其他的與本次構建配置文件無關。

一、顯示配置文件激活

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的任務。

再測試其他兩個命令,結果以下:

二、經過Maven設置激活配置文件

打開%USER_HOME%/.m2目錄下的settings.xml文件,其中%USER_HOME%表明用戶主目錄。若是setting.xml文件不存在就直接拷貝%M2_HOME%/conf/settings.xml到.m2目錄,其中%M2_HOME%表明Maven的安裝目錄。對於爲何能夠這樣作,參考:http://www.cnblogs.com/EasonJim/p/6827058.html

配置setting.xml文件,增長<activeProfiles>屬性:

<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">
   ...
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

執行命令:

mvn test

提示:此時不須要使用-Ptest來輸入參數了,上面的setting.xml文件的<activeprofile>已經指定了test參數,代替了。

提示2:一樣可使用在%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

提示:上面使用-D傳遞環境變量,其中evn對應剛纔設置的<name>值,test對應<value>。

提示2:在Windows 10上測試了系統的環境變量,可是不生效,因此,只能經過-D傳遞。

執行結果:

四、經過操做系統激活配置文件

五、經過文件的存在或者缺失激活配置文件

六、經過JDK的版本激活配置文件

...

更多激活配置,能夠參考官方的例子:http://maven.apache.org/guides/introduction/introduction-to-profiles.html

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test4/test4/testproject

相關文章
相關標籤/搜索