maven properties-maven-plugin是替换变量么

 我来答
ifeilong
2016-09-23 · TA获得超过8068个赞
知道大有可为答主
回答量:1187
采纳率:100%
帮助的人:811万
展开全部

功能比替换变量要多 


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&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里可以看到配置文件被成功替换。

由于某些原因(比如配置文件项比较多,为了让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&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里可以看到配置文件被成功替换。



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&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

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式