在依赖项管理中导入pom

时间:2019-04-28 01:59:33

标签: java maven maven-3 dependency-management

doc中读取

  
      
  1. 请勿尝试导入在当前pom子模块中定义的pom。尝试这样做将导致构建   失败,因为它无法找到pom。
  2.   
  3. 永远不要将导入pom的pom声明为目标pom的父母(或祖父母等)。无法解决   循环性和异常将被抛出。
  4.   
  5. 当引用其pom具有传递依赖项的工件时,项目将需要指定这些工件的版本为   托管依赖项。不这样做将导致构建失败   因为工件可能没有指定版本。 (这应该是   在任何情况下都被视为最佳做法,因为它保留了   工件从一个版本转换到另一个版本。
  6.   

我有以下疑问:

a)第3点是什么意思?

b)在第一点上,为什么maven无法找到子模块pom?子模块不是在父模块之前构建的吗?


在第3点中,我需要更清楚地说明...When referring to artifacts whose poms have transitive dependencies the project will need to specify versions of those artifacts as managed dependencies...

因此,假设我们有project A,我们将在project B <dependencyManagement>部分中将其导入。现在,创建项目A的人必须在其<dependencyManagement>部分中提及项目A的所有传递依赖项(不是直接的)的版本吗?谁能知道项目A的所有传递依赖项的那些版本?


我对第1点有另一个疑问。我基本上创建了一个骨骼项目,其中包含一个超级模块和一个子模块,而基本上没有Java代码。因此,我将分享他们的诗歌。我们将看到构建成功,而根据第1点,情况并非如此。

项目结构如下:

enter image description here

超级模块的pom如下:

<?xml version="1.0" encoding="UTF-8"?>

<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.home</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>my-project</name>
    <url>http://www.example.com</url>

    <modules>
        <module>./sub-module1/pom.xml</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.home</groupId>
                <artifactId>sub-module1</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>


    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>
</project>

子模块的pom如下:

<?xml version="1.0" encoding="UTF-8"?>

<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.home</groupId>
    <artifactId>sub-module1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

当我执行mvn clean compile时,它可以正常运行。

此外,我看到即使子模块pom包装类型保留为jar,作用域导入也不会引发错误。仍然可以编译。



对于将来的读者,我将总结答案。点1和2都成立,但是只有当POM1(父模块或超级模块)导入POM2(子模块或子模块),然后POM2需要POM1来解决依赖关系时,它才会给出错误。

在点1中,找不到。在第2点中,由于继承而将找到它,但会创建一个循环。

下面我举个例子,以便人们可以验证。

超级模块POM

<?xml version="1.0" encoding="UTF-8"?>

<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.home</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>my-project</name>
    <url>http://www.example.com</url>

    <modules>
        <module>./sub-module1/pom.xml</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.home</groupId>
                <artifactId>sub-module1</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.8.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
        </dependency>
    </dependencies>


    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>


    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>
</project>

子模块POM

<?xml version="1.0" encoding="UTF-8"?>

<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.home</groupId>
    <artifactId>sub-module1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
        </dependency>
    </dependencies>

</project>

1 个答案:

答案 0 :(得分:1)

  

b)在第一点上,为什么maven无法找到子模块pom?子模块不是在父模块之前构建的吗?

未在GAV引用(您的Maven缓存或远程Nexus存储库)中安装/部署子模块

这意味着任何引用子模块pom的项目都不会在子模块GAV中找到该pom,因为构建,安装和部署的是主要项目GAV。

khmarbaise中添加注释:

  

永远不要在已经定义为子模块的项目中执行<scope>import</scope>,这意味着列出了一个<module>..</module>条目。因为导入scope的解析将在其余解析之前完成,这将导致构建失败。

并且:

  

a)第3点是什么意思?

这意味着<dependencies>部分应声明仅包含<dependency><group>的一些<artifact>不是 <version>
该版本将从导入的<dependencyManagement>中获取。
另请参见“ Tracking managed dependency versions in Maven”。
illustrated here一样,托管依赖项用于将版本锁定在父pom。