功能比替換變量要多?
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=".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=".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=".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