Maven Profile的使用

在平常开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的 filter profile 功能,我们可实现在 编译阶段 简单的指定一个参数就能切换配制,提高效率,还不容易出错。

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

一、maven profile 的使用
1.1、切换不同环境下tomcat 的端口
pom.xml 文件

<properties>
    <port>9105</port>
</properties>

那么如何解决端口切换的问题:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <port>9105</port>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <port>9205</port>
        </properties>
    </profile>
</profiles>

tomcat 插件的配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <!-- 指定端口 -->
                <port>${port}</port>  这里使用的是SPEL
                <!-- 请求路径 -->
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

执行命令:tomcat7:run -P pro 以9205端口启动
执行命令:tomcat7:run -P dev 以9105端口启动
  -p 后面是 <profile> 的 <id>

1.2、切换数据库连接配置
1、在 -dao 工程的 src/main/resources 下创建 filter 文件夹
2、在 filter 文件夹下创建 db_dev.properties(开发环境下用的配置)

env.jdbc.driver=com.mysql.jdbc.Driver
env.jdbc.url=jdbc:mysql://localhost:3306/test001_dev?characterEncoding=utf-8
env.jdbc.username=root
env.jdbc.password=root

3、在filter文件夹下创建 db_pro.properties

env.jdbc.driver=com.mysql.jdbc.Driver
env.jdbc.url=jdbc:mysql://localhost:3306/test001b_pro\u2014\u2014?characterEncoding=utf-8
env.jdbc.username=root
env.jdbc.password=root

4、修改properties文件夹 下的 db.properties。使用的是传入参数的方式,具体传入什么参数还不确定。

jdbc.driver=${env.jdbc.driver}
jdbc.url=${env.jdbc.url}
jdbc.username=${env.jdbc.username}
jdbc.password=${env.jdbc.password}

5、定义profile

<properties>
    <env>dev</env>
</properties>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <env>pro</env>
        </properties>
    </profile>
</profiles>

  这里定义了两个profile,两个环境。
6、资源过滤与变量替换

<build>
      <filters>
          <filter>src/main/resources/filter/db_${env}.properties</filter>
      </filters>
      <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>    

  使用了 filter 插件,对资源文件进行过滤。
  filter 插件可以是用 SPEL 进行传递参数。
7、右键工程,执行打包命令行命令:

package -P xxx


1.3、切换注册中心连接配置
1、在common 工程中 properties 文件夹下,创建 dubbox.properties

address=${env.address}

2、创建 filter文件夹
创建 dubbox_dev.properties

env.address=192.168.25.128:2181

创建 dubbox_pro.properties

env.address=192.168.25.128:2181

2、在spring 文件夹下创建 applicationContext-dubbox.xml 

<context:property-placeholder location="classpath*:properties/*.properties" />   
<dubbo:registry protocol="zookeeper" address="${address}"/>  <!-- zookeeper服务端地址及端口 -->

3、因为common 工程在所有子工程都有依赖到,所以这时候,可以删除所有子工程的注册中心地址的配置。而统一采用common 工程中注册中心的配置。

posted @ 2019-04-11 21:29  payn  阅读(311)  评论(0)    收藏  举报