【问题标题】:How to Import Spring Boot Jar Into Other Spring Boot Jar Maven Base如何将 Spring Boot Jar 导入其他 Spring Boot Jar Maven Base
【发布时间】:2019-03-07 05:34:57
【问题描述】:


我需要帮助。我有 2 个 spring-boot-jar 项目 maven 基础,我想将第一个导入到第二个中,如图

我在 Config Spring-Boot 中导入 Util Jar 作为打击并运行 Spring Boot 但它显示错误

<dependency>
    <groupId>com.ballistic</groupId>
    <artifactId>adminutil</artifactId>
    <version>0.1</version>
</dependency>

[错误] 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project config: Compilation failure: Compilation failure:


Pom.xm Util-Jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.ballistic</groupId>
<artifactId>adminutil</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>adminutil</name>

<description>
    Util For Admin Tool Help In All Jar's
</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Add Log4j2 Dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <!-- Needed for Async Logging with Log4j 2 -->
    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.4.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Pom.xm 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.ballistic</groupId>
<artifactId>config</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>config</name>

<!--
 mvn install:install-file -Dfile=C:\Users\AdMaxim\Desktop\App\adminutil\target\adminutil-0.1.jar -DgroupId=com.ballistic -DartifactId=adminutil -Dversion=0.1 -Dpackaging=jar
 -->
<description>
    Config Help The Configuration Of All Sub Jar
</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>com.ballistic</groupId>
        <artifactId>adminutil</artifactId>
        <version>0.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 发布两个项目的 pom.xml
  • 请显示编译错误...这是您发布的消息上方的行。
  • @ArunKumar 请检查一下,错误是在 util jar 中找不到类
  • 由于 jar 文件内部的结构,您不能在另一个 jar 中使用 Spring Boot jar 作为依赖项。我也非常怀疑 util 项目需要是一个单独的可运行的 Spring Boot 应用程序。所以我建议/期望那里没有spring-boot-maven-plugin。如果你真的想让它产生 2 个资源,一个由 Spring Boot 打包,另一个是一个普通的 jar。请参阅docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/… 了解如何做到这一点。
  • @M.Deinum 您的评论对我的项目有帮助,我在导入的项目中有分类器执行,现在我可以访问类:)

标签: maven spring-boot


【解决方案1】:

您必须使用重新包装来满足此要求。

默认情况下,重新打包目标会将原始工件替换为 重新包装的。对于代表的模块来说,这是一种理智的行为 一个应用程序,但如果您的模块被用作另一个模块的依赖项, 您需要为重新包装的分类器提供分类器。

原因是应用程序类被打包在 BOOT-INF/classes 使依赖模块无法加载重新打包的 jar 的类。如果是这种情况,或者您更愿意保留 原始工件并将重新包装的工件附加到不同的工件上 分类器,配置插件如下:

<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>

或者你也可以声明主类。

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>YourMainClass.java</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 2020-10-18
    • 2017-10-02
    • 1970-01-01
    • 2017-11-21
    • 2018-02-23
    • 2018-10-17
    • 2014-07-17
    • 2015-01-14
    相关资源
    最近更新 更多