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

标签 maven spring-boot jar spring-boot-maven-plugin

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

相关文章:

java - 在 maven 版本上更新 README 中包含的版本

java - java项目转maven后找不到主类

java - 如果使用maven,通常你把log4j.properties放在java或resources下?

java - 在 Jackson/Spring Boot 中测试自定义 Json Deserializer

html - 如何使用 Thymeleaf 和 Spring Boot 将 UI 中的对象集合绑定(bind)到后端

java - 用于登录的WebSecurityConfigurerAdapter的配置

java - 使用 weblogic 部署计划更新 JAR 中的 xml 文件

java - Maven:如何 "best"也使用maven来下载 "project related files"(如配置.xml)?

java - 在 jar 之前提取文件路径

java - 如何从 META-INF 目录加载属性文件?