在实际开发过程中,可能需要不断进行环境的切换和打包部署,通常我们会选择在application.properties
中修改不同环境对应的配置文件,这种方式不仅效率低,而且很容易发生错误,造成不必要的麻烦降低工作效率。maven提供了多环境配置,可以方便实现不同环境的配置切换和打包。
pom.xml
文件加上根据环境来打包.为了不把多余的其它环境的配置打到包里面。
打包命令
mvn clean package
mvn clean package -Pdev
mvn clean package -Ptest
mvn clean package -Pprod
执行命令,指定-P参数,启用指定的profile。
pom.xml
文件例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<version>3.1.0version>
<configuration>
<propertiesEncoding>UTF-8propertiesEncoding>
configuration>
plugin>
plugins>
<resources>
<resource>
<filtering>truefiltering>
<directory>src/main/resourcesdirectory>
<includes>
<include>public/**include>
<include>static/**include>
<include>templates/**include>
<include>tpl/**include>
<include>application.*include>
<include>application-${spring.profiles.active}.*include>
<include>log*-${spring.profiles.active}.xmlinclude>
includes>
resource>
resources>
build>
<profiles>
<profile>
<id>devid>
<properties>
<spring.profiles.active>devspring.profiles.active>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>testid>
<properties>
<spring.profiles.active>testspring.profiles.active>
properties>
profile>
<profile>
<id>prodid>
<properties>
<spring.profiles.active>prodspring.profiles.active>
properties>
profile>
profiles>
project>
为了提升下载速度.
pom.xml
例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<repositories>
<repository>
<id>nexus-aliyunid>
<name>nexus-aliyunname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
...
project>
<<<<<<<<<<<< 🏆 >>>>>>>>>>>>