Yuan
Yuan

Reputation: 1

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

So I have a Spring Boot app, and after mvn clean install some classes are generating because I'm using Swagger codegen plugin. 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. 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.

This is the pom.xml for the Spring Boot project that I want to create the fat 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.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. But it doesn't contain the classes from 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.

This is the Spring Boot app after mvn clean install:

springboot

Please help and thank you in advance!

Upvotes: 0

Views: 1590

Answers (1)

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

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.

Upvotes: 1

Related Questions