【轉載:https://blog.csdn.net/blueheart20/article/details/52838093】
1. Maven中的profile設置
Maven是目前主流的項目代碼結構管理工具和打包發布工具,在其中提供了profile方式,可以將不同的環境下的信息,基於profile來進行管理,所有的配置信息放入profile之內;
大家可以把profile當作一套環境下的獨立一套配置來理解。
示例如下, pom.xml中的配置部分內容:
<project>
<!-- 省略其他部分內容 --->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<ab.key>testkey</ab.key>
</properties>
<build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile>
<profile>
<id>test</id>
<properties>
<ab.key>anothertestkey</ab.key>
</properties>
<build>
<filters>
<filter>src/main/resources/config/db.properties</filter>
<filter>src/main/resources/config/test.xml</filter>
</filters>
</build>
</profile>
</profiles>
</project>
這里使用了ab.key來表示不同環境下的配置信息,大家可以看到不同配置環境下的擁有相同key的值是各不相同的, testkey和anothertestkey.
<activation>標簽下的內容標識為在基於mvn的命令操作情況下,默認的profile指定,在命令行如果沒有指定profile,則默認使用activeByDefault中設定的profile值。
2. Profile實現配置信息動態過濾的依賴包
主要的plugins如下所示:
<project> <!--這里省略其他部分的內容 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <!-- <includes> <include>**/*</include> </includes> --> <filtering>true</filtering> </resource> </resources> </build>
maven-surefire-plugin, 可選組件, 插件用來在maven構建生命周期的test phase執行一個應用的單元測試。它會產生兩種不同形式的測試結果報告。
使用方式: 使用該插件很簡單,使用mvn surefire:test或者mvn test都可以運行工程下的單元測試。
結果信息: 在工程的${basedir}/target/surefire-reports,目錄下(basedir指的是pom文件所在的目錄)
主頁地址: http://maven.apache.org/components/plugins/maven-surefire-plugin/
maven-resources-plugin: 必選組件, 基於profile指定動態過濾配置信息
使用方式: mvn package -P profileName (or mvn deploy etc)
結果信息: 將src/main/resources下的配置信息占位符替換為profile中指定的值
主頁地址: http://maven.apache.org/components/plugins/maven-resources-plugin/
關於resources節點下的信息作用,這里是整個resources的總開關,filtering這里指定啟用過濾功能,否則該功能將無法正常使用。 <directory>節點設定了掃描的目錄,includes節點主要是從整體的角度設定具體的掃描目錄或者文件;如果這里不指定的話,可以在各個profile之中指定具體需要掃描的配置文件,在build->filters->filter中指定特定的配置文件, 具體信息可以參照#1中的pom.xml示例。
#1中的resources filter設置如下:
............ <profile> <id>test</id> <properties> <ab.key>anothertestkey</ab.key> </properties> <build> <filters> <filter>src/main/resources/config/db.properties</filter> <filter>src/main/resources/config/test.xml</filter> </filters> </build> </profile> ...............
3. profile文件使用示例
3.1 創建maven項目
打開Eclipse, 打開File--> New--> Maven Project, 創建maven項目:
填寫相應的項目groupId和artifactId信息:
點擊Finish按鈕之后,創建項目成功
3.2 項目結構以及相應的配置文件信息
這里我們將配置文件test.xml和db.properties放入了src/main/resources目錄下的config中。
同時將maven的plugins按照#1的要求配置進入pom.xml文件中。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>org.test</groupId> <artifactId>mymaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mymaven</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <!-- <includes> <include>**/*</include> </includes> --> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <ab.key>testkey</ab.key> </properties> <build> <filters> <filter>src/main/resources/config/db.properties</filter> <filter>src/main/resources/config/test.xml</filter> </filters> </build> </profile> <profile> <id>test</id> <properties> <ab.key>anothertestkey</ab.key> </properties> <build> <filters> <filter>src/main/resources/config/db.properties</filter> <filter>src/main/resources/config/test.xml</filter> </filters> </build> </profile> </profiles> </project>
配置文件中的信息如下, test.xml中的內容:
<?xml version="1.0" encoding="UTF-8"?> <info>${ab.key}</info>
db.properties中的內容如下:
my.key2=${ab.key}
ab.key在pom.xml中的不同profile中設定的相應值。
在pom.xml中設定了2個profile, test和dev, dev做為缺省的profile。
4. 執行打包或者發布操作,查看打包結果
>> mvn clean # 清理上次打包的結果和臨時文件
>> mvn package -P dev -Dmaven.test.skip=true # 打包,忽略測試部分
然后接入生成的target目錄,查看classes目錄下的config, 查看db.properties和test.xml中的內容:
maven的命令運行過程信息:
然后我們就可以在打包之后的結果中,看到過濾之后的信息了。
在Maven中-DskipTests和-Dmaven.test.skip=true的區別如下:
- -DskipTests,不執行測試用例,但編譯測試用例類生成相應的class文件至target/test-classes下。
- -Dmaven.test.skip=true,不執行測試用例,也不編譯測試用例類。
5. 在過程中碰到的問題以及解決辦法
Q1: 在打包過程中,發現配置信息,並未被正確的profile下的信息替換掉
How to fix it?
a. 檢查maven中的resources plugin是否被正確的引入, 在全局的filtering設置是否被打開,在build節點中的directory目錄設置是否正確。另外,在特定的profile中設置的filter路徑以及文件是否正確等
Q2: 在打包過程中,配置文件被正確過濾替換了,但是配置文件不是被復制到特定的目錄,比如config下,而是被放入了classes下的根目錄了,為什么?
How to fix it ?
此類情況應是build-->resources下設置了includes信息,include了所有的文件或者include文件路徑為缺省值,比如下面的設置:
..................... <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
這種情況下,將includes節點去掉,在profile中進行配置filter即可。
Q3: 我的配置都是沒有問題,但是依然發現配置文件中的占位符,沒有被正確替換,問題在哪里?
How to fix it ?
請檢查maven-resources-plugin是否被正確的引入到plugins的列表中。