maven发布时在不同的环境使用不同的配置文件

  在开发时,不同的环境总会使用到不同的配置。如本地,测试,预发布,发布等环境,像数据库这些都要使用到不同的配置。如果手动改的话肯定会十分的麻烦。

还好maven提供的功能能够帮我们解决这个问题。

 

我们通过不同环境使用不同数据库的配置来说明

直接上代码:

1.db.properties

jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.url=${jdbc.url}
name=${myName}

2.dev.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/devdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=devuser
jdbc.password=dev123456

3.product.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=productuser
jdbc.password=product123456

4.test.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=testuser
jdbc.password=test123456

5.pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mavenImparityProfile</groupId>
  <artifactId>mavenImparityProfile</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mavenImparityProfile Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env><!--相当于定义一个变量 供下面使用-->
                <myName>张三</myName><!--使用一个properties文件中未定义,但是其他地方会取值的变量-->
            </properties>
            <activation><!--默认激活-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
                <myName>李四</myName>
            </properties>
            <activation><!--默认激活-->
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>
  <build>
    <finalName>mavenImparityProfile</finalName>
      <filters><!--获得过滤使用的源文件  即有实际数据的地反-->
          <filter>src/main/resources/properties/${env}.properties</filter>
      </filters>

      <!-- 指定 src/main/resources下所有文件及文件夹为资源文件 -->
      <resources>
        <resource>
            <directory>src/main/resources</directory>
             <filtering>true</filtering> <!--是否使用过滤器-->
         </resource>
          <!-- 第二中方式 设置对dev.properties,等进行过虑,即这些文件中的${key}会被替换掉为真正的值-->
          <!-- <resource>
              <directory>src/main/resources/properties/properties</directory>&lt;!&ndash;一定要指向上层目录&ndash;&gt;
              <includes>
                  &lt;!&ndash;要遍历出来文件都必须写&ndash;&gt;
                  <include>product.properties</include>
                  <include>test.properties</include>
                  <include>dev.properties</include>
                  <include>db.properties</include>
              </includes>
              <filtering>true</filtering>
          </resource>-->
     </resources>
  </build>

</project>

 

其实现主要是通过配置frofile来实现。上面配置了3个环境(test,dev,product)。test环境是默认激活的。

我们直接执行 deploy 则使用的是test的配置。

如果要使用product的配置,则使用maven的命令 mvn clean package -P product (注:-P要大写 -P后面的参数是我们前面定义不同环境的id。如果你使用的是idea工具,在配置run的时候不用写mvn这个参数)

 

附录:

如果要配置jenkins,其他的参数配置不变,只需要修改maven的命令

 

这里是发布product环境

posted @ 2015-12-11 16:36  耗子搞IT  阅读(3866)  评论(0编辑  收藏  举报