美文网首页
maven 父子项目说明

maven 父子项目说明

作者: 后知不觉1 | 来源:发表于2024-07-17 12:47 被阅读0次

1、maven 父目录的作用

  • 父模块的groupId和version会传递到子模块,子模块不需要再声明,仅需要指定自己独有的artifactId即可。也可以指定子模块的groupId和version;
  • 父模块的依赖配置会传递到子模块,子模块不需要再单独引入依赖;
  • 父模块可以管理依赖的版本,子模块可以自由灵活的选择需要的依赖,不需要再关心版本的问题

2、maven父子项目关键标签

<modules> 父目录独有
<parent> 子目录独有
<dependencyManagement> 父目录独有
<dependencies> 都可以有

2.1、module标签的作用

在构建这个项目的时候,不需要深入每个module去单独构建,而只是在项目A下的pom.xml构建,就会完成对两个module的构建

  <!--  父pom必须声明子模块,无法识别子模块,在  -->
  <modules>
    <module>child1</module>
    <module>child2</module>
  </modules>
2.2、parent标签的作用

parent标签就是继承,代表在有没有配置时找父目录。
能够通过parent标签能够继承的有

  • groupId
  • version
  • description
  • url
  • inceptionYear
  • organization
  • licenses
  • developers
  • contributors
  • mailingLists
  • scm
  • issueManagement
  • ciManagement
  • properties
  • dependencyManagement
  • dependencies
  • repositories
  • pluginRepositories
  • build

例如:子项目添加parent标签,未写依赖,在maven中却有多个依赖引入,这些都来自于父目录

<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.tianzehao</groupId>
        <artifactId>mavenTest</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>child2</artifactId>
    <packaging>jar</packaging>

    <name>child2</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    </dependencies>
</project>
image.png
2.3、dependencyManagement作用

在父项目中只声明,默认不会被子项目加载,只有子项目pom中引入dependencyManagement管理的模块才会被继承到并且version和scope都读取自父pom;
另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本

附录:

项目结构为

parent
|--- child1
|--- child2

parent的pom.xml

  <groupId>com.tianzehao</groupId>
  <artifactId>mavenTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>mavenTest</name>
  <url>http://maven.apache.org</url>
  <!--  父pom必须声明子模块,无法识别子模块,在  -->
  <modules>
    <module>child1</module>
    <module>child2</module>
  </modules>

child的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.tianzehao</groupId>
        <artifactId>mavenTest</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>child2</artifactId>
    <packaging>jar</packaging>

    <name>child2</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    </dependencies>
</project>

相关文章

网友评论

      本文标题:maven 父子项目说明

      本文链接:https://www.haomeiwen.com/subject/wzxkhjtx.html