在实际开发过程中,可能需要不断进行环境的切换和打包部署,通常我们会选择在application.properties
中修改不同环境对应的配置文件,这种方式不仅效率低,而且很容易发生错误,造成不必要的麻烦降低工作效率。maven提供了多环境配置,可以方便实现不同环境的配置切换和打包。
多环境打包
pom.xml
文件加上根据环境来打包.为了不把多余的其它环境的配置打到包里面。
打包命令
mvn clean package
mvn clean package -Pdev
mvn clean package -Ptest
mvn clean package -Pprod
执行命令,指定-P参数,启用指定的profile。
pom.xml
文件例子:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!-- maven-compiler-plugin用来编译Java代码 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- maven-resources-plugin用来处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version> <!-- @wjw_note: 指定版本号,解决maven.plugin.MojoExecutionException: Input length = 1异常 -->
<configuration>
<propertiesEncoding>UTF-8</propertiesEncoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<filtering>true</filtering>
<!-- 指定配置文件所在的resource目录 -->
<directory>src/main/resources</directory>
<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}.xml</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.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-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
...
</project>
<<<<<<<<<<<< 🏆 >>>>>>>>>>>>