简体   繁体   English

如何从目标/生成源创建包含类的Spring Boot胖JAR

[英]How to create a Spring Boot fat JAR containing classes from target/generated-sources

I want to create a Spring Boot fat JAR containing classes from target/generated-sources. 我想创建一个包含来自目标/生成源的类的Spring Boot胖JAR。 After that I want to add this JAR as a dependency in another app and use a class from target/generated-sources in another app. 之后,我想将此JAR添加为另一个应用程序中的依赖项,并在另一个应用程序中使用来自目标/生成源的类。

So I have a Spring Boot app, and after mvn clean install some classes are generating because I'm using Swagger codegen plugin. 所以我有一个Spring Boot应用程序,并且在mvn clean install后,由于使用Swagger codegen插件,正在生成一些类。 After many hours of searching how to create a Spring Boot fat JAR containing all the classes from src/main/java, because you know that a Spring Boot fat JAR is not a standard Java JAR and all the classes are in the BOOT-INF folder of the JAR and you cannot import them in another app until you add some code in the pom.xml. 经过许多小时的搜索,如何创建一个包含src / main / java中所有类的Spring Boot fat JAR,因为您知道Spring Boot fat JAR不是标准的Java JAR,并且所有类都位于BOOT-INF文件夹中JAR,并且您不能将它们导入另一个应用程序,除非您在pom.xml中添加一些代码。 But this fat JAR still doesn't contain the classes from target/generated-sources and I need these classes because I want to import them in another project. 但是这个胖的JAR仍然不包含来自目标/生成源的类,我需要这些类,因为我想将它们导入另一个项目中。

This is the pom.xml for the Spring Boot project that I want to create the fat JAR: 这是我要创建胖JAR的Spring Boot项目的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.7.RELEASE</version>
        <relativePath/> 
    </parent>
    <groupId>com.example.spring</groupId>
    <artifactId>spring-boot-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-swagger</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>

..............

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            ...................


        </plugins>
    </build>

</project

After mvn clean install there are 2 JARs created and they are added in the local repository, one is the JAR with BOOT-INF folder and the other is the JAR containing the classes from src/main/java. 在执行mvn clean install ,将创建2个JAR,并将它们添加到本地存储库中,一个是带有BOOT-INF文件夹的JAR,另一个是包含src / main / java中的类的JAR。 But it doesn't contain the classes from target/generated-sources. 但是它不包含来自target / generated-sources的类。 I want a JAR containing the classes from src/main/java and also the classes from target/generated-sources because I want to add this JAR as a dependency in another app and import these classes. 我想要一个JAR,其中包含来自src / main / java的类以及来自target / generated-sources的类,因为我想将此JAR添加为依赖项,并将其导入另一个应用程序中。

This is the Spring Boot app after mvn clean install : 这是mvn clean install后的Spring Boot应用程序:

springboot springboot

Please help and thank you in advance! 请帮助并提前感谢您!

A Spring Boot Fat Jar is a deployment wrapping of the finished product, not a jar in the traditional sense (the name is slightly misleadning, probably for historic reasons). Spring Boot Fat Jar是成品的展开包装 ,而不是传统意义上的jar(名称可能有点误导,可能出于历史原因)。

You should strongly consider moving the fat jar creation step into your packaging step instead (possibly in a separate Maven module) and do all the modifications needed before that. 您应该强烈考虑将胖子创建步骤转移到包装步骤中(可能在单独的Maven模块中),并在此之前进行所有需要的修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM