19) maven 项目结构:聚集

Project Aggregation [,æɡrɪ'ɡeɪʃən]

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

https://maven.apache.org/guides/mini/guide-multiple-modules.html

模块式开发(将所有模块聚合在一个父pom)

使用一个父pom,管理各个模块,控制模块间依赖关系和构建顺序

开始一个简单的demo来理解这个结构

1.创建一个父pom(Maven Project)

<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>cn.zno</groupId>
  <artifactId>app-pom</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
</project>

2.创建一个jar模块(Maven Module)

右键 app-pom

new Maven Module

<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>
  <parent>
    <groupId>cn.zno</groupId>
    <artifactId>app-pom</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>app-jar</artifactId>
</project>

3.创建一个war模块

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.zno</groupId>
    <artifactId>app-pom</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>app-war</artifactId>
  <packaging>war</packaging>
  <name>app-war Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>app-war</finalName>
  </build>
</project>

现在父pom自动修改为:

<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>cn.zno</groupId>
  <artifactId>app-pom</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
      <module>app-jar</module>
      <module>app-war</module>
  </modules>
</project>

现在install app-pom

右键app-pom ,Run As ,Maven install

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] app-pom
[INFO] app-jar
[INFO] app-war Maven Webapp
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building app-pom 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ app-pom ---
[INFO] Installing F:eworkspacetestapp-pompom.xml to C:Userswl.m2
epositorycnznoapp-pom1.0app-pom-1.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building app-jar 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app-jar ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app-jar ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app-jar ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ app-jar ---
[INFO] Building jar: F:eworkspacetestapp-pomapp-jar	argetapp-jar-1.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ app-jar ---
[INFO] Installing F:eworkspacetestapp-pomapp-jar	argetapp-jar-1.0.jar to C:Userswl.m2
epositorycnznoapp-jar1.0app-jar-1.0.jar
[INFO] Installing F:eworkspacetestapp-pomapp-jarpom.xml to C:Userswl.m2
epositorycnznoapp-jar1.0app-jar-1.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building app-war Maven Webapp 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app-war ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app-war ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app-war ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:eworkspacetestapp-pomapp-warsrc	est
esources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app-war ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app-war ---
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ app-war ---
[INFO] Packaging webapp
[INFO] Assembling webapp [app-war] in [F:eworkspacetestapp-pomapp-war	argetapp-war]
[INFO] Processing war project
[INFO] Copying webapp resources [F:eworkspacetestapp-pomapp-warsrcmainwebapp]
[INFO] Webapp assembled in [36 msecs]
[INFO] Building war: F:eworkspacetestapp-pomapp-war	argetapp-war.war
[INFO] WEB-INFweb.xml already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ app-war ---
[INFO] Installing F:eworkspacetestapp-pomapp-war	argetapp-war.war to C:Userswl.m2
epositorycnznoapp-war1.0app-war-1.0.war
[INFO] Installing F:eworkspacetestapp-pomapp-warpom.xml to C:Userswl.m2
epositorycnznoapp-war1.0app-war-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] app-pom ............................................ SUCCESS [  0.674 s]
[INFO] app-jar ............................................ SUCCESS [  2.903 s]
[INFO] app-war Maven Webapp ............................... SUCCESS [  0.759 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.483 s
[INFO] Finished at: 2016-09-07T15:11:51+08:00
[INFO] Final Memory: 13M/111M
[INFO] ------------------------------------------------------------------------

做了这几件事:

install 了一个pom文件

install 了一个 jar 文件

install 了一个 war 文件

构建顺序引用官网的话:

The following relationships are honoured when sorting projects:

  • a project dependency on another module in the build
  • a plugin declaration where the plugin is another modules in the build
  • a plugin dependency on another module in the build
  • a build extension declaration on another module in the build
  • the order declared in the <modules> element (if no other rule applies)

因为app-war 和 app-jar 没有依赖关系,所以构建顺序是用的最后一个规则(modules 声明顺序)

为何模块化开发

  • 分工明确代码复用
  • SOA模块便于分布式
  • 保护源码权限控制
  • 局部构建节省时间

1.开发过程中可能遇到多个项目,里面的dao ,servie, util 重复使用,形成代码到处复制粘贴。等到想修改某处逻辑时傻眼了,因为要改N处。无谓的加大了开发成本和维护成本。

2.并发访问量提升之后考虑的SOA,如果最初设计时没有服务模块化,改动成本巨大。

3.版本控制软件svn 、git,如果代码全在一处,虽然只分配一个小任务只涉及到小部分代码,但全部的代码都要被下载下来。

4. 只修改了一个小功能没想到需要整体构建一下子,慢慢慢。

原文地址:https://www.cnblogs.com/zno2/p/5850089.html