maven profiles多环境配置
maven profiles多环境配置
今天做了一个小项目,需要配置开发、测试、预发布、生产四套环境配置文件,正常是用配置中心的,但是我的项目还没上线,所以想下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}会报异常
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <profile-name>dev</profile-name>
- </properties>
- </profile>
- <profile>
- <id>test</id>
- <properties>
- <profile-name>test</profile-name>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>prod</id>
- <properties>
- <profile-name>prod</profile-name>
- </properties>
- </profile>
- <profile>
- <id>pre</id>
- <properties>
- <profile-name>pre</profile-name>
- </properties>
- </profile>
- </profiles>
步骤三、编写filters ,再次强调${profile-name} 是<profile>的properties里的自定义的名字,profiles 必须指定默认激活环境,参看步骤二
- <build>
- <filters>
- <filter>src/main/filters/application_${profile-name}.properties</filter>
- </filters>
- ...... 省略其它配置
- <build>
步骤四、在build里添加<resources>,maven打包默认会把src/main/resources下的文件都打包,并且指定是否要开启filtering,设置为true 会加载filters文件夹下符合要求的文件中的值
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <excludes>
- <exclude>src/main/resources/application_backup.properties</exclude>
- </excludes>
- </resource>
- </resources>
步骤五、在build里添加maven打包插件,我使用了maven-assembly-plugin自定义打包插件,它需要自定义一个xml文件,
<descriptor>src/main/assembly/package.xml</descriptor>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>3.1.0</version>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- <configuration>
- <descriptors>
- <descriptor>src/main/assembly/package.xml</descriptor>
- </descriptors>
- </configuration>
- </execution>
- </executions>
- </plugin>
package.xml文件
- <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
- <id>${profile-name}</id>
- <formats>
- <format>zip</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <fileSets>
- <fileSet>
- <directory>${basedir}/src/main/resources/</directory>
- <includes>
- <include>application.properties</include>
- </includes>
- <outputDirectory>config</outputDirectory>
- <filtered>true</filtered>
- </fileSet>
- <fileSet>
- <directory>${basedir}/target/</directory>
- <includes>
- <include>${project.artifactId}-${project.version}.jar</include>
- </includes>
- <outputDirectory>.</outputDirectory>
- </fileSet>
- <fileSet>
- <directory>${basedir}/target/scripts/</directory>
- <includes>
- <include>*.sh</include>
- </includes>
- <directoryMode>755</directoryMode>
- <outputDirectory>bin</outputDirectory>
- </fileSet>
- <fileSet>
- <directory>${project.build.outputDirectory}/docs</directory>
- <includes>
- <include>*.*</include>
- </includes>
- <outputDirectory>docs</outputDirectory>
- </fileSet>
- </fileSets>
- </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多环境配置的更多相关文章
- Eclipse Maven profiles 多环境配置,测试环境与开发环境分开打包
1.将开发环境.测试环境.生产环境的配置文件分开存放,如下图: 2.在Maven中配置不同的环境打包配置文件的路径,配置如下: <profiles> <profile> < ...
- maven的多环境配置
<profiles> <profile> <id>dev</id> <activation> <activeByDefault> ...
- Jenkins Jenkins结合GIT Maven持续集成环境配置
Jenkins结合GIT Maven持续集成环境配置 by:授客 QQ:1033553122 安装Git插件 1 安装Git客户端 1 安装JAVA JDK及MAVEN 2 Jenkins JDK ...
- Maven为不同环境配置打包
在开发过程中经常要遇到为不同的环境打包,这里面最主要的问题在于,不同环境的配置是不一样的,如果为不同环境打包每次都手工修改配置,那不但工作量大,而且很容易出错.如果用ant的话,用变量加上replac ...
- Maven 多套环境配置
在Java开发中会配置不同环境,可通过Maven的profile指定不同的环境配置,pom.xml配置如下: <project xmlns="http://maven.apache.o ...
- Java(JDK/Tomcat/Maven)运行环境配置及工具(idea/eclipse)安装
Java (计算机编程语言) Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. Java语 ...
- SpringBoot Profiles 多环境配置及切换
目录 前言 默认环境配置 多环境配置 多环境切换 小结 前言 大部分情况下,我们开发的产品应用都会根据不同的目的,支持运行在不同的环境(Profile)下,比如: 开发环境(dev) 测试环境(tes ...
- maven 多套环境 配置(开发、测试、预发、正式)
接上一节继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre ...
- Maven安装与环境配置(Windows)
1.下载安装包 在Maven官网下载最新版的安装包:http://maven.apache.org/download.cgi 2.解压安装包 3.配置Maven环境变量 配置M2_HOME环境变量,指 ...
随机推荐
- S标签和C标签
<s:if test="#attr.info.RLZT==1"> <a style="cursor:hand;" onclick=" ...
- GO语言strconv包的使用
Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,官方文档中文版. string与int类型转换 Ato ...
- apache不能解析php之解决办法
记录一下Ubuntu16.04下的apache服务器不能解析php的问题,如图所示: 对于这个Ubuntu16.04最直接最快速的解决办法就是执行如下命令: sudo apt-get install ...
- ubuntu之路——day11.1 如何进行误差分析
举个例子 还是分类猫图片的例子 假设在dev上测试的时候,有100张图片被误分类了.现在要做的就是手动检查所有被误分类的图片,然后看一下这些图片都是因为什么原因被误分类了. 比如有些可能因为被误分类为 ...
- Chrome 9229
Enable Inspector When started with the --inspect switch, a Node.js process listens for a debugging c ...
- 优化Unity游戏项目的脚本(下)
金秋9月,我们祝所有的老师们:教师节快乐 ! 今天,我们继续分享来自捷克的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法. 在优化Unity游戏项目的脚本(上)中,我们介绍了如何查 ...
- 微信小程序wx.uploadFile的两个坑
-- setImage:function(e){ var _this = this //坑1 wx.chooseImage({ count: , sizeType: ['original', 'com ...
- 一个完整的类用来读取OpenSSL生成的pem格式的x509证书(C#)
internal static class CcbRsaHelper { private const string Begin = "-----BEGIN "; private c ...
- AI学习网址记录
https://ai.yanxishe.com/ https://ai.yanxishe.com/page/blogDetail/14365 GAN网络 对抗式生成网络-图像超分辨及图像修复 完全可见 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_22、SpringBoot启动方式和部署war项目到tomcat9
笔记 1.SpringBoot启动方式讲解和部署war项目到tomcat9 简介:SpringBoot常见启动方式讲解和部署war项目Tomcat 1.ide启动 2.jar包方式启动 ...