Spring重新打包正在BOOT-INF/classes中添加所有spring jar的副本

wztqucjr  于 2023-02-11  发布在  Spring
关注(0)|答案(1)|浏览(152)

我最近将我的项目升级到了spring-boot版本2.7.0。升级之后,当我运行maven配置文件的jar时,它会给我一个invocationTargetException

Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)

我注意到我构建的jar现在包含了BOOT-INF/classes目录中所有org/springframework/boot/loader/库的副本,这是我通过运行jar tvf jarfile查看jar文件时看到的结果:

0 Wed Jun 29 20:06:28 PDT 2022 META-INF/
   492 Wed Jun 29 20:06:28 PDT 2022 META-INF/MANIFEST.MF
     0 Fri Feb 01 00:00:00 PST 1980 org/
     0 Fri Feb 01 00:00:00 PST 1980 org/springframework/
     0 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/
     0 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/
  5871 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/ClassPathIndexFile.class
  7675 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/ExecutableArchiveLauncher.class
  2551 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/JarLauncher.class
  1483 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/LaunchedURLClassLoader$DefinePackageCallType.class
  1535 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
 11154 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/LaunchedURLClassLoader.class
  5932 Fri Feb 01 00:00:00 PST 1980 org/springframework/boot/loader/Launcher.class

...

     0 Wed Jun 29 20:06:28 PDT 2022 BOOT-INF/
     0 Wed Jun 29 20:06:28 PDT 2022 BOOT-INF/classes/
     0 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/
     0 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/
     0 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/
     0 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/
  5871 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/ClassPathIndexFile.class
  7675 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/ExecutableArchiveLauncher.class
  2551 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/JarLauncher.class
  1483 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/LaunchedURLClassLoader$DefinePackageCallType.class
  1535 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
 11154 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/LaunchedURLClassLoader.class
  5932 Fri Feb 01 00:00:00 PST 1980 BOOT-INF/classes/org/springframework/boot/loader/Launcher.class

在我的pom.xml中,我有一个针对特定配置文件的重新打包步骤:

<profile>
            <id>profileName</id>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                  <execution>
                    <id>repackage</id>
                    <configuration>
                       <classifier>someName</classifier>
                    </configuration>
                  </execution>
                </executions>
            </plugin>
</profile>

这个配置文件给了我一个损坏的jar。在 Spring 升级之前,它甚至可以在重新打包步骤中工作,所以一定有什么地方发生了变化。我如何避免在我的jar中出现重复的Spring Boot 库?

w41d8nur

w41d8nur1#

请生成Maven有效POM并搜索repackage,您可以找到两个具有repackage目标但执行id不同的执行,这意味着repackage将执行两次,因此这些spring Boot 类将被打包两次。在第二次执行时,它将把第一次运行添加的spring boot类视为正常应用程序类,并将其添加到BOOT-INF/classes中。
如果您使用的是spring-boot-starter-parent 2.1.0或更高版本,则不会发生这种情况,因为重新打包ID是从2.1.0版本开始预定义的。目标将只运行一次,因为您在POM中引用了相同的ID。在2.1.0版本之前,有一个预先配置的重新打包目标,但没有配置重新打包ID,因此2个重新打包目标将在2次执行中运行。
考虑到您使用的是2.7.0,可能POM中的某些依赖项引入了额外的重新打包目标,但是使用了不同的执行id。
您可以参考Sping Boot Maven插件文档https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#packaging

相关问题