maven多模块项目构建

时间:2023-03-09 04:28:12
maven多模块项目构建

描述
  一个大的企业级项目通常跨越了数十万行代码,牵涉了数十或数百软件人员的努力。如果开发者在同一个项目下开   发,那么项目的管理、构建将会变得很难控制。因此设计人员会将项目划分为多个模块,多个模块独立开发、构建,    最终通过依赖整合到一起。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>

pompom.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 多模块项目
maven多模块项目构建

1、类似打开Project
maven多模块项目构建

2、新建聚合工程、填写相应的信息,下一步即可

maven多模块项目构建

maven多模块项目构建

3、新建子模块 ,Add as module to 和 parent 必须要选择,下一步即可

maven多模块项目构建

maven多模块项目构建

3、新建web子模块 ,Add as module to 和 parent 必须要选择,下一步即可

maven多模块项目构建

maven多模块项目构建

maven的配置

maven多模块项目构建

4、maven打包
maven多模块项目构建

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启动

maven多模块项目构建

maven多模块项目构建

maven多模块项目构建

maven多模块项目构建

maven多模块项目构建

maven多模块项目构建

maven多模块项目构建

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