有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等

每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到maven的profile配置了

1,在项目下pom.xml的project节点下创建了开发环境和线上环境的profile

    <profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prd</id>
<properties>
<env>prd</env>
</properties>
</profile>
</profiles>

其中id代表这个环境的唯一标识,下面会用到
properties下我们我们自己自定义了标签env,内容分别是dev和prd。

activeByDefault=true代表如果不指定某个固定id的profile,那么就使用这个环境

2,下面是我们resources下的目录,有两个目录,dev和prd,在开发时,我们使用dev下的配置文件,部署时候使用prd下的配置文件

3配置pom.xml,如果直接install,那么就会找到默认的id为dev的这个profile,然后会在里面找env的节点的值,

接着就会执行替换,相当于将src/main/resources/dev这个文件夹下的所有的配置文件打包到classes根目录下。

    <build>
<finalName>springmvc2</finalName>
<resources>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>
 

在idea下指定打包时指定profile的id

1第一步

2第二步

3第三步

4第四步,执行打包命令

这个时候target下springmvc项目下的classes根目录下有了env.properties,并且里面内容是我们指定的那个env.properties,

但是发现resources下的aa.properties文件没有被打包进去,那是因为我们只指定了resources/prd下的文件打包到根目录下,并没有指定其他文件

我们在pom.xml下的resources节点下新增规则

    <directory>src/main/resources</directory>
<excludes>
<exclude>dev/*</exclude>
<exclude>prd/*</exclude>
</excludes>
</resource>
 

再执行打包就会将resources下的除了dev文件夹和prd文件夹的其他所有文件打包到classes根目录下了

最后注意:如果有其他配置文件在src/main/java目录下,也是可以这样指定的,但是要指定

<includes>
<include>*.xml</include>
</includes>

不然会将java类当配置文件一块放到classes根目录下,加了include就会只匹配符合条件的放到target的classes根目录下

最后放我的所有的配置

   <profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prd</id>
<properties>
<env>prd</env>
</properties>
</profile>
</profiles> <build>
<finalName>springmvc</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>dev/*</exclude>
<exclude>prd/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>
 

转自:https://www.cnblogs.com/nfcm/p/7550772.html

最新文章

  1. Leetcode: plus one
  2. Ubuntu16.04 安装MATALAB R2015b教程
  3. PHP访问MSSQL数据库(实例代码)
  4. [转]LINQ To SQL 语法及实例大全
  5. sql 联合查询并更新
  6. MHA命令系统介绍 --masterha_master_switch
  7. 【hoj】1604 cable master
  8. 转场动画2-Pop动画
  9. 多媒体开发库 之 SDL 详解
  10. css样式表。作用是美化HTML网页.
  11. golang关于一些新手不注意会出现的小问题
  12. python3对比python2的更新点
  13. SQLServer Always On FCI 脑裂及可疑状态修复
  14. css定位实现星级展示没有交互
  15. 解题(TakeBusChooseLine)
  16. noip2014生活大爆炸版石头剪刀布
  17. cookie、localStorage、sessionStorage 的生命周期
  18. JS传递函数并且调用
  19. centos linux系统日常管理复习 CPU物理数逻辑核数,iftop ,iotop ,sar ,ps,netstat ,一网卡多IP,mii-tool 连接,ethtool速率,一个网卡配置多个IP,mii-tool 连接,ethtool速率 ,crontab备份, 第十八节课
  20. 【开发者笔记】C#连接mysql问题记录

热门文章

  1. 媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
  2. VC++ 控件赋值取值
  3. [HDU2276]Kiki &amp; Little Kiki 2
  4. DZY Loves Math
  5. Where should I put &lt;script&gt; tags in HTML markup?
  6. ubuntu16.04 常用软件
  7. bzoj 1233: [Usaco2009Open]干草堆tower 【想法题】
  8. 116、TensorFlow变量的版本
  9. appium常见问题04_查看andriod内置浏览器webview版本
  10. ThinkPHP内置标签库原理(Cx标签库)