05 maven 创建多模块项目

1. 创建一个根模块

Paste_Image.png

打包方式必须为pom,这样代表一个根模块
Paste_Image.png

创建完毕后删除根模块的src目录,因为没有必要

2. 创建子模块
根模块右建->maven model

Paste_Image.png

这里不使用archetype骨架创建项目


Paste_Image.png

子模块要打包成jar包供其他模块使用

Paste_Image.png

同理创建:helloweb-entity helloweb-dao

3. 创建web模块

  • 不使用architype骨架来创建web项目
Paste_Image.png
Paste_Image.png

如果不使用architype骨架的话,那么需要手动添加内容到webapp下

Paste_Image.png
  • 使用architype骨架来创建web项目


    Paste_Image.png
Paste_Image.png

4. pom配置
创建完成后,结构如下

Paste_Image.png

根模块的pom.xml

<?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.xxjqr</groupId>
    <artifactId>helloweb-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 跟子模块的关联 -->
    <modules>
        <module>helloweb-core</module>
        <module>helloweb-entity</module>
        <module>helloweb-dao</module>
        <module>helloweb-web</module>
    </modules>

    <!-- 用dependencyManagement来管理依赖版本 -->
    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>tomcat</groupId>
                <artifactId>servlet</artifactId>
                <version>4.1.36</version>
                <scope>provided</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>
    <!-- Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。通常会在一个组织或者项目的最顶层的父POM
    中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能让
    所有在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement
    元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号。

    这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改
    ;另外如果某个子项目需要另外的一个版本,只需要声明version就可。

    dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。 -->
</project>

entity子模块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>
  
  <!-- 跟根模块的关联 -->
  <parent>
    <groupId>com.xxjqr</groupId>
    <artifactId>helloweb-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <artifactId>helloweb-entity</artifactId>
  <!-- 为什么子模块中不用写<groupId></groupId>和<version></version>?
    因为子模块的pom会继承自根模块的pom,而根模块的pom中已经写了<groupId>和<version>
    且这些在所以的模块中都是一样的,所以就不用在子模块中写了
   -->
</project>

web子模块pom.xml

<?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>com.xxjqr</groupId>
        <artifactId>helloweb-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.xxjqr</groupId>
    <artifactId>helloweb-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>helloweb-web Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- 依赖管理(不再需要指定版本号和域;除非我们想覆盖)-->
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>servlet</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>helloweb-web</finalName>
    </build>
</project>

5. 最后操作
选中全部模块,右键->maven->update project
选中web模块部署到tomcat中运行

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容