You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何编译无版本号的spring-boot-maven-plugin?及生成无版本可执行Jar

这两个问题其实都可以通过Maven的配置轻松解决,我来帮你一步步梳理:

1. 不指定版本号使用spring-boot-maven-plugin

Spring Boot已经通过依赖管理机制帮我们维护了官方插件的版本,所以你不需要手动指定版本号,分两种常见场景处理:

  • 场景1:项目继承了spring-boot-starter-parent
    如果你的pom.xml里已经有如下继承配置,直接使用spring-boot-maven-plugin时无需写<version>标签,parent会自动引入匹配的插件版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>你的Spring Boot版本</version> <!-- 仅需在这里指定一次Spring Boot版本 -->
    </parent>
    
  • 场景2:未继承spring-boot-starter-parent
    这种情况下,你需要在<dependencyManagement>中引入Spring Boot的依赖管理,插件版本会被自动同步:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>你的Spring Boot版本</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    之后配置spring-boot-maven-plugin时,同样不需要手动指定<version>

2. 修改生成的Jar文件名为auth_service.jar

默认Maven会用artifactId-version作为Jar文件名,你只需在pom.xml的<build>标签下添加<finalName>配置,就能覆盖默认规则:

完整的配置示例如下:

<build>
    <!-- 指定最终生成的Jar文件名 -->
    <finalName>auth_service</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 若已通过parent或dependencyManagement管理版本,此处无需version -->
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

配置完成后执行mvn package命令,生成的可执行Jar就会是auth_service.jar了。

内容的提问来源于stack exchange,提问作者Oleg Zdanevich

火山引擎 最新活动