maven模块化管理

将一个大项目拆分成若干个子模块,方便项目管理维护、扩展,也方便模块间的相互引用,资源共享

具体步骤

先创建一个空项目(父项目)即下图的sky-take-out,然后打开项目结构的模块,选中父模块,再点+,

如果是导入模块,则导入对应模块的pom.xml文件

我们来看一下父工程的pom文件

  • parent表示父工程
  • 第11-14行本项目的坐标
  • modules表示本项目的子模块
  • properties管理依赖的版本
xml 复制代码
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>${mybatis.spring}</version>
</dependency>

子模块可以引用同一父工程的其他子模块,这些被引用的子模块一般是可复用的公共模块,比如这里的pojo封装实体类对象,common封装各种工具类对象

dependencies和dependencyManagement

父工程使用 dependencies直接声明依赖,子工程会自动继承父工程的依赖,无需重复声明。子工程的 pom.xml 不需要再写这个依赖。

xml 复制代码
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
</dependencies>

父工程使用 dependencyManagement管理依赖,子工程不会自动继承这些依赖!必须显式声明需要的依赖,但可以省略version和 scope

父工程

xml 复制代码
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

子工程·

xml 复制代码
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
</dependencies>
相关推荐
一只学java的小汉堡8 分钟前
微服务与面向服务编程(SOA)入门指南:从架构演进到 Spring Cloud 实践(初学者友好版)
spring boot·spring cloud·java-ee·maven
程序员烧烤20 分钟前
【Java初学基础10】一文讲清反射
java·开发语言
长安——归故李22 分钟前
【PLC程序学习】
java·c语言·javascript·c++·python·学习·php
大卫小东(Sheldon)25 分钟前
如何用Java25编译Java17的项目
java
笨手笨脚の1 小时前
设计模式-建造者模式
java·设计模式·建造者模式·创建型设计模式
SimonKing1 小时前
SpringBoot多模板引擎整合难题?一篇搞定JSP、Freemarker与Thymeleaf!
java·后端·程序员
Craaaayon2 小时前
【数据结构】二叉树-图解深度优先搜索(递归法、迭代法)
java·数据结构·后端·算法·leetcode·深度优先
梦之翼6187202 小时前
eclipse复制项目后原项目名依然伴随值所复制的项目名
java·eclipse