gpt4 book ai didi

maven - 如何在另一个项目中向Spring Boot Jar添加依赖项?

转载 作者:行者123 更新时间:2023-12-03 05:50:26 27 4
gpt4 key购买 nike

我有一个Spring Boot应用程序,并由此创建了一个Jar。以下是我的pom.xml:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- WebJars -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

我想在其他应用程序中使用该Jar,因此将该jar添加到了我的应用程序中。但是,当我在那个Jar中调用方法时,它会抛出 ClassNotFoundException

如何解决此问题?如何为Spring Boot JAR添加依赖项?

最佳答案

默认情况下,Spring Boot将您的JAR重新打包为可执行的JAR,并通过将您的所有类放入BOOT-INF/classes以及所有相关库放入BOOT-INF/lib来实现。创建此胖JAR的结果是您不能再将其用作其他项目的依赖项。

Custom repackage classifier:

By default, the repackage goal will replace the original artifact with the repackaged one. That's a sane behaviour for modules that represent an app but if your module is used as a dependency of another module, you need to provide a classifier for the repackaged one.

The reason for that is that application classes are packaged in BOOT-INF/classes so that the dependent module cannot load a repackaged jar's classes.



如果要保留原始主要 Artifact 以将其用作依赖项,则可以在 classifier目标配置中添加 repackage :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>

通过这种配置,Spring Boot Maven插件将创建2个JAR:主要的将与通常的Maven项目相同,而第二个将附加分类器并成为可执行的JAR。

关于maven - 如何在另一个项目中向Spring Boot Jar添加依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48146168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com