maven profiles多环境配置

转载。 https://blog.csdn.net/runbat/article/details/81747874

今天做了一个小项目,需要配置开发、测试、预发布、生产四套环境配置文件,正常是用配置中心的,但是我的项目还没上线,所以想下maven的profiles+filters,我看了下官网对profiles的介绍,官网profiles地址,一个字,累。

开始吧,要想实现多环境打包,总共分6步

步骤一、在src/main 下创建filters文件夹,maven文件目录结构里建议有filters文件夹(感兴趣的可以自己查下maven文件结构),这个和filters配合使用

步骤二、编写profiles, 这里需要说明两点 1:<properties>节点里的名字[profile-name] 这个可以自己定义,后面的<filter>节点要用到

2:  <activation><activeByDefault>true</activeByDefault></activation> 默认激活环境,一定要有,不然${profile-name}会报异常

  1.  
    <profiles>
  2.  
    <profile>
  3.  
    <id>dev</id>
  4.  
    <properties>
  5.  
    <profile-name>dev</profile-name>
  6.  
    </properties>
  7.  
    </profile>
  8.  
    <profile>
  9.  
    <id>test</id>
  10.  
    <properties>
  11.  
    <profile-name>test</profile-name>
  12.  
    </properties>
  13.  
    <activation>
  14.  
    <activeByDefault>true</activeByDefault>
  15.  
    </activation>
  16.  
    </profile>
  17.  
    <profile>
  18.  
    <id>prod</id>
  19.  
    <properties>
  20.  
    <profile-name>prod</profile-name>
  21.  
    </properties>
  22.  
    </profile>
  23.  
    <profile>
  24.  
    <id>pre</id>
  25.  
    <properties>
  26.  
    <profile-name>pre</profile-name>
  27.  
    </properties>
  28.  
    </profile>
  29.  
    </profiles>

步骤三、编写filters ,再次强调${profile-name} 是<profile>的properties里的自定义的名字,profiles 必须指定默认激活环境,参看步骤二

  1.  
    <build>
  2.  
    <filters>
  3.  
    <filter>src/main/filters/application_${profile-name}.properties</filter>
  4.  
    </filters>
  5.  
    ...... 省略其它配置
  6.  
    <build>

步骤四、在build里添加<resources>,maven打包默认会把src/main/resources下的文件都打包,并且指定是否要开启filtering,设置为true 会加载filters文件夹下符合要求的文件中的值

  1.  
    <resources>
  2.  
    <resource>
  3.  
    <directory>src/main/resources</directory>
  4.  
    <filtering>true</filtering>
  5.  
    <excludes>
  6.  
    <exclude>src/main/resources/application_backup.properties</exclude>
  7.  
    </excludes>
  8.  
    </resource>
  9.  
    </resources>

步骤五、在build里添加maven打包插件,我使用了maven-assembly-plugin自定义打包插件,它需要自定义一个xml文件,

<descriptor>src/main/assembly/package.xml</descriptor>
  1.  
    <plugin>
  2.  
    <artifactId>maven-assembly-plugin</artifactId>
  3.  
    <version>3.1.0</version>
  4.  
    <executions>
  5.  
    <execution>
  6.  
    <id>make-assembly</id>
  7.  
    <phase>package</phase>
  8.  
    <goals>
  9.  
    <goal>single</goal>
  10.  
    </goals>
  11.  
    <configuration>
  12.  
    <descriptors>
  13.  
    <descriptor>src/main/assembly/package.xml</descriptor>
  14.  
    </descriptors>
  15.  
    </configuration>
  16.  
    </execution>
  17.  
    </executions>
  18.  
    </plugin>

package.xml文件

  1.  
    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  2.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  4.  
    <id>${profile-name}</id>
  5.  
    <formats>
  6.  
    <format>zip</format>
  7.  
    </formats>
  8.  
    <includeBaseDirectory>false</includeBaseDirectory>
  9.  
    <fileSets>
  10.  
    <fileSet>
  11.  
    <directory>${basedir}/src/main/resources/</directory>
  12.  
    <includes>
  13.  
    <include>application.properties</include>
  14.  
    </includes>
  15.  
    <outputDirectory>config</outputDirectory>
  16.  
    <filtered>true</filtered>
  17.  
    </fileSet>
  18.  
    <fileSet>
  19.  
    <directory>${basedir}/target/</directory>
  20.  
    <includes>
  21.  
    <include>${project.artifactId}-${project.version}.jar</include>
  22.  
    </includes>
  23.  
    <outputDirectory>.</outputDirectory>
  24.  
    </fileSet>
  25.  
    <fileSet>
  26.  
    <directory>${basedir}/target/scripts/</directory>
  27.  
    <includes>
  28.  
    <include>*.sh</include>
  29.  
    </includes>
  30.  
    <directoryMode>755</directoryMode>
  31.  
    <outputDirectory>bin</outputDirectory>
  32.  
    </fileSet>
  33.  
    <fileSet>
  34.  
    <directory>${project.build.outputDirectory}/docs</directory>
  35.  
    <includes>
  36.  
    <include>*.*</include>
  37.  
    </includes>
  38.  
    <outputDirectory>docs</outputDirectory>
  39.  
    </fileSet>
  40.  
    </fileSets>
  41.  
    </assembly>

步骤六、打包 -P 指定打包环境,-Dmaven.test.skip=true 跳过测试,不然的话会检查配置文件是否能正常连接等等,比如会测试zk是否可用啊什么的,如果不可以打包会失败的。开发环境和线上肯定是隔离的,所以要指定跳过检查。生成的包在项目目录下的target文件夹下,mvn package会生成target文件夹  mvn clean会清除target文件夹的内容。

mvn package -P [profile-name] -Dmaven.test.skip=true

maven profiles多环境配置的更多相关文章

  1. Eclipse Maven profiles 多环境配置,测试环境与开发环境分开打包

    1.将开发环境.测试环境.生产环境的配置文件分开存放,如下图: 2.在Maven中配置不同的环境打包配置文件的路径,配置如下: <profiles> <profile> < ...

  2. maven的多环境配置

    <profiles> <profile> <id>dev</id> <activation> <activeByDefault> ...

  3. Jenkins Jenkins结合GIT Maven持续集成环境配置

    Jenkins结合GIT Maven持续集成环境配置   by:授客 QQ:1033553122 安装Git插件 1 安装Git客户端 1 安装JAVA JDK及MAVEN 2 Jenkins JDK ...

  4. Maven为不同环境配置打包

    在开发过程中经常要遇到为不同的环境打包,这里面最主要的问题在于,不同环境的配置是不一样的,如果为不同环境打包每次都手工修改配置,那不但工作量大,而且很容易出错.如果用ant的话,用变量加上replac ...

  5. Maven 多套环境配置

    在Java开发中会配置不同环境,可通过Maven的profile指定不同的环境配置,pom.xml配置如下: <project xmlns="http://maven.apache.o ...

  6. Java(JDK/Tomcat/Maven)运行环境配置及工具(idea/eclipse)安装

    Java (计算机编程语言) Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. Java语 ...

  7. SpringBoot Profiles 多环境配置及切换

    目录 前言 默认环境配置 多环境配置 多环境切换 小结 前言 大部分情况下,我们开发的产品应用都会根据不同的目的,支持运行在不同的环境(Profile)下,比如: 开发环境(dev) 测试环境(tes ...

  8. maven 多套环境 配置(开发、测试、预发、正式)

    接上一节继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre ...

  9. Maven安装与环境配置(Windows)

    1.下载安装包 在Maven官网下载最新版的安装包:http://maven.apache.org/download.cgi 2.解压安装包 3.配置Maven环境变量 配置M2_HOME环境变量,指 ...

随机推荐

  1. 根据字符串从资源中取出对应的资源ResourceManager.GetObject

    ResourceManager.GetObject 根据名称从资源中取出资源 需要:我有25张 五笔图片名称是a b c d ...y 这样组成的 每张图片名字只有一个名字 我想通过字符串a取出a这张 ...

  2. Oracle表空间 与 分页

    目录 Oracle表空间 表空间的构建以及数据文件的挂接 创建一个用户,并指定专有的永久表空间和临时表空间 伪列 分页 Oracle表空间 一个表空间可以包含1至n个数据文件 一个数据文件必然属于某个 ...

  3. html5中progress/meter元素

    html5中progress/meter元素 一.总结 一句话总结: progress元素:用来建立一个进度条 meter元素的作用:用来建立一个度量条,用来表示度量衡的评定 <progress ...

  4. Unexpected token o in JSON at position 1 报错原因

    写在前面的话这个问题在之前做项目时碰到过一次,当时按照网上的做法,去掉JSON.parse()这一层转换后就没有这个报错了,数据也能正常使用,就没多想,也没深究是什么原因. 可是这次又碰到了,所以这次 ...

  5. You are using the runtime-only build of Vue where the template compiler is not available. Either pre

    在升级脚手架到vue-cli3.0版本的时候出现了这个报错: [Vue warn]: You are using the runtime-only build of Vue where the tem ...

  6. 无法调用到appcode下的类

    解决方法: 右键 appp_code下的类, 点击 “属性”, 里面 [生成操作] 一项 由内容 改为 编译 即可

  7. C/C++ socket编程教程之九:TCP的粘包问题以及数据的无边界性

    C/C++ socket编程教程之九:TCP的粘包问题以及数据的无边界性 上节我们讲到了socket缓冲区和数据的传递过程,可以看到数据的接收和发送是无关的,read()/recv() 函数不管数据发 ...

  8. xcopy命令拷贝文件夹和文件

    文件夹: xcopy /r /y 'c:123\' 'D:\123\' 文件: echo f | xcopy  /d /r /k  c:\index2.htm c:\index.htm

  9. osgViewer

    /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source ...

  10. OpenLDAP配置坎坷路

    https://segmentfault.com/a/1190000014683418 轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDA ...