spring-boot-maven-plugin 没有将依赖 类 拉入 jar

spring-boot-maven-plugin not pulling in dependent classes into jar

我在使用 spring-boot-maven-plugin 时遇到问题,它没有在生成的 jar 文件中包含相关的 classes。

docs 中,它声明具有范围 provided 的依赖项将包含在 jar 文件中,但我无法让它包含它们。

我有一个包含 2 sub-modules 的项目:modelrestServer。在 model 模块中,我想使用 swagger 来基于 openApi 输入模型进行代码生成。生成的 classes 被放入一个 jar 文件中:model/target/rest-model-0.0.1-SNAPSHOT.jar.

restServer模块中,我有SpringRestController和Applicationjava代码,想"pull in"modelclasses 到生成的 jar 文件中:restServer/target/rest-server-0.0.1-SNAPSHOT.jar 使用 spring-boot-maven-plugin 构建器,但它不包括 model sub-module.

中的任何内容

下面列出了整个项目结构和 pom 文件。

如何让 spring-boot-maven-pluginmodel sub-module 中提取 class 文件,有效地创建 "fat" self-contained 罐子?

项目结构

project-root/
    pom.xml # parent pom
    model/
        pom.xml
        src/main/openApi/model.json
        target/
            generated-sources/* (package: com.me.rest.model.*)
            rest-model-0.0.1-SNAPSHOT.jar

    restServer/
        pom.xml
        src/main/java/com/me/rest/
            controller/Controller.java
            Application.java

Parent pom.xml

<?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.2.RELEASE</version>
  </parent>

  <groupId>com.me</groupId>
  <artifactId>rest-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>rest</name>
  <packaging>pom</packaging>

  <description>Swagger codegen for Spring Rest Server sandbox project</description>

  <scm>
    <connection>scm:git:git@github.com:swagger-api/swagger-codegen.git</connection>
    <developerConnection>scm:git:git@github.com:swagger-api/swagger-codegen.git</developerConnection>
    <url>https://github.com/swagger-api/swagger-codegen</url>
  </scm>

  <prerequisites>
    <maven>2.2.0</maven>
  </prerequisites>

  <modules>
    <module>model</module>
    <module>restServer</module>
  </modules>
</project>

model/pom.xml

<?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>com.kontron</groupId>
        <artifactId>rest-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.kontron</groupId>
    <artifactId>rest-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies> ... </dependencies>

    <build>
        <plugins>
            <!-- Swagger codegen plugin -->
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration> ... </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

restServer/pom.xml

<?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>com.kontron</groupId>
      <artifactId>rest-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>rest-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.kontron</groupId>
            <artifactId>rest-model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>provided</scope>     <!-- Notice scope is provided -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

更新:添加结果 META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Implementation-Title: rest-server
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: bjohnson
Implementation-Vendor-Id: com.me
Spring-Boot-Version: 2.1.2.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.me.rest.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_144
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
 ot-starter-parent/rest-parent/rest-server

依赖项放在重新打包的JAR文件的BOOT-INF\lib\中。

此路径将添加到您的类路径中 Spring 启动应用程序。