Maven 教程(17)— Maven Profile 和 Filtering 簡介


原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79774572

每個項目都會有多套運行環境(開發,測試,正式等等),不同的環境配置也不盡相同(如jdbc.url),借助Jenkins和自動部署提供的便利,我們可以把不同環境的配置文件單獨抽離出來,打完包后用對應環境的配置文件替換打包后的文件,其實maven已經給我們提供了替換方案:profile + filtering

Filtering


Filtering 是 maven 的 resource 插件 提供的功能,作用是用環境變量、pom文件里定義的屬性和指定配置文件里的屬性替換屬性(*.properties)文件里的占位符(${jdbc.url}),具體使用如下:
src/main/resources目錄有個配置文件jdbc.properties,內容如下:

jdbc.url=${pom.jdbc.url}
jdbc.username=${pom.jdbc.username}
jdbc.passworkd=${pom.jdbc.password}

配置 resource 插件,啟用filtering功能並添加屬性到pom:

<project>
    ...
    <!-- 用pom里定義的屬性做替換 -->
    <properties>
        <pom.jdbc.url>jdbc:mysql://127.0.0.1:3306/dev</pom.jdbc.url>
        <pom.jdbc.username>root</pom.jdbc.username>
        <pom.jdbc.password>123456</pom.jdbc.password>
    </properties>
    <build>
        ...
        <!-- 可以把屬性寫到文件里,用屬性文件里定義的屬性做替換 -->
        <filters>
            <filter>src/main/filters.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>
    ...
</project>

編譯包后target目錄下的jdbc.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
jdbc.username=root
jdbc.passworkd=123456

Profile 簡介


什么是profile?<profile>就像<dependencies>一樣是pom文件里的一個xml元素,在profile里幾乎可以定義所有在pom里的定義的內容(<dependencies><properties>,插件配置等等,不過不能再定義他自己了)。當一個profile被激活時,它定義的<dependencies><properties>等就會覆蓋掉原pom里定義的相同內容,從而可以通過激活不同的profile來使用不同的配置。

<!-- profile 的感性認識 -->
<project>
    ...
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
                <pom.jdbc.url>jdbc:mysql://127.0.0.1:3306/dev</pom.jdbc.url>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>3.2.4.RELEASE</version>
                </dependency>
                <dependencies>
        </profile>
    </profiles>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
    </dependencies>
    ...
</project>

Profile 如何配置


可以在兩個位置配置profile:settings.xml 和 pom.xml

  • settings.xml里定義的profile是全局的,對所有的項目都可用,在里面定義的配置項也稍微少了些,只能定義遠程服務器的信息和屬性信息(<repositories>,<pluginRepositories>,<properties>)。這些信息在pom.xml里也是可以定義的。
  • pom.xml里可以定義的配置如下:

 

<repositories>
<pluginRepositories>
<dependencies>
<plugins>
<properties>
<modules>
<reporting>
<dependencyManagement>
<distributionManagement>

以及build下的:

    <defaultGoal>
    <resources>
    <testResources>
    <finalName>

如果profile被激活,profile里的配置和原pom的配置會做覆蓋合並。

如何激活Profile


可以通過多種方式激活profile(顯式的,隱式的)

顯式的激活

通過maven 的-P參數激活指定的profile,參數的值是profile的id,多個profile以逗號分割,如果不想激活某個默認的profile,就在它的id前加個!

mvn -U clean package -Ptest,local,!ignore

IDEA里則可以在 Maven Projects 里直接勾選想要激活的profile

隱式的激活

配置profile時,可以在<profile>的 <activation>元素下配置隱式激活的信息。

默認激活
  • pom.xml文件里
<!-- 默認激活 -->
<profiles>
  <profile>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
</profiles>

settings.xml文件里則是通過<activeProfiles>來配置默認激活的profile列表

<activeProfiles>
    <activeProfile>artifactory</activeProfile>
</activeProfiles>
根據操作系統類型激活
<profiles>
    <profile>
        <activation>
            <os>
                <!-- 不必指定所有信息 -->
                <name>linux</name>
                <family>unix</family>
                <arch>amd64</arch>
                <version>3.19.0-30-generic</version>
            </os>
      </activation>
    </profile>
</profiles>

關於OS值的更多信息可以參考這里

根據JDK版本激活
<!-- 如果jdk的版本為1.8則激活該profile -->
<profiles>
  <profile>
    <activation>
      <jdk>1.8</jdk>
    </activation>
    </profile>
</profiles>

也可以通過[1.6,1.8)匹配多個jdk版本,關於匹配模式的詳細信息可以參考這里

根據環境變量激活
<!-- 如果環境變量里有`debug`且它的值為`true`則激活 -->
<!-- 也可以不指定`<value>`元素, 則只有環境變量里有`debug`就激活 -->
<profiles>
  <profile>
    <activation>
      <property>
        <name>debug</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
</profiles>

mvn -U clean package -Ddebug=true

通過判斷文件是否存在激活
<profiles>
  <profile>
    <activation>
      <file>
        <missing>/path/to/missing/file</missing>
        <exists>/path/to/exists/file</exists>
      </file>
    </activation>
    ...
  </profile>
</profiles>

不同類型的隱式激活方式可以組合使用,如根據同時指定根據操作系統類型和JDK版本來激活profile,只有但兩個條件都匹配是才激活之。

Filtering + Profile


思路:在不同的profile里配置不同的屬性(properties),然后激活相應的profile,用其中的屬性去替換jdbc.properties里的占位符。

繼續使用介紹Filtering時的例子,現在添加三個profile配置,分別對應開發,測試,正式環境。
修改后的pom文件如下:

<project>
    ...
    <build>
        <filters>
            <filter>src/main/filters-${active.profile}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
            </properties>
            <!-- 把當前profile設置為默認profile,可以同時這是多個為默認 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <active.profile>test</active.profile>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <active.profile>product</active.profile>
            </properties>
        </profile>
        ...
</project>

然后在src/main下新建三個文件:filters-dev.properties,filters-test.properties,filters-product.properties,文件內容如下(以filters-dev.properties為例):

pom.jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
pom.jdbc.username=root
pom.jdbc.password=123456

用 dev profile 打開發包mvn clean package -Pdev, 打包后jdbc.properties文件內容如下:

jdbc.url=jdbc:mysql://127.0.0.1:3306/dev
jdbc.username=root
jdbc.password=123456

如果不同的運行環境只是屬性值的不同,用上面的 profile + filtering 進行下變量替換可以很好的滿足打包需求,如果不是簡單的替換(如log4j.xml,開發環境只要輸出到標准輸出,測試和線上環境則還需要打到文件且文件的位置和策略也不相同),這個就需要借助maven 的 ant 插件。src/main/resources目錄下有三個log4j的配置文件,分別對應三個運行環境:

resources
├── log4j-product.xml
├── log4j-test.xml
└── log4j.xml

配置如下profile:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>config.*.properties</exclude>
                        <exclude>log4j-*.xml</exclude>
                    </excludes>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>test</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <tasks>
                                    <delete file="${project.build.outputDirectory}/log4j.xml" />
                                    <delete file="${project.build.outputDirectory}/log4j-product.xml" />
                                    <move file="${project.build.outputDirectory}/log4j-test.xml" tofile="${project.build.outputDirectory}/log4j.xml" />
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>product</id>
        <properties>
            <active.profile>product</active.profile>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <tasks>
                                    <delete file="${project.build.outputDirectory}/log4j.xml" />
                                    <delete file="${project.build.outputDirectory}/log4j-test.xml" />
                                    <move file="${project.build.outputDirectory}/log4j-product.xml" tofile="${project.build.outputDirectory}/log4j.xml" />
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM