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

环境: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包,

  其他的多环境配置同。

到此这篇关于maven profile实现多环境配置的示例的文章就介绍到这了,更多相关maven profile多环境配置内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。