设为首页 收藏本站
查看: 1081|回复: 0

[经验分享] maven profile实现多环境打包

[复制链接]

尚未签到

发表于 2017-3-2 10:37:39 | 显示全部楼层 |阅读模式
  快速解决:
  项目目录
DSC0000.png

  1.pom文件中添加profile
<profiles>
    <profile>
        <!-- 本地开发环境 -->
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!-- 测试环境 -->
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>

    </profile>
    <profile>
        <!-- 生产环境 -->
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
        </properties>
    </profile>
</profiles>   2. pom中指定 filter文件夹 和 maven-war-plugin指定替换文件夹
<build>
       <resources>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>spring-content.xml</include>
               </includes>
               <filtering>true</filtering>
           </resource>
       </resources>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-war-plugin</artifactId>
               <version>2.4</version>
               <configuration>
                   <archiveClasses>true</archiveClasses>
                   <warName>${project.artifactId}</warName>
                   <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
                   <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
                   <webappDirectory>${project.build.directory}/${project.artifactId}
                   </webappDirectory>
                   <webResources>
                       <resource>
         <!-- 由于我是把配置文件都在/WEB-INF/config/文件夹-->
         <!-- 所以把src/main/resources 被filter替换的文件替换dao WEB-INF/config/下-->
                           <directory>src/main/resources</directory>
                           <targetPath>WEB-INF/config</targetPath>
                           <filtering>true</filtering>
                       </resource>
                   </webResources>
               </configuration>
           </plugin>
           <plugin>
               <groupId>org.eclipse.jetty</groupId>
               <artifactId>jetty-maven-plugin</artifactId>
               <version>9.3.0.M2</version>
               <configuration>
                   <scanIntervalSeconds>6</scanIntervalSeconds>
                   <httpConnector>
                       <port>5004</port>
                   </httpConnector>
                   <webAppConfig>
                       <contextPath>/xxxx</contextPath>
                       <!--<defaultsDescriptor>${basedir}/src/main/resources/webdefault.xml</defaultsDescriptor>-->
                   </webAppConfig>
               </configuration>
           </plugin>
       </plugins>
</build>
  3. 对比 WEB-INF/config下aplicationContent.xml(将被后面替换)和 src/main/resources 下aplicationContent.xml
<bean id="configProperties"
   class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="locations">
      <list>
         <value>/WEB-INF/config/application_dev.properties</value>
      </list>
   </property>
</bean>  VS
<bean id="configProperties"
   class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="locations">
      <list>
         <!--${profiles.active} 此处占位符 会被mvn替换从pom中profile.active的环境变量
         第三步中 maven-war-plugin配置替换文件  完成多环境切换-->
         <value>/WEB-INF/config/application_${profiles.active}.properties</value>
      </list>
   </property>
</bean>  4.maven 编译打包  mvn  clean package  -Dmaven.test.skip=true -Ptest
  指定-Dmaven.test.skip=true表示跳过测试   -Ptest 激活Profile id=test的环境参数
  实现效果
DSC0001.png

  config下 原本application_dev.properties 编译完成 替换为application_test.properties
  从而实现了加载多环境配置.
  概念简介
  构建项目时可能会遇到在测试(如单元测试)、开发、模拟、生产等不同环境下需要不同配置.
如果需要修改的项目很多而且复杂的话,则应该使用 Apache Maven 的 Profile 和 Filtering 功能来解决。
Filtering 功能
  Filtering 是 Maven Resources Plugin 的一个功能,它会使用系统属性或者项目属性的值替换资源文件(*.properties,*.xml)当中 ${&#8230;} 符号的值。比如你系统属性有一项 &#8220;user.name=foobar&#8221;,那么资源文件当中的 ${user.name} 符号会在 Maven 编译时自动被替换为 &#8220;foobar&#8221;。
Profile 功能
  Profile 的作用是允许你在项目文件(pom.xml)里定义若干个 profile 段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。接着上一个例子,如果我们需要为开发环境和生产环境定义不同的 user.name 属性值,则我们在项目目录里创建两个属性文件:
  profile-development.properties,内容
user.name=foobar  profile-production.properties,内容
user.name=tom  然后在项目文件(pom.xml)里增加 profile 段,如下:
<build>
   <filters>
      <filter>src/main/filters/filter-${env}.properties</filter>
   </filters>
   <resources>
      <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
      </resource>
   </resources>
</build>
<profiles>
   <profile>
      <id>develop</id>
      <properties>
         <env>develop</env>
      </properties>
   </profile>
   <profile>
      <id>test</id>
      <properties>
         <env>test</env>
      </properties>
   </profile>
   <profile>
      <id>product</id>
      <properties>
         <env>product</env>
      </properties>
   </profile>
</profiles>pasting  在编译项目时,可以使用 -P 参
  数指定需要使用的 profile 的 id,比如下面命令将会使用 development profile:
$mvn clean compile -Pdevelopment  如果想使用 production profile 则执行如下命令:
$mvn clean compile -Pproduction  假如不指定 -P 参数的话,则会使用 activeByDefault=true 的一项(即 development)。
  至此,通过 filtering 和 profile 功能实现了为开发环境和生产环境使用不同配置值的目的。当然 profile 还可以允许你添加更多的定义,比如为某一个 profile 添加不同的资源文件。在一些大中型项目里,不同的环境可能仅仅修改配置值并不足够,可能还需要某个配置文件整个替换,那么就应该在 profiles/profile/build/resources 段里指定了。详细的可以参阅附录链接。

  http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/


http://www.kafeitu.me/solution/2013/06/29/a-successful-cofiguration-file-management-solution.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-349193-1-1.html 上篇帖子: 使用Spring Session做分布式会话管理 下篇帖子: 用maven创建springmvc项目
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表