怎么在maven中利用profile实现多环境配置

发布时间:2021-01-25 15:27:13 作者:Leah
来源:亿速云 阅读:211

本篇文章为大家展示了怎么在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包,

上述内容就是怎么在maven中利用profile实现多环境配置,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. maven-profile
  2. Spring @Profile注解如何实现多环境配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

maven profile

上一篇:如何在Maven中对profile进行配置

下一篇:同一份数据Redis需要存两次法人原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》