1.properties-maven-plugin是個什么鬼?
介紹前我們先看一個問題,比如我們有一個maven項目結構如下:
一般我們都把一些配置文件放到像src/main/resources/jdbc.properties
這樣的文件中。但是文件里我們更多的放的還是變量,內容如下:
jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.validationQuery=${jdbc.validationQuery}
具體的值我們會放到pom.xml
中,用<properties>
來配置,如下所示代碼:
<?xml version="1.0" encoding="UTF-8"?>
<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.qyf404</groupId>
<artifactId>learn-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<profiles></profiles>
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password></jdbc.password>
<jdbc.validationQuery>SELECT 1 + 1</jdbc.validationQuery>
</properties>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
按照上面的方式配置。我們執行mvn package
后,在target/classes/jdbc.properties
里可以看到配置文件被成功替換。
由於某些原因(比如配置文件項比較多,為了讓pom.xml
更精簡),我們希望把這些配置項提取到一個properties文件中進行配置。
這時候就需要用到properties-maven-plugin了。properties-maven-plugin可以在執行maven命令時,讀取指定properties文件中的配置項,來實現和pom.xml中配置<properties>
一樣的效果。
2.properties-maven-plugin怎么用?
還拿上面的例子說,比如我們把pom.xml
中的配置項放到一個全局的my.properties
中。
profiles/dev/my.properties
文件內容如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
jdbc.username=root
jdbc.password=
jdbc.validationQuery=SELECT 1 + 1
pom.xml
我們把插件加進去,並把之前里面的配置項注釋掉.
<?xml version="1.0" encoding="UTF-8"?>
<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.qyf404</groupId>
<artifactId>learn-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<properties>-->
<!--<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>-->
<!--<jdbc.url>jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true</jdbc.url>-->
<!--<jdbc.username>root</jdbc.username>-->
<!--<jdbc.password></jdbc.password>-->
<!--<jdbc.validationQuery>SELECT 1 + 1</jdbc.validationQuery>-->
<!--</properties>-->
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>default-cli</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${user.dir}/profiles/dev/my.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我們執行mvn package
后,在target/classes/jdbc.properties
里可以看到配置文件被成功替換。
3.進階
把pom.xml
里的配置項提取到properties文件中,這是properties-maven-plugin干的事情。但是我們用properties-maven-plugin要達到更好的效果。
想一個場景:
- 我們的項目有開發環境的配置,測試環境的配置;
- 而且開發環境是mysql數據庫,測試環境是hsqldb數據庫;
- 最后還要在一個文件中統一打印出配置項內容。
我們可以通過maven的profile來配上properties-maven-plugin實現針對不同環境的快速打包。
我們把項目做個改造,結構如下:
profiles/dev/my.properties
內容如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
jdbc.username=root
jdbc.password=
jdbc.validationQuery=SELECT 1 + 1
profiles/test/my.properties
內容如下:
jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:hsql://localhost/stocktest
jdbc.username=root
jdbc.password=
jdbc.validationQuery=SELECT 1 + 1
pom.xml
內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.qyf404</groupId>
<artifactId>learn-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<properties>-->
<!--<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>-->
<!--<jdbc.url>jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true</jdbc.url>-->
<!--<jdbc.username>root</jdbc.username>-->
<!--<jdbc.password></jdbc.password>-->
<!--<jdbc.validationQuery>SELECT 1 + 1</jdbc.validationQuery>-->
<!--</properties>-->
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>default-cli</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
<goal>write-project-properties</goal>
</goals>
<configuration>
<files>
<!--<file>${user.dir}/profiles/dev/my.properties</file>-->
<file>${user.dir}/profiles/${profile.id}/my.properties</file>
</files>
<!--輸出全部配置項到指定文件-->
<outputFile>${build.directory}/profile.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.id>dev</profile.id>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>test</id>
<properties>
<profile.id>test</profile.id>
</properties>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.6</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
我們現在只需要執行命令mvn package -Pdev
或者mvn package
就可以打一個開發的包。
執行命令mvn package -Ptest
就可以打一個測試用的包。
而且在target/profile.properties
里查看項目打包的全部maven用到的配置項。內容如下:
#Properties
#Wed Sep 23 19:06:47 CST 2015
jdbc.url=jdbc\:mysql\://localhost/stock?createDatabaseIfNotExist\=true&useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
jdbc.username=root
jdbc.validationQuery=SELECT 1 + 1
jdbc.password=
profile.id=dev
jdbc.driverClassName=com.mysql.jdbc.Driver
示例代碼github地址: https://github.com/qyf404/learn-maven/tree/properties-maven-plugin
更多關於properties-maven-plugin請參閱這里:http://www.mojohaus.org/properties-maven-plugin/index.html