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}会报异常
- <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