Maven Profile

一、什么是MavenProfile

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

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

二、Maven Profile入门

  修改pinyougou-page-web的pom.xml

 <properties>
      <port>9105</port>
  </properties>
  <build>  
      <plugins>         
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>${port}</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
      </plugins>  
    </build>

   运行tomcat7:run ,发现运行结果是一样的,因为port是变量,而变量值是定义为9105。这其实就是我们之前学习的maven的变量。

  那我们现在思考一下,如果这个端口在开发时使用9105,如果在生产环境(或其他环境)为9205呢?如何解决值的动态切换呢?

  这时我们修改pom.xml,增加profile定义

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

  执行命令 tomcat7:run -P pro  发现以9205端口启动

  执行命令 tomcat7:run -P dev  发现以9105端口启动

  -P 后边跟的是profile的id

  如果我们只执行命令tomcat7:run ,也是以9105启动,因为我们一开始定义的变量值就是9105,就是在不指定profileID时的默认值.

三、切换数据库连接配置

3.1 编写不同环境的配置文件

  (1)我们在pinyougou-dao工程中src/main/resources下创建filter文件夹

  (2)filter文件夹下创建db_dev.properties ,用于配置开发环境用到的数据库

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

  (3)filter文件夹下创建db_pro.properties 

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

  (4)修改properties下的db.properties

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

3.2 定义Profile

  修改pom.xml

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

  这里我们定义了2个profile,分别是开发环境和生产环境

3.3 资源过滤与变量替换

  修改pom.xml ,在build节点中添加如下配置

    <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实现对资源文件(resouces) 过滤。
  maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value 

3.4 打包

  在pinyougou-dao 工程 执行命令:package -P pro ,  解压生成的jar包,观察db.properties配置文件内容,已经替换为生产环境的值。

  在pinyougou-sellergoods-service工程 执行命令 pageage  ,解压生成的war包里的pinyougou-dao的jar包,发现也是生成环境的值。

3.5 测试运行

【1】连接生产数据库

  (1)在pinyougou-dao 工程执行命令:install -P pro

  (2)在pinyougou-sellergoods-service:执行命令:tomcat7:run

  (3)在pinyougou-shop-web :  执行命令:tomcat7:run

【2】连接开发数据库

  (1)在pinyougou-dao 工程执行命令:install -P dev  (或 install   )

  (2)在pinyougou-sellergoods-service:执行命令:tomcat7:run

  (3)在pinyougou-shop-web :  执行命令:tomcat7:run

四、切换注册中心连接配置

4.1 集中配置注册中心地址

  (1)在pinyougou-common工程中properties下创建dubbox.properties

address=192.168.25.140:2181

  (2)Spring目录下创建spring配置文件 applicationContext-dubbox.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath*:properties/*.properties" />
    <dubbo:registry protocol="zookeeper"  address="${address}"/>
    
</beans>

  (3)所有的服务工程与web工程都要依赖pinyougou-common . 并删除每个工程中关于注册中心地址的配置 

  (4)安装pinyougou-common到本地仓库,然后测试运行。

4.2 MavenProfile配置

  (1)在pinyougou-common工程中创建filters目录 ,目录下建立dubbox_dev.properties

env.address=192.168.25.140:2181

  (2)建立dubbox_pro.properties

env.address=192.168.25.141:2181

  (3)修改dubbox.properties

address=${env.address}

  (4)修改pinyougou-common的pom.xml

<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> 
.............................
 <build>
        <filters>
            <filter>src/main/resources/filters/dubbox_${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>    
        </resources>    
  </build>

猜你喜欢

转载自www.cnblogs.com/yft-javaNotes/p/10691994.html