zoukankan      html  css  js  c++  java
  • Maven根据不同的环境打包不同的配置

    前言:

    在开发过程中,我们的软件会面对不同的运行环境,比如开发环境、测试环境、生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置等等。

    那么就需要借助maven提供的profile功能,通过不同的环境激活不同的profile来实现“maven根据不同的运行环境,打包不同的配置文件”的目的。

    一、原理      

       利用filter实现对资源文件(resouces)过滤 

               maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value 

       利用profile来切换环境 

           maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile 
    结合filter和profile,我们就可以方便的在不同环境下使用不同的配置

    二、pom配置

     <profiles> 
        <profile> 
          <id>cluster</id>  
          <activation> 
            <property> 
              <name>node</name> 
            </property> 
          </activation>  
          <build> 
            <finalName>ota</finalName>  
            <filters> 
              <filter>${project.basedir}/src/cluster/nodes/${node}.properties</filter> 
            </filters>  
            <resources> 
              <resource> 
                <targetPath>${project.basedir}/target/${project.build.finalName}</targetPath>  
                <filtering>true</filtering>  
                <directory>${project.basedir}/src/main/sca-resources</directory>  
                <includes> 
                  <include>META-INF/**/*.composite</include>  
                  <include>definitions.xml</include> 
                </includes> 
              </resource>  
              <resource> 
                <targetPath>${project.basedir}/target/${project.build.finalName}/WEB-INF/classes</targetPath>  
                <filtering>true</filtering>  
                <directory>${project.basedir}/src/main/resources</directory>  
                <includes> 
                  <include>**/*.xml</include>  
                  <include>**/*.properties</include>  
                  <include>policy</include> 
                </includes> 
              </resource> 
            </resources> 
          </build> 
        </profile>  
        
      </profiles> 
    • activation:在activation元素中指定激活条件
    • property:当maven检测到property(pom中如${name}这样的)profile将被激活   无论取任何值,都会触发

    执行打包命令

     手工编译,打包:maven clean package -Ptest      激活id="test"的profile 

                                  

    install 和 package命令的区别

    1.install:打包好的 jar 包会安装到本地的 maven 仓库中,使用的配置是默认的配置,供其他项目使用。 
    2.package 指定参数打包:clean package -Dmaven.test.skip=true -Pprod 这种方式就是指定了打包的参数,并且打包后的文件存放到项目的 target 目录下。

  • 相关阅读:
    JSON,数组根据字段多次分组
    .net c#后台请求接口
    数组的高级应用含ES6 for of 用法
    js获取当前页面url网址信息
    资源管理神器Clover
    ES6的7个实用技巧
    单标签实现气泡三角形
    移动端--上拉加载更多
    checkbox对齐-复选框图标
    textarea显示源代码
  • 原文地址:https://www.cnblogs.com/whx7762/p/7911843.html
Copyright © 2011-2022 走看看