簡體   English   中英

為什么Maven + Spring Boot創建巨大的jar文件?

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

我具有以下Maven項目結構:

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

以及以下簡化的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>

模塊_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>

實際上,我有更多模塊,其中更多模塊是其他模塊的依賴項。 當我運行mvn install ,主應用程序得到一個1.2GB的文件。 我注意到所有模塊的所有依賴項都組裝到了模塊中。 因此,許多jar文件會多次組裝到文件中。 如何避免這種情況?

您已經在父pom中聲明了spring-boot-maven-plugin 因此,每個創建的工件都將是一個可執行jar文件,並且所有這些可執行jar文件都包含jar所需的依賴項。

domain_models_and_repository包含在父級中聲明的所有依賴項及其自身的依賴項。

模塊1模塊2包含所有父依賴項,本地聲明的依賴項以及domain_models_and_repository項目表示的所有依賴項以及模塊本身。

模塊3包含所有的父依賴項,它自己在本地聲明的依賴項以及domain_models_and_repository模塊1模塊2以及所有這些模塊本身的所有其他尚不可用的依賴

主應用程序包含父依賴項,它自己在本地聲明的依賴項以及domain_models_and_repository模塊1模塊2以及這些模塊本身中所有其他尚未使用的依賴

要解決此問題,請從父級中刪除spring-boot-maven-plugin ,僅將其添加到主應用程序的pom中。 這樣,只有您的主應用程序是可執行jar,而所有其他模塊只是普通的jar文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM