一、技术背景
在不同的环境,要打不同版本的API包,开发测试环境打SNAPSHOT,线上要打RELEASE包,但是每次都要上线之前都要改代码,很麻烦,所以用更通用的配置来解决这个问题。
二、实现
pom文件配置
<groupId>com.yx</groupId>
<artifactId>yx-test-api</artifactId>
<version>${version.final}</version>
<name>test-api</name>
<properties>
<java.version>1.8</java.version>
<version.current>1.0.0</version.current>
<project.version>1.0.0</project.version>
</properties>
<distributionManagement>
<repository>
<id>nexus</id>
<name>releases</name>
<url>http:</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>snapshots</name>
<url>http:</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>Nexus</id>
<name>Nexus Public Repository</name>
<url>http:</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<active>dev</active>
<version.final>${version.current}-SNAPSHOT</version.final>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<active>test</active>
<version.final>${version.current}-RELEASE</version.final>
</properties>
<activation>
<!-- 默认环境 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<active>prod</active>
<version.final>${project.version}-RELEASE</version.final>
</properties>
</profile>
</profiles>
其中<active> 标签,是根据application.properties的配置
#环境
spring.profiles.active=@active@
标签内dev,test,prod取值
根据不同环境的配置名称,如下图
参考大佬:
Spring boot 使用profile完成不同环境的maven打包功能