一、項目結構
在開始介紹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