spring-boot-maven-plugin插件配置classifier打不出-exec.jar

场景:

springboot默认打出的包是无法被依赖的,可以对spring-boot-maven-plugin插件配置classifier属性,生成两个jar包,一个是可执行的jar,一个是可以被依赖的jar


问题描述

配置了spring-boot-maven-plugin插件配置classifier属性,执行package命令,却没有打出-exec.jar包

			<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>

如下,只打出来jar,无-exec.jar
在这里插入图片描述
但是我其他的项目一模一样的配置就可以打出来,相当费解(+_+)?
另一个项目pom如下

	  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.4.8</version>
        <configuration>
          <classifier>exec</classifier>
        </configuration>
      </plugin>

可以打出两个jar包
在这里插入图片描述


原因分析:

其实大概能猜到问题所在,能打出两个jar的项目,引入了spring-boot-starter-parent,现在的这个项目没有用spring-boot-starter-parent,而是使用了spring-boot-dependencies管理版本,没有使用parent引包

PS:之前一直以为spring-boot-starter-parent和spring-boot-dependencies是一样的,都会引包。因为配置了这两个以后,在子项目里引springboot的相关包都不用配置版本,但实际上spring-boot-starter-parent不用写版本是因为他把包都引到项目里了,你在子项目里引springboot的相关包其实是浪费,引不引都能使用,而spring-boot-dependencies不一样,他的功能是版本管理,并不会引入里面的包,子项目如果不引包,在项目里是无法使用的。
哎!对maven的了解还是太粗浅,只会加个包减个包,出问题就刷新≡(▔﹏▔)≡有时间还是要多了解了解

问题大概出在哪里知道了,就好找问题了,无非是少包或者parent存在默认配置


解决方案:

需增加repackage

spring-boot-maven-plugin插件增加如下配置

			<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

再进行package,就可以打出来-exec.jar了
在这里插入图片描述

而使用spring-boot-starter-parent为什么不需要这样配置呢?
因为spring-boot-maven-plugin插件默认在父工程sprint-boot-starter-parent中被指定为repackage,可以点击sprint-boot-starter-parent进入父pom进行查看

		<plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <id>repackage</id>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <mainClass>${start-class}</mainClass>
          </configuration>
        </plugin>

参考:
springboot 打包插件spring-boot-maven-plugin打包机制及内部结构分析


版权声明:本文为qq_42445433原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。