maven多模塊打包詳解

一、項目結構

在開始介紹maven多模塊打包前,先介紹一下項目結構。多模塊項目就是由多個小的項目組成一個大的項目,它們之間存在父子關係,也就是說每個小的項目是在一個父項目之下的。

├── parent         #父模塊
│   ├── pom.xml
│   ├── child1     #子模塊1
│   │   ├── pom.xml
│   ├── child2     #子模塊2
│   │   ├── pom.xml
│   └── child3     #子模塊3
│       ├── pom.xml
└── pom.xml

在這個示例中,parent是一個父模塊,包含了三個子模塊child1、child2和child3。子模塊之間是相互獨立的,它們都有自己的pom.xml文件,定義了自己的依賴、插件等,並且它們都是在parent項目之下的。

二、maven多模塊打包基礎

1、子模塊的pom.xml文件

在每個子模塊的pom.xml文件中,需要定義一些基本信息,例如groupId、artifactId和version。

<groupId>com.example</groupId>
<artifactId>child1</artifactId>
<version>1.0-SNAPSHOT</version>

同時需要引入父模塊的依賴,這個可以通過 <parent> 標籤來實現。

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

2、父模塊的pom.xml文件

在父模塊的pom.xml文件中,也需要定義一些基本信息,例如groupId、artifactId和version。

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

重要的是,需要在 <modules> 標籤下聲明使用哪些子模塊。

<modules>
    <module>child1</module>
    <module>child2</module>
    <module>child3</module>
</modules>

3、多模塊打包命令

在多模塊項目中,就可以使用maven來進行打包了。在父模塊下執行以下命令即可打包所有子模塊。

mvn clean package

maven會在每個子模塊的target目錄下生成一個jar包。

三、多模塊打包進階

1、多模塊項目中的依賴管理

在多模塊項目中,存在多個模塊間的依賴關係,如果沒有合理地管理這些依賴,就可能會導致一些問題,比如版本衝突等問題。

為了解決這些問題,可以使用Maven的依賴管理機制。在父模塊的pom.xml文件中,可以定義一些依賴的版本號等信息,然後在子模塊中引用這些依賴時,直接使用父模塊中定義的版本號即可。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dependency1</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dependency2</artifactId>
            <version>2.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

2、多模塊項目中的插件管理

與依賴管理類似,多模塊項目中也可能有很多相同的插件配置,這時候可以使用Maven的插件管理機制。在父模塊的pom.xml文件中,也可以定義一些插件的配置,然後在子模塊中引用這些插件時,直接使用父模塊中定義的配置即可。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Class-Path>lib/dependency1-1.0.jar lib/dependency2-2.0.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

3、多模塊項目中的聚合打包

在上面的示例中,使用了mvn clean package命令來打包所有的子模塊。實際上,還可以使用聚合打包來一次性打包所有的子模塊。在父模塊中增加一個 <modules> 標籤即可。

<modules>
    <module>child1</module>
    <module>child2</module>
    <module>child3</module>
</modules>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

說明:這裡使用了maven-assembly-plugin插件來進行聚合打包。在pom.xml同級目錄下面增加一個assembly.xml文件如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>all-in-one</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly> 

說明:使用這個插件可以聚合打包所有的子模塊,並將它們的依賴一一打包,生成一個zip文件。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/289116.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-24 03:01
下一篇 2024-12-24 03:01

相關推薦

  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變量讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web服務器。nginx是一個高性能的反向代理web服務器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分布式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性傳感器,能夠同時測量加速度和角速度。它由三個傳感器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25

發表回復

登錄後才能評論