标签parent,dependency和plugin(Maven)之间有什么区别

Hel*_*lad 2 maven

我很困惑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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-app</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

为什么我必须指定两次依赖?我该如何使用这三个标签?

eck*_*kes 8

parent是一个"继承"的POM,它基本上通过覆盖与你当前的POM结合起来.(您可以使用"mvn help:effective-pom"打印组合结果).因此,父级中的所有依赖项和插件都将成为您的(以及所有托管依赖项版本,这在spring-boot中占多数).

dependendency你需要在你的classpath编译或测试或运行项目指定的工件(取决于它的范围).

虽然plugin是被在编译的时候(如编译器,报表生成器之类的东西)执行的假象.

我建议阅读一本关于maven的好教程或书,我的简单解释不足以充分利用它.https://maven.apache.org/articles.html