maven profile实现多环境配置的示例
时间:2021-03-19 09:45:59|栏目:JAVA代码|点击: 次
环境:eclipse + spring mvc + maven
1、直接看图,把数据库的配置单独拿出来放在了resources_env目录下,三个不同环境参数不同,

2,在pom文件中添加配置
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 默认激活该profile节点-->
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources_env/dev</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<!-- 测试环境 -->
<id>qa</id>
<properties>
<env>qa</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources_env/qa</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<!-- 生产环境 -->
<id>online</id>
<properties>
<env>online</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources_env/online</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
说明:这个resources里面的路径对应上面文件路径,resources里面所有的配置加上各自环境的配置,
在引用jdbc.pro的地方如下:在datasource.xml中,

还有 新增的 evn那个包下面的所有文件都需要设置为资源文件,这个不必说 直接看图

3,maven设置要使用的环境:
项目右键-->maven-->Select Maven profiles ,选择一个环境,修改最好清理一下项目才生效,我之前没清理,发现没起作用。

4、然后运行项目就是你选择的环境了,或者直接导出war包,
其他的多环境配置同。
上一篇:关于idea更新到2020.2.3无法创建web项目原因 library is not specified
栏 目:JAVA代码
本文地址:http://www.codeinn.net/misctech/83854.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




