學習Maven之Properties Maven Plugin


1.properties-maven-plugin是個什麼鬼?

介紹前咱們先看一個問題,好比咱們有一個maven項目結構以下:

通常咱們都把一些配置文件放到像src/main/resources/jdbc.properties這樣的文件中。可是文件裏咱們更多的放的仍是變量,內容以下:html

jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.validationQuery=${jdbc.validationQuery}

具體的值咱們會放到pom.xml中,用<properties>來配置,以下所示代碼:java

<?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&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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裏能夠看到配置文件被成功替換。mysql

因爲某些緣由(好比配置文件項比較多,爲了讓pom.xml更精簡),咱們但願把這些配置項提取到一個properties文件中進行配置。
這時候就須要用到properties-maven-plugin了。properties-maven-plugin能夠在執行maven命令時,讀取指定properties文件中的配置項,來實現和pom.xml中配置<properties>同樣的效果。git


2.properties-maven-plugin怎麼用?

還拿上面的例子說,好比咱們把pom.xml中的配置項放到一個全局的my.properties中。github

profiles/dev/my.properties文件內容以下:sql

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/stock?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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裏能夠看到配置文件被成功替換。apache


3.進階

pom.xml裏的配置項提取到properties文件中,這是properties-maven-plugin乾的事情。可是咱們用properties-maven-plugin要達到更好的效果。maven

想一個場景:測試

  • 咱們的項目有開發環境的配置,測試環境的配置;
  • 並且開發環境是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&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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&amp;useUnicode\=true&amp;characterEncoding\=utf-8&amp;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

相關文章
相關標籤/搜索