软件编程
位置:首页>> 软件编程>> java编程>> maven profile实现多环境配置的示例

maven profile实现多环境配置的示例

作者:松鼠陛下  发布时间:2023-09-24 12:02:57 

标签:maven,profile,多环境配置

环境:eclipse + spring mvc + maven

1、直接看图,把数据库的配置单独拿出来放在了resources_env目录下,三个不同环境参数不同,

maven profile实现多环境配置的示例

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中,

maven profile实现多环境配置的示例

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

maven profile实现多环境配置的示例

3,maven设置要使用的环境:

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

 maven profile实现多环境配置的示例

4、然后运行项目就是你选择的环境了,或者直接导出war包,

  其他的多环境配置同。

来源:https://blog.csdn.net/songshuguowang/article/details/86606210

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com