描述
一個大的企業級項目通常跨越了數十萬行代碼,牽涉了數十或數百軟件人員的努力。如果開發者在同一個項目下開 發,那么項目的管理、構建將會變得很難控制。因此設計人員會將項目划分為多個模塊,多個模塊獨立開發、構建, 最終通過依賴整合到一起。Maven的聚合特性能夠把項目的各個模塊集合在一起構建,而Maven的繼承特性則能幫助抽取各模塊相同的依賴和 插件配置,在簡化POM的同時,還能促進各個模塊配置的一致性。
聚合
通過一個POM模塊來統一構建多個模塊,Maven可以根據依賴關系計算出構建順序,然后逐一構建。如果沒有 聚合,則需要每個模塊手動構建
聚合pom:pom.xml:
。。。。。。 <packaging>pom</packaging> <modules> <module>../complex_base</module> <module>../complex_user</module> <module>../complex_menu</module> 。。。。。。 </modules> 。。。。。。
配置解析:
聚合模塊只需要pom.xml文件,其他目錄可以刪除,在該文件中聲明packaging為pom
module元素用來配置聚合的模塊,被聚合的模塊可以和聚合模塊是父子目錄,也可以是平行目錄
在聚合模塊執行對應的構建生命周期階段,將會按序執行各個模塊的對應階段
繼承
繼承是指POM的繼承。子模塊繼承父模塊,繼承的是POM,也可以理解為配置的繼承。通過繼承能抽取各模塊相同的依賴和插件配置到父模塊,在簡化POM的同時,還能促進各個模塊配置的一致性。
父pom:pom.xml:
<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> <groupId>com.demo.maven.complex</groupId> <artifactId>complex_integrated</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>complex_integrated</name> <url>http://maven.apache.org</url> <!-- 聚合:mvn package,maven會計算依賴關系,逐模塊構建 --> <modules> <module>../complex_base</module> <module>../complex_user</module> <module>../complex_menu</module> <module>../complex_sample</module> <module>../complex_web</module> </modules> <!--jetty --> <build> <pluginManagement> <plugins> <!-- mvn jetty:run -Djetty.port=9999 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.22.v20170606</version> </plugin> </plugins> </pluginManagement> </build> <!-- 依賴管理 :dependencyManagement下的依賴,不會引入當前項目,也不會引入子項目,只有在子項目中通過dependencies下的子元素進行了聲明才會引入 --> <!-- 依賴管理 :dependencyManagement可以在提供繼承的便利性和統一性的基礎上,提供一定的靈活性,pluginManagement的原理是一致的 --> <dependencyManagement> <dependencies> <!-- javax.servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> </project>
子pom:pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.maven.complex</groupId> <artifactId>complex_integrated</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 默認../pom.xml --> <relativePath>../complex_integrated/pom.xml</relativePath> </parent> <artifactId>complex_web</artifactId> <packaging>war</packaging> <name>complex_web</name> <build> <plugins> <!-- mvn -Djetty.port=9999 jetty:run --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.22.v20170606</version> </plugin> </plugins> <finalName>complex_web</finalName> </build> <dependencies> <dependency> <groupId>com.demo.maven.complex</groupId> <artifactId>complex_user</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
配置解析:
dependencyManagement下的依賴,不會引入當前模塊,也不會引入子模塊,只有在子模塊中通過
dependencies下的子元素進行了聲明才會引入。pluginManagement的原理是一致的 父模塊的packagin必須是pom,目錄下可以只有pom.xml,src等目錄不需要
子模塊可以繼承父模塊大多數的配置,如:groupId等,但並不是可以繼承全部
子模塊中通過parent元素聲明繼承的父模塊,子元素relativePath聲明pom.xml文件的位置,如果父子模塊是父子目錄,則不需要relativePath聲明
子模塊中對於繼承自dependencyManagement的依賴,只需要聲明依賴groupId、 artifactId
IDEA開發工具構建Maven 多模塊項目
1、類似打開Project
2、新建聚合工程、填寫相應的信息,下一步即可


3、新建子模塊 ,Add as module to 和 parent 必須要選擇,下一步即可


3、新建web子模塊 ,Add as module to 和 parent 必須要選擇,下一步即可


maven的配置

4、maven打包

5、POM配置文件如下
complex_integrated配置文件
<?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>
<groupId>com.yygx</groupId>
<artifactId>complex_integrated</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../complex_base</module>
<module>../complex_web</module>
</modules>
</project>
complex_base配置文件
<?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">
<parent>
<artifactId>complex_integrated</artifactId>
<groupId>com.yygx</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../complex_integrated/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>complex_base</artifactId>
</project>
complex_web配置文件
<?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">
<parent>
<artifactId>complex_integrated</artifactId>
<groupId>com.yygx</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../complex_integrated/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>complex_web</artifactId>
<packaging>war</packaging>
<name>complex_web Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yygx</groupId>
<artifactId>complex_base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>complex_web</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置tomcat啟動






OK!!!!!!!!!!!
