方法一:
首先在/resource目录下创建各自环境的配置
要在不同的环境中使用不同的配置文件进行Maven打包,可以使用Maven的profiles特性和资源过滤功能。下面是配置Maven打包参数的步骤:
在项目的pom.xml文件中,添加profiles配置。在标签下,为每个环境定义一个profile。示例代码如下:
<profiles>
<!-- mvn -DskipTests=true clean package -Pprod -->
<profile>
<id>prod</id>
<properties>
<package.environment>prod</package.environment>
</properties>
<activation>
<!-- 默认的,不加参数时执行这个profile
<activeByDefault>true</activeByDefault> -->
</activation>
</profile>
<!-- mvn -DskipTests=true clean package -Ptest -->
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
2.在标签内的标签下,添加插件创建的配置。示例代码如下:
<build>
<finalName>${project.name}-${maven.build.timestamp}</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals> <goal>shade</goal> </goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>prod/*.*</exclude>
<exclude>test/*.*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
3.配置resource插件
<!-- resource插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 覆盖原有文件 -->
<overwrite>true</overwrite>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<!-- 待处理的资源定义 -->
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/resources/${package.environment}</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
4.mvn打包命令
#mvn 打包命令
#生产环境
mvn -DskipTests=true clean package -Pprod
#开发环境
mvn -DskipTests=true clean package -Ptest