maven 父子项目说明

2024-07-17  本文已影响0人  后知不觉1

1、maven 父目录的作用

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标签能够继承的有

例如:子项目添加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>

上一篇 下一篇

猜你喜欢

热点阅读