以数据库连接池的配置文件(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. Consul 注册中心介绍

    在 Spring Cloud 体系中,几乎每个角色都会有两个以上的产品提供选择,比如在注册中心有:Eureka.Consul.zookeeper.etcd 等:网关的产品有 Zuul.Spring C ...

  2. jquery.marquee

    http://aamirafridi.com/jquery/jquery-marquee-plugin#examples <script src="/plugins/marquee/j ...

  3. typeAliasesPackage 属性的作用

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  4. MySQL多表关联查询数量

    //多表关联查询数量select user, t1.count1, t2.count2from user tleft join ( select user_id, count(sport_type) ...

  5. Atcoder&CodeForces杂题11.7

    Preface 又自己开了场CF/Atcoder杂题,比昨天的稍难,题目也更有趣了 昨晚炉石检验血统果然是非洲人... 希望这是给NOIP2018续点rp吧 A.CF1068C-Colored Roo ...

  6. PX4/Pixhawk uORB

    PX4/Pixhawk的软件体系结构主要被分为四个层次 应用程序的API:这个接口提供给应用程序开发人员,此API旨在尽可能的精简.扁平及隐藏其复杂性 应用程序框架:这是为操作基础飞行控制的默认程序集 ...

  7. 十三、细说NULL导致的神坑,让人防不胜防

    当数据的值为NULL的时候,可能出现各种意想不到的效果,让人防不胜防,我们来看看NULL导致的各种神坑,如何避免? 一.比较运算符中使用NULL 任何值和NULL使用运算符(>.<.> ...

  8. springboot系列(七) 项目热加载

    spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. devtool ...

  9. Pyspark笔记一

    1. pyspark读csv文件后无法显示中文 #pyspark读取csv格式时,不能显示中文 df = spark.read.csv(r"hdfs://mymaster:8020/user ...

  10. HTML&CSS基础-相对定位

    HTML&CSS基础-相对定位 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> &l ...