Maven 多模块构建
在之前的章节中,我们已经学习了关于 Maven 的大部分知识。从本节开始,我们会通过开发小型项目来更好的理解之前的概念。一节中,我们首先的就是用 Maven 来构建包含多个模块的项目。
提到多模块项目构建,我们首先要弄明白 project 和 module 这两个概念。
a. 相同点:
b. 不同点:
简单总结一下,project 工程包含的范围更广,单模块项目中作为项目整体,多模块项目中作为父模块存在。module 工程在多模块项目中作为子工程存在。
这里,我们结合市面上比较常用的开发工具 idea 来进行演示,当然,其他的开发工具也能够得到相同的。
刚刚,我们也了解到多模块项目必须要有 project 工程作为父项目存在,那么首先,我们就需要创建 project 工程。
在创建好父工程之后,我们开始创建 module 工程。在父工程上点击右键,选择New -> Module
。
这里我们分别创建 mall-account 工程,mall-commodity 工程,mall-order 工程,mall-delivery 工程,分别对应商城系统的模块,商品模块,订单模块以及物流模块。
另外我们在单独创建 mall-web 工程,主要的是作为各个模块的整合并与前端进行对接。
在创建 mall-web 工程的时候,需要注意的是,我们要选择-webapp
的模块。然后点击 Next 按钮,进入下一步。
现在,整个项目已经初具雏形了,我们来看一下现在项目的结构。
注意:mall 项目为实例项目,中间可能会存在某些不太合理的地方,我们这里更多的关注项目构建过程。
我们首先在父工程 mall-aggregate 的 pom.xml 中, dependencyManagement 节点元素。
<properties>
<junit@H__172@.version></junit@H__172@.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com@H__172@.mic@H__172@.tech</groupId>
<artifactId>mall-account</artifactId>
<version>$@H__172@{project@H__172@.version@H__172@}</version>
</dependency>
<dependency>
<groupId>com@H__172@.mic@H__172@.tech</groupId>
<artifactId>mall-commodity</artifactId>
<version>$@H__172@{project@H__172@.version@H__172@}</version>
</dependency>
<dependency>
<groupId>com@H__172@.mic@H__172@.tech</groupId>
<artifactId>mall-order</artifactId>
<version>$@H__172@{project@H__172@.version@H__172@}</version>
</dependency>
<dependency>
<groupId>com@H__172@.mic@H__172@.tech</groupId>
<artifactId>mall-delivery</artifactId>
<version>$@H__172@{project@H__172@.version@H__172@}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>$@H__172@{junit@H__172@.version@H__172@}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后,依次在各个子工程中,根据子项目之依赖关系,对应的依赖。在完依赖之后,我们可以使用之前安装过的 Maven Helper 来整个工程的依赖关系图。
在项目创建好之后,我们就来运行这个项目。在 idea 中点击Add Conuration
按钮。
有了之前章节的铺垫,项目构建就变得非常容易了。
我们将目录切换到 mall-aggregate 目录下,执行 mvn clean install
命令,就可以得到如下执行结果。
@H__172@[INFO@H__172@] Scanning for projects@H__172@.@H__172@.@H__172@.
@H__172@[WARNING@H__172@]
@H__172@[WARNING@H__172@] Some problems were encountered while building the effective model for com@H__172@.mic@H__172@.tech:mall-web:war:@H__172@.-SNAPSHOT
@H__172@[WARNING@H__172@] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: junit:junit:jar -> duplicate declaration of version @H__172@(?@H__172@) @ line @H__172@, column
@H__172@[WARNING@H__172@]
@H__172@[WARNING@H__172@] It is highly recommended to fix these problems because they threaten the stability of your build@H__172@.
@H__172@[WARNING@H__172@]
@H__172@[WARNING@H__172@] For this reason@H__172@, future Maven versions might no longer support building such malformed projects@H__172@.
@H__172@[WARNING@H__172@]
@H__172@[INFO@H__172@] ------------------------------------------------------------------------
@H__172@[INFO@H__172@] Reactor Build Order:
@H__172@[INFO@H__172@]
@H__172@[INFO@H__172@] mall-aggregate @H__172@[pom@H__172@]
@H__172@[INFO@H__172@] mall-account @H__172@[jar@H__172@]
@H__172@[INFO@H__172@] mall-commodity @H__172@[jar@H__172@]
@H__172@[INFO@H__172@] mall-order @H__172@[jar@H__172@]
@H__172@[INFO@H__172@] mall-delivery @H__172@[jar@H__172@]
@H__172@[INFO@H__172@] mall-web Maven Webapp @H__172@[war@H__172@]
@H__172@[INFO@H__172@]
@H__172@[INFO@H__172@] --------------------< com@H__172@.mic@H__172@.tech:mall-aggregate >---------------------
@H__172@[INFO@H__172@] Building mall-aggregate @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ pom @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] ---------------------< com@H__172@.mic@H__172@.tech:mall-account >----------------------
@H__172@[INFO@H__172@] Building mall-account @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ jar @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] --------------------< com@H__172@.mic@H__172@.tech:mall-commodity >---------------------
@H__172@[INFO@H__172@] Building mall-commodity @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ jar @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] ----------------------< com@H__172@.mic@H__172@.tech:mall-order >-----------------------
@H__172@[INFO@H__172@] Building mall-order @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ jar @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] ---------------------< com@H__172@.mic@H__172@.tech:mall-delivery >---------------------
@H__172@[INFO@H__172@] Building mall-delivery @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ jar @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] -----------------------< com@H__172@.mic@H__172@.tech:mall-web >------------------------
@H__172@[INFO@H__172@] Building mall-web Maven Webapp @H__172@.-SNAPSHOT @H__172@[/@H__172@]
@H__172@[INFO@H__172@] --------------------------------@H__172@[ war @H__172@]---------------------------------
@H__172@[INFO@H__172@] @H__172@.@H__172@.@H__172@.
@H__172@[INFO@H__172@] Building war: D:\code\mall-aggregate\mall-web\target\mall-web@H__172@.war
@H__172@[INFO@H__172@] ------------------------------------------------------------------------
@H__172@[INFO@H__172@] Reactor Summary for mall-aggregate @H__172@.-SNAPSHOT:
@H__172@[INFO@H__172@]
@H__172@[INFO@H__172@] mall-aggregate @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] mall-account @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] mall-commodity @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] mall-order @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] mall-delivery @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] mall-web Maven Webapp @H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@.@H__172@. SUCCESS @H__172@[ s@H__172@]
@H__172@[INFO@H__172@] ------------------------------------------------------------------------
@H__172@[INFO@H__172@] BUILD SUCCESS
@H__172@[INFO@H__172@] ------------------------------------------------------------------------
@H__172@[INFO@H__172@] Total time: s
@H__172@[INFO@H__172@] Finished at: --09T22::+:
@H__172@[INFO@H__172@] ------------------------------------------------------------------------
在本章节中,我们完成了 mall 工程的搭建,运行,构建的整个过程,本节中并没有太多新的,都是基于之前章节的,进行整合,后续的章节也会使用这个示例项目来进行讲解。