手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2017-03-05] 来源:Linux社区  作者:mouseIT [字体: ]

在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环境

Maven权威指南_中文完整版清晰PDF  http://www.linuxidc.com/Linux/2014-06/103690.htm

Maven 3.1.0 发布,项目构建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm

Linux 安装 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm

Ubuntu 16.04 安装Maven3.3.9 http://www.linuxidc.com/Linux/2017-02/140097.htm

Maven3.0 配置和简单使用 http://www.linuxidc.com/Linux/2013-04/82939.htm

Ubuntu下搭建sun-jdk和Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm

Maven使用入门 http://www.linuxidc.com/Linux/2012-11/74354.htm

Ubuntu 下 搭建Nexus Maven私服中央仓库  http://www.linuxidc.com/Linux/2016-08/133936.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-03/141398.htm

linux
相关资讯       Maven 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款