如何在另一个项目中向Spring Boot Jar添加依赖项? [英] How to add a dependency to a Spring Boot Jar in another project?

查看:155
本文介绍了如何在另一个项目中向Spring Boot Jar添加依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 目标将用重新包装的目标替换原始工件。对于代表应用程序的模块而言,这是一种理智的行为,但如果您的模块用作另一个模块的依赖项,则需要为重新打包的模块提供分类器。

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.

原因是应用程序类打包在 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.

如果要保留原始主工件以将其用作依赖项,可以添加 classifier 重新包装目标配置:

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添加依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆