问题描述
我有一个 Spring Boot 应用程序,我用它创建了一个 Jar.以下是我的pom.xml
:
I have a Spring Boot application and I have created a Jar out of that. Following is my 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
.
I want to use this Jar in my other application so added this jar to my application. But when I am calling a method in that Jar, it is throwing a ClassNotFoundException
.
我该如何解决这个问题?如何向 Spring Boot JAR 添加依赖项?
How can I fix this issue? How can I add a dependency to a Spring Boot JAR?
推荐答案
默认情况下,Spring Boot 将你的 JAR 重新打包成一个可执行的 JAR,它通过将你的所有类放在 BOOT-INF/classes
,以及 BOOT-INF/lib
中的所有依赖库.创建这个胖 JAR 的后果是您不能再将它用作其他项目的依赖项.
By default, Spring Boot repackages your JAR into an executable JAR, and it does that by putting all of your classes inside BOOT-INF/classes
, and all of the dependent libraries inside BOOT-INF/lib
. The consequence of creating this fat JAR is that you can no longer use it as a dependency for other projects.
来自 自定义重新打包分类器:
默认情况下,repackage
目标将用重新打包的工件替换原始工件.对于代表应用的模块来说,这是一种理智的行为,但如果您的模块被用作另一个模块的依赖项,则您需要为重新打包的模块提供一个分类器.
这样做的原因是应用程序类打包在BOOT-INF/classes
中,因此依赖模块无法加载重新打包的 jar 类.
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.
如果你想保留原来的 main artifact 以便将其用作依赖,你可以添加一个 :
If you want to keep the original main artifact in order to use it as a dependency, you can add a classifier
in the repackage
goal configuration:
<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.
With this configuration, the Spring Boot Maven Plugin will create 2 JARs: the main one will be the same as a usual Maven project, while the second one will have the classifier appended and be the executable JAR.
这篇关于如何向另一个项目中的 Spring Boot Jar 添加依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!