以数据库连接池的配置文件(db.properties)为例,一般的项目会有开发用数据库,测试用数据库,正式环境数据库三种配置。

以前的做法是拷贝成三份,注释掉其他了两份

# 开发用
jdbc.url =jdbc:mysql://localhost:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
jdbc.username = root
jdbc.password = root # 测试用
# jdbc.url =jdbc:mysql://111.111.111.111:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
# jdbc.username = root
# jdbc.password = a@#$ # 正式环境用
# jdbc.url =jdbc:mysql://112.121.211.222:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
# jdbc.username = root
# jdbc.password = asd123&*(

项目每次打包到不同的环境都需要,选择正确的配置,取消它的注释,并注释掉另外两套配置。

如果用到pom.xml中的profiles标签,打包前的这些配置步骤就可以省略了。

1、首先在src/main/resources下建立environment文件夹,里面新建3个properties文件,代表上面提到的三种配置策略

db_dev.properties

env.jdbc.url =jdbc:mysql://localhost:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
env.jdbc.username = root
env.jdbc.password = root

db_test.properties

env.jdbc.url =jdbc:mysql://111.111.111.111:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
env.jdbc.username = root
env.jdbc.password = a@#$

db_prod.properties

env.jdbc.url =jdbc:mysql://112.121.211.222:3306/app_name?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
env.jdbc.username = root
env.jdbc.password = asd123&*(

2、改变原有的db.properties中的内容

db.properties

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

3、在pom.xml中追加profiles标签

<profiles>
<profile>
<id>dev</id>
<activation>
<!-- 代表默认配置是dev -->
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/resources/environment/db_dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<filters>
<filter>src/main/resources/environment/db_prod.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>test</id>
<build>
<filters>
<filter>src/main/resources/environment/db_test.properties</filter>
</filters>
</build>
</profile>
</profiles>

4、pom.xml的resources标签中追加对environment的配置。由于environment文件夹只作为“配置仓库”用,所以它不需要参与编译

            <resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>environment/*</exclude>
</excludes>

<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>

5、打包命令

给开发环境打包(用到得很少,一般都是直接jetty:run来调试)

mvn clean install -Dmaven.test.skip=true -Pdev

给测试环境打包

mvn clean install -Dmaven.test.skip=true -Ptest

给正式环境打包

mvn clean install -Dmaven.test.skip=true -Pprod

Maven项目打包时指定配置策略的更多相关文章

  1. maven 项目打包时无法解析读取properties文件

    在做项目时遇见一个问题,无法解析properties文件的 内容 异常为 Could not resolve placeholder ......... 在此之前均有做相关的 配置 但是从未出现过如上 ...

  2. maven项目打包时生成dependency-reduced-pom.xml

    今天给maven项目打jar包,发现在pom.xml文件的同路径下,突然生出了一个dependency-reduced-pom.xml,也不知道这个文件是干什么的,看着别扭就想着删除了它. 后来知道是 ...

  3. maven项目打包运行出错问题汇总

    maven项目打包时总会出现莫名其妙的错误,现总结一下. 打包方式:在maven项目底下运行cmd,输入mvn clean package,会自动按pom.xml的配置打成包.使用java -jar ...

  4. Maven之打包时配置文件替换

    在JavaWeb项目中,使用maven打包.在打正式包时,需要手动修改数据库配置为线上环境的地址,这样每次修改起来比较麻烦. 搜索了一些资料后,大部分的做法或原理都是预先使用表达式占位符,然后在打包时 ...

  5. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  6. IntelliJ IDEA自身以及maven项目打包方式

    1. Idea自身打包方式 1.1 创建Artifacts 快捷键(Ctrl+Alt+Shift+S)打开项目的Project Structure.在Artifacts创建 接着,指定main cla ...

  7. 十六:SpringBoot-自定义启动页,项目打包和指定运行环境

    SpringBoot-自定义启动页,项目打包和指定运行环境 1.自定义启动页 2.打包配置 2.1 打包pom配置 2.2 多环境配置 3.环境测试接口 4.打包执行 4.1 指定模块打包 4.2 运 ...

  8. maven项目install时忽略执行test

    1.在项目所在文件夹根目录使用maven命令打包时: <!-- 不执行单元测试,也不编译测试类 --> mvn install -Dmaven.test.skip=true 或 <! ...

  9. maven 项目打包 及window下部署到tomcat

    1.maven项目打包 2.将war文件拷贝到tomcat目录webapps下(不要再建目录)3.将必要的jar文件拷贝到tomcat目录libx下 war包 或jar 包 会生成到项目所在路径 的t ...

随机推荐

  1. Tomcat HTTP connector和AJP connector

    Tomcat服务器通过Connector连接器组件与客户程序建立连接,“连接器”表示接收请求并返回响应的端点.即Connector组件负责接收客户的请求,以及把Tomcat服务器的响应结果发送给客户. ...

  2. ubuntu下使用eclipse调试jni无法获取环境变量,本地库(java.library.path,LD_LIBRARY_PATH)等问题的解决。

    首先要把本地库全部配置到LD_LIBRARY_PATH中. 然后一定要采用命令行方式启动eclipse(也可以写一个启动shell,通过桌面启动器打开这个shell),这样环境变量才会有效. 打开终端 ...

  3. 正整数序列 Help the needed for Dexter ,UVa 11384

    题目描述 Description 给定正整数n,你的任务是用最少的操作次数把序列1, 2, …, n中的所有数都变成0.每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数.比如,1,2,3 ...

  4. IErrorHandler

    /// <summary> /// WCF服务端异常处理器 /// </summary> public class WCF_ExceptionHandler : IErrorH ...

  5. 【leetcode】338 .Counting Bits

    原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...

  6. Android笔记(五) Activity的启动模式

    Android中Activity是由返回栈来管理的,在默认情况下,每当启动一个新的Activity,它都会在返回栈中入栈,并且出于栈的顶端.但是有些时候Activity已经在栈的顶端了,也就不需要再启 ...

  7. 第十七篇:WEB服务器之HTTP协议

    本篇主要为为了实现WEB服务器,其中包含了HTTP协议的理解,以及TCP的三次握手.四次挥手等方面相关知识,同时还包含了关于web浏览器与服务器之间的通信过程. 一.web浏览器 通常在我们上网时会在 ...

  8. Flutter中的按钮组件介绍

    Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton.FlatButton.IconButton.OutlineButton.ButtonBar.Floati ...

  9. TLS1.3对CIP的影响(对密码套件的解释)

    1.术语定义的即使(算法)Definition of terms (optional) Cipher Suite  :通信数据保护规范,对TLS指定对端身份验证,关键技术机制,后续数据加密和数据验证机 ...

  10. linux----centos7 yum安装lnmp+zabbix

    安装yum utils工具包,若不安装则会找不到命令yum-config-manageryum -y install yum-utils 启用yum仓库yum-config-manager --ena ...