Maven项目打包时指定配置策略
以数据库连接池的配置文件(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项目打包时指定配置策略的更多相关文章
- maven 项目打包时无法解析读取properties文件
在做项目时遇见一个问题,无法解析properties文件的 内容 异常为 Could not resolve placeholder ......... 在此之前均有做相关的 配置 但是从未出现过如上 ...
- maven项目打包时生成dependency-reduced-pom.xml
今天给maven项目打jar包,发现在pom.xml文件的同路径下,突然生出了一个dependency-reduced-pom.xml,也不知道这个文件是干什么的,看着别扭就想着删除了它. 后来知道是 ...
- maven项目打包运行出错问题汇总
maven项目打包时总会出现莫名其妙的错误,现总结一下. 打包方式:在maven项目底下运行cmd,输入mvn clean package,会自动按pom.xml的配置打成包.使用java -jar ...
- Maven之打包时配置文件替换
在JavaWeb项目中,使用maven打包.在打正式包时,需要手动修改数据库配置为线上环境的地址,这样每次修改起来比较麻烦. 搜索了一些资料后,大部分的做法或原理都是预先使用表达式占位符,然后在打包时 ...
- ******可用 SpringBoot 项目打包分开lib,配置和资源文件
spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...
- IntelliJ IDEA自身以及maven项目打包方式
1. Idea自身打包方式 1.1 创建Artifacts 快捷键(Ctrl+Alt+Shift+S)打开项目的Project Structure.在Artifacts创建 接着,指定main cla ...
- 十六:SpringBoot-自定义启动页,项目打包和指定运行环境
SpringBoot-自定义启动页,项目打包和指定运行环境 1.自定义启动页 2.打包配置 2.1 打包pom配置 2.2 多环境配置 3.环境测试接口 4.打包执行 4.1 指定模块打包 4.2 运 ...
- maven项目install时忽略执行test
1.在项目所在文件夹根目录使用maven命令打包时: <!-- 不执行单元测试,也不编译测试类 --> mvn install -Dmaven.test.skip=true 或 <! ...
- maven 项目打包 及window下部署到tomcat
1.maven项目打包 2.将war文件拷贝到tomcat目录webapps下(不要再建目录)3.将必要的jar文件拷贝到tomcat目录libx下 war包 或jar 包 会生成到项目所在路径 的t ...
随机推荐
- java常用配置文件头部声明
spring: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http: ...
- intellij idea 中右键项目没有git
1.没有GIT选项说明还没有关联你的仓库 2.ctrl + alt +s 打开VersionControl添加git仓库 3.添加后再次查看,GIT就出现了 4.可以查看历史版本了. 原文地址:htt ...
- javascript:void(0);的含义以及使用场景
一.含义: javascript:是伪协议,表示内容通过javascript执行. void(0)表示不作任何操作. 二.使用场景 1.href=”javascript:void(0);” 作用:为了 ...
- Asp.Net Mvc 整站Https
网站要使用https需要如下几个步骤 1.申请https证书,现在已经有很多免费的https证书申请了 2.服务器中安装证书 3.网站的连接全部改为https连接 Asp.Net Mvc网站中整站改为 ...
- vue的事件对象
事件对象: v-on:click/mouseover 简写: @click="" @click="show($event)" <input t ...
- js数组实现上移下移
up(index) { if(index === 0) { return } //在上一项插入该项 this.list.splice(index - 1, 0, (this.list[index])) ...
- vue中将时间戳转换为YYYY-MM-dd hh:mm格式时间的组件
首先我们可以使用vue中的过滤方法将数据变成另一个格式 // html <span class="rate-time">{{rating.rateTime | form ...
- HTTP协议的详解
[HTTP协议的详解] Ø 请求部分 * 请求行 * 提交方式: * 提交方式有很多,常用的GET和POST: * GET和POST的区别: * GET的提交的参数会显示到地址栏上,而POST不显示. ...
- shell 三剑客之 sed pattern 详解
sed 基础介绍 语法格式 sed 处理过程 sed 选项 cat sed.txt '-p' 打印输出 ,默认输出两次,流输出一次,源文件输出一次 sed 'p' sed.txt -n 只显示处理的 ...
- java中javamail收发邮件实现方法
概述 1.邮件相关的标准 厂商所提供的 JavaMail 服务程序可以有选择地实现某些邮件协议,常见的邮件协议包括: SMTP(Simple Mail Transfer Protocol) :即简单邮 ...