mvn打包生成版本后缀,同时支持生产及开发环境运行输出版本号
以下是的完整步骤梳理,
·
以下是 Spring Boot 项目中实现“在启动时打印版本号,并确保 IDE 和打包都能正常使用” 的完整步骤梳理,适用于大多数实际生产开发项目:
✅ 一次性搞定的实现步骤
🧩 第 1 步:在 pom.xml 中配置版本号
<groupId>com.example</groupId>
<artifactId>aixwfx-web</artifactId>
<version>2.2.33</version>
<name>xx平台</name>
📌 保证项目的版本号只维护在
<version>中。
🧩 第 2 步:配置 spring-boot-maven-plugin 自动生成 build-info
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
✅ 执行
mvn package后,会在 JAR 中生成META-INF/build-info.properties。
🧩 第 3 步:手动创建一个默认版本信息(供 IDE 启动时使用)
创建文件:
src/main/resources/META-INF/build-info.properties
内容:
build.version=dev
build.name=aixwfx-web
build.time=2025-06-19T00:00:00Z
✅ 该文件在 IDE运行时被使用,打包时会被自动覆盖,不影响最终构建。
🧩 第 4 步:编写代码注入 BuildProperties 并打印版本号
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.info.BuildProperties;
import org.springframework.stereotype.Component;
@Component
public class VersionLogger implements ApplicationRunner {
private final BuildProperties buildProperties;
public VersionLogger(BuildProperties buildProperties) {
this.buildProperties = buildProperties;
}
@Override
public void run(ApplicationArguments args) {
System.out.println("应用启动,版本号:" + buildProperties.getVersion());
}
}
✅ 你也可以打印更多如 build.time、build.name、build.artifact 等。
🧩 第 5 步:运行验证
✅ 本地开发:
-
直接在 IDEA 启动 Spring Boot,看到输出:
应用启动,版本号:dev
✅ 正式打包:
-
执行打包:
mvn clean package -
运行 JAR:
java -jar target/aixwfx-web-2.2.33.jar -
控制台输出:
应用启动,版本号:2.2.33
输出svn 修订号到jar包
<scm>
<connection>scm:svn:http://svn.example.com/repos/aixwfx/trunk</connection>
<developerConnection>scm:svn:http://svn.example.com/repos/aixwfx/trunk</developerConnection>
<url>http://svn.example.com/repos/aixwfx/trunk</url>
</scm>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- <excludes>-->
<!-- <exclude>**/*.yml</exclude>-->
<!-- </excludes>-->
</resource>
</resources>
<finalName>${project.artifactId}-${project.version}.${buildNumber}</finalName>
<plugins>
<!-- 插件1:提取SVN修订号 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 设置输出的 revision 变量 -->
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<shortRevisionLength>4</shortRevisionLength>
<scmDirectory>${project.basedir}</scmDirectory> <!-- 明确指定 SCM 目录 -->
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
更多推荐




所有评论(0)