Maven 区分环境打包
方法一:使用juvenxu.portable-config-maven-plugin
使用方法
- pom文件中引入插件
<plugin>
<groupId>com.juvenxu.portable-config-maven-plugin</groupId>
<artifactId>portable-config-maven-plugin</artifactId>
<version>1.1.4</version>
<executions>
<execution>
<goals>
<goal>replace-package</goal>
</goals>
</execution>
</executions>
</plugin>
- 建立如下的目录结构: 其中portable中对应的是不同环境替换配置文件
4. 替换配置内容:
<?xml version="1.0" encoding="utf-8" ?>
<portable-config>
<config-file path="WEB-INF/classes/properties/db.properties">
<replace key="mysql.password">123456789</replace>
</config-file>
</portable-config>
config-file path 标签对应的是替换配置的路径
replace key 对应的是 配置文件中key,里面的内容对应的是替换配置的内容
其它
打包的时候如果想要排除portable文件内容,则pom文件中可以加入如下配置内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>
WEB-INF/classes/portable/**
</packagingExcludes>
<includeEmptyDirectories>false</includeEmptyDirectories>
</configuration>
</plugin>
方法二:选择指定的目录文件进行打包
- 添加环境参数
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>
- 指定动态的打包替换,打成war包时,排除对应的目录
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<!--动态指定文件-->
<resource>
<directory>src/main/resources/env/${env}</directory>
<targetPath>WEB-INF/classes/properties</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
<packagingExcludes>
**/portable/**,
**/env/**
</packagingExcludes>
<includeEmptyDirectories>false</includeEmptyDirectories>
</configuration>
</plugin>
- 建立对应的目录结构