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

时间:2023-01-25 11:39:23

I have a Spring Boot application and I have created a Jar out of that. Following is my pom.xml:

我有一个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>

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.

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

How can I fix this issue? How can I add a dependency to a Spring Boot JAR?

我该如何解决这个问题?如何向Spring Boot JAR添加依赖项?

5 个解决方案

#1


63  

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.

默认情况下,Spring Boot将JAR重新打包为可执行的JAR,它通过将所有类放在BOOT-INF / classes和BOOT-INF / lib中的所有依赖库中来实现。创建这个胖JAR的结果是你不能再将它用作其他项目的依赖项。

From Custom repackage classifier:

来自Custom repackage分类器:

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.

原因是应用程序类打包在BOOT-INF / classes中,因此依赖模块无法加载重新打包的jar类。

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>

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 Maven插件将创建2个JAR:主要的一个将与通常的Maven项目相同,而第二个将附加分类器并且是可执行的JAR。

#2


2  

What @Tunaki stated was mostly correct but the one missing part based on your original question was:

@Tunaki所说的大部分是正确的,但根据你原来的问题,一个缺失的部分是:

This throwing ClassNotFoundException. The External jar's used in spring boot application is missing.

这会抛出ClassNotFoundException。缺少Spring Boot应用程序中使用的外部jar。

This is due to the fact that the FatJAR created from the maven packaging has the dependent libraries specified in a specific location that works for how Spring Boot executes the application. If you are just adding the JAR to another application's classpath then you should do what @Tunaki said and also include the dependent JAR files to the classpath. The best way to work with this is to use the Maven Dependency Plugin specifically targetting the dependency:copy-dependencies mojo to download all the dependencies into a folder that you can then specify as a library path when compiling the other application.

这是因为从maven包装创建的FatJAR具有在特定位置指定的依赖库,这些库适用于Spring Boot如何执行应用程序。如果您只是将JAR添加到另一个应用程序的类路径中,那么您应该执行@Tunaki所说的内容,并将依赖的JAR文件包含到类路径中。处理此问题的最佳方法是使用专门针对依赖项的Maven Dependency Plugin:copy-dependencies mojo将所有依赖项下载到一个文件夹中,然后在编译其他应用程序时可以将其指定为库路径。

#3


1  

You can extend your project by maven-assembly-plugin

您可以通过maven-assembly-plugin扩展您的项目

<plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-assembly-plugin</artifactId>        <version>3.1.0</version>        <configuration>        <descriptorRefs>                <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>        </configuration>        <executions>        <execution>            <id>make-assembly</id>            <phase>package</phase>            <goals>            <goal>single</goal>            </goals>            </execution>        </executions></plugin>

After the build you will get 3 jars. The main one will be the same as a usual Maven project, while the second one will have the classifier appended with exec and be the executable JAR. The third jar name will be appended by jar-with-dependencies and will contain your classes with classes added as dependencies in your spring boot application(spring-boot-starter-web, thymeleaf,...), so into the pom of the application where you want to add that project as dependencie you won't have to add dependencies from spring boot project.

构建完成后,您将获得3个罐子。主要的一个将与通常的Maven项目相同,而第二个将具有附加exec的分类器并且是可执行的JAR。第三个jar名称将附加jar-with-dependencies,并将包含您的类,其类在spring启动应用程序中添加为依赖项(spring-boot-starter-web,thymeleaf,...),因此进入pom在您希望将该项目添加为依赖项的应用程序中,您不必从spring boot项目添加依赖项。

#4


0  

You can setup your projects so that the batch launcher relies on a jar, which would be shared with your other application.

您可以设置项目,以便批处理启动程序依赖于jar,该jar将与您的其他应用程序共享。

Said differently, as per your initial request :

换句话说,根据您的初始要求:

I want to use this Jar in my other application so added this jar to my application.

我想在我的其他应用程序中使用此Jar,因此将此jar添加到我的应用程序中。

Let's say your jar is your project A, and your application is your project B.

假设你的jar是你的项目A,你的应用程序就是你的项目B.

Now, what I suggest, is that you remove the launching part from A ; then you put it into a new project C, that would embed Spring Boot, and that would rely almost totally on A.

现在,我建议你从A中移除发射部分;然后你将它放入一个新的项目C,它将嵌入Spring Boot,而这几乎完全依赖于A.

Then, since A is now a simple jar, B can use it as a dependency.

然后,由于A现在是一个简单的jar,B可以将它用作依赖。

#5


0  

any project if you want add as a dependency you need that project <groupId>,<artifactId>,<version>, with these details you can add your project as a dependency in another module or applicationfor ex: your application pom details

任何项目,如果你想要添加为依赖项,你需要项目 , , ,这些细节可以将你的项目作为依赖项添加到另一个模块或应用程序中,例如:你的应用程序pom详细信息

<project         <groupId>com.sample</groupId>        <artifactId>sampleapp</artifactId>        <version>1.0</version>        <packaging>jar</packaging></project>`

your dependency as like below

你的依赖如下

 <dependency> <groupId>com.sample</groupId> <artifactId>sampleapp</artifactId> <version>1.0</version></dependency>

#1


63  

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.

默认情况下,Spring Boot将JAR重新打包为可执行的JAR,它通过将所有类放在BOOT-INF / classes和BOOT-INF / lib中的所有依赖库中来实现。创建这个胖JAR的结果是你不能再将它用作其他项目的依赖项。

From Custom repackage classifier:

来自Custom repackage分类器:

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.

原因是应用程序类打包在BOOT-INF / classes中,因此依赖模块无法加载重新打包的jar类。

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>

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 Maven插件将创建2个JAR:主要的一个将与通常的Maven项目相同,而第二个将附加分类器并且是可执行的JAR。

#2


2  

What @Tunaki stated was mostly correct but the one missing part based on your original question was:

@Tunaki所说的大部分是正确的,但根据你原来的问题,一个缺失的部分是:

This throwing ClassNotFoundException. The External jar's used in spring boot application is missing.

这会抛出ClassNotFoundException。缺少Spring Boot应用程序中使用的外部jar。

This is due to the fact that the FatJAR created from the maven packaging has the dependent libraries specified in a specific location that works for how Spring Boot executes the application. If you are just adding the JAR to another application's classpath then you should do what @Tunaki said and also include the dependent JAR files to the classpath. The best way to work with this is to use the Maven Dependency Plugin specifically targetting the dependency:copy-dependencies mojo to download all the dependencies into a folder that you can then specify as a library path when compiling the other application.

这是因为从maven包装创建的FatJAR具有在特定位置指定的依赖库,这些库适用于Spring Boot如何执行应用程序。如果您只是将JAR添加到另一个应用程序的类路径中,那么您应该执行@Tunaki所说的内容,并将依赖的JAR文件包含到类路径中。处理此问题的最佳方法是使用专门针对依赖项的Maven Dependency Plugin:copy-dependencies mojo将所有依赖项下载到一个文件夹中,然后在编译其他应用程序时可以将其指定为库路径。

#3


1  

You can extend your project by maven-assembly-plugin

您可以通过maven-assembly-plugin扩展您的项目

<plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-assembly-plugin</artifactId>        <version>3.1.0</version>        <configuration>        <descriptorRefs>                <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>        </configuration>        <executions>        <execution>            <id>make-assembly</id>            <phase>package</phase>            <goals>            <goal>single</goal>            </goals>            </execution>        </executions></plugin>

After the build you will get 3 jars. The main one will be the same as a usual Maven project, while the second one will have the classifier appended with exec and be the executable JAR. The third jar name will be appended by jar-with-dependencies and will contain your classes with classes added as dependencies in your spring boot application(spring-boot-starter-web, thymeleaf,...), so into the pom of the application where you want to add that project as dependencie you won't have to add dependencies from spring boot project.

构建完成后,您将获得3个罐子。主要的一个将与通常的Maven项目相同,而第二个将具有附加exec的分类器并且是可执行的JAR。第三个jar名称将附加jar-with-dependencies,并将包含您的类,其类在spring启动应用程序中添加为依赖项(spring-boot-starter-web,thymeleaf,...),因此进入pom在您希望将该项目添加为依赖项的应用程序中,您不必从spring boot项目添加依赖项。

#4


0  

You can setup your projects so that the batch launcher relies on a jar, which would be shared with your other application.

您可以设置项目,以便批处理启动程序依赖于jar,该jar将与您的其他应用程序共享。

Said differently, as per your initial request :

换句话说,根据您的初始要求:

I want to use this Jar in my other application so added this jar to my application.

我想在我的其他应用程序中使用此Jar,因此将此jar添加到我的应用程序中。

Let's say your jar is your project A, and your application is your project B.

假设你的jar是你的项目A,你的应用程序就是你的项目B.

Now, what I suggest, is that you remove the launching part from A ; then you put it into a new project C, that would embed Spring Boot, and that would rely almost totally on A.

现在,我建议你从A中移除发射部分;然后你将它放入一个新的项目C,它将嵌入Spring Boot,而这几乎完全依赖于A.

Then, since A is now a simple jar, B can use it as a dependency.

然后,由于A现在是一个简单的jar,B可以将它用作依赖。

#5


0  

any project if you want add as a dependency you need that project <groupId>,<artifactId>,<version>, with these details you can add your project as a dependency in another module or applicationfor ex: your application pom details

任何项目,如果你想要添加为依赖项,你需要项目 , , ,这些细节可以将你的项目作为依赖项添加到另一个模块或应用程序中,例如:你的应用程序pom详细信息

<project         <groupId>com.sample</groupId>        <artifactId>sampleapp</artifactId>        <version>1.0</version>        <packaging>jar</packaging></project>`

your dependency as like below

你的依赖如下

 <dependency> <groupId>com.sample</groupId> <artifactId>sampleapp</artifactId> <version>1.0</version></dependency>