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 ...
随机推荐
- Python Unittest进行接口测试的简单示例
今年肯定是要把Python学到一定程度的,否则感觉自己混不下去了,那就开始半挣扎的咸鱼生活吧. ---------------------------------------------------- ...
- 谷歌浏览器调用activex控件方法
原文转自 https://jingyan.baidu.com/article/af9f5a2d0ebe5543140a4596.html activex是由微软开发,所以在支持上,目前原生态支持的只有 ...
- 使用async和await的异步编程
异步编程模型(TAP)提供了抽象的异步代码.异步代码看起来和同步代码没什么大的区别,无非多个了两个关键字(async和await).但是代码的执行顺序并没看起来那么简单,代码的执行顺序根据cpu资源的 ...
- pinfinder
pinfinder https://pinfinder.net https://github.com/gwatts/pinfinder 关于 Pinfinder是一个小型免费程序,可以使用iPhone ...
- vue复制textarea文本域内容到粘贴板
vue实现复制内容到粘贴板 方案:找到textarea对象(input同样适用),获取焦点,选中textarea的所有内容,并调用document.execCommand("copy&q ...
- 上传图片,图片过大压缩处理以及解决自拍时会出现图片横屏的bug修复、长按保存图片
js部分:module.exports = { resize: function (file, callback, options) { //配置 options = Object.assign({ ...
- English-培训5-How much is it
- 安卓开发之生成cache目录和files目录
package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...
- rsync 进行本地拷贝
带杠还是不带杠 带杠表示拷贝目录里面的内容不包括目录本身 重要的是源路径带不带杠,目标路径没关系 rsync -az /root/test/src/ /root/test/dest/ rsync -a ...
- linux管道和重定向
管道 管道应该是等左边的程序执行完,才使用左边的程序的输出执行右边的程序. 但是在测试的时候,如果左边的程序无限循环且不等待的输出,那么左边的程序执行时右边的程序也会执行,个人感觉这是linux的机制 ...