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

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

我有一个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添加依赖项?


慕尼黑的夜晚无繁华
浏览 1224回答 3
3回答

慕盖茨4494581

默认情况下,Spring Boot会将JAR重新打包为可执行的JAR,并将所有类放入其中BOOT-INF/classes,并将所有依赖库放入其中BOOT-INF/lib。创建这个胖JAR的结果是你不能再将它用作其他项目的依赖项。来自Custom repackage分类器:默认情况下,repackage目标将使用重新包装的工件替换原始工件。对于代表应用程序的模块而言,这是一种理智的行为,但如果您的模块用作另一个模块的依赖项,则需要为重新打包的模块提供分类器。原因是应用程序类被打包,BOOT-INF/classes以便依赖模块无法加载重新打包的jar类。如果要保留原始主工件以将其用作依赖关系,可以classifier在repackage目标配置中添加:<plugin> &nbsp;&nbsp;<groupId>org.springframework.boot</groupId> &nbsp;&nbsp;<artifactId>spring-boot-maven-plugin</artifactId> &nbsp;&nbsp;<version>1.4.1.RELEASE</version> &nbsp;&nbsp;<executions> &nbsp;&nbsp;&nbsp;&nbsp;<execution> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<goals> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<goal>repackage</goal> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</goals> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<configuration> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<classifier>exec</classifier> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</configuration> &nbsp;&nbsp;&nbsp;&nbsp;</execution> &nbsp;&nbsp;</executions></plugin>使用此配置,Spring Boot Maven插件将创建2个JAR:主要的一个将与通常的Maven项目相同,而第二个将附加分类器并且是可执行的JAR。

慕斯王

Tunaki的答案是正确的,但在Spring Boot 2中不起作用。Spring Boot&nbsp;1.x&nbsp;&nbsp;<plugin> &nbsp;&nbsp;&nbsp;&nbsp;<groupId>org.springframework.boot</groupId> &nbsp;&nbsp;&nbsp;&nbsp;<artifactId>spring-boot-maven-plugin</artifactId> &nbsp;&nbsp;&nbsp;&nbsp;<version>1.5.20.RELEASE</version> &nbsp;&nbsp;&nbsp;&nbsp;<executions> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<execution> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<goals> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<goal>repackage</goal> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</goals> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<configuration> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<classifier>exec</classifier> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</configuration> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</execution> &nbsp;&nbsp;&nbsp;&nbsp;</executions> &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;</plugin>阅读更多Spring Boot&nbsp;2.x如果您正在使用spring-boot-starter-parent,repackage目标将在具有id的执行中自动执行repackage。在该设置中,只应指定配置,如以下示例所示:&nbsp;&nbsp;<plugin> &nbsp;&nbsp;&nbsp;&nbsp;<groupId>org.springframework.boot</groupId> &nbsp;&nbsp;&nbsp;&nbsp;<artifactId>spring-boot-maven-plugin</artifactId> &nbsp;&nbsp;&nbsp;&nbsp;<executions> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<execution> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<id>repackage</id> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<configuration> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<classifier>exec</classifier> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</configuration> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</execution> &nbsp;&nbsp;&nbsp;&nbsp;</executions> &nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;</plugin>阅读更多
打开App,查看更多内容
随时随地看视频慕课网APP