Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Maven + Spring Boot create huge jar-files?

I have the following Maven project structure:

parent_project
+--main_application
+--domain_models_and_repository
+--module_1
+--module_2
+--module_3

And the following simplified POMS:

parent_project.pom

<project>
    <dependencies>
        [Spring Boot dependencies]
    </dependencies>

    <modules>
        <module>main_application</module>
        <module>domain_models_and_repository</module>
        <module>module_1</module>
        <module>module_2</module>
        <module>module_3</module>
    </modules>

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

main_application

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_1</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_2</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_3</artifactId>
        </dependency>
    </dependencies>
</project>

module_1

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
    </dependencies>
</project>

module_2

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
    </dependencies>
</project>

module_3

<project>
    <parent>
        <artifactId>parent_project</artifactId>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>domain_models_and_repository</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_1</artifactId>
        </dependency>
        <dependency>
            <artifactId>module_2</artifactId>
        </dependency>
    </dependencies>
</project>

In reality I have more modules and more of them are dependencies of others. When I run mvn install I get a 1.2GB file for the main application. I noticed that all dependencies of all modules are assembled into the modules. Thus many jar-files are multiple times assembled into the file. How can I avoid this?

like image 480
stevecross Avatar asked Sep 15 '25 11:09

stevecross


1 Answers

You have declared the spring-boot-maven-plugin in your parent pom. Due to this each created artifact will be an executable jar file and all these executable jar files contain the dependencies needed for the jar.

domain_models_and_repository contains all the dependencies declared in the parent and its own dependencies.

module 1 and module 2 contain all the parent dependencies, the locally declared dependencies and all dependencies expressed by the domain_models_and_repository project as well as the module itself.

module 3 contains all the parent dependencies, its own locally declared dependencies and all other not yet available dependencies from domain_models_and_repository, module 1 and module 2 as well the those modules itself.

main application contains the parent dependencies its own locally declared dependencies and all other not yet available dependencies from domain_models_and_repository, module 1 and module 2 as well the those modules itself.

To fix remove the spring-boot-maven-plugin from the parent and only add it to the pom of your main application. That way only your main application is an executable jar and all other modules are just plain jar files.

like image 156
M. Deinum Avatar answered Sep 18 '25 10:09

M. Deinum