maven parent中的dependencies和dependencyManagement区别

本文详细介绍了如何创建Maven的父级POM文件,包括配置项目基本信息、设置依赖项和依赖管理,以及构建插件的配置。文章还解释了父模块与子模块之间的依赖关系,以及dependencyManagement的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,写一个maven parent

<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.lala</groupId>
    <artifactId>my-parent</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>javax.mail-api</artifactId>
                <version>1.5.3</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


dependencies里面的依赖是:2.4.0的jedis

dependencyManagement里面的依赖是:1.5.3的java.mail

子模块这样写的话

<parent>
        <groupId>com.lala</groupId>
        <artifactId>my-parent</artifactId>
        <version>0.0.1</version>
    </parent>


会有如下反应:

1:子模块会自动引入父模块的dependencies依赖,而不会自动引入dependencyManagement的依赖

2:dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖,而且不需要指定version、scope,系统会自动用父模块的version、scope
 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值