【问题标题】:Importing pom in dependency management在依赖管理中导入pom
【发布时间】:2019-09-17 01:51:18
【问题描述】:

读自doc

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

我有以下疑问:

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 代码。因此,我将分享他们的 poms。我们将看到构建成功,但根据第 1 点,情况并非如此。

项目结构如下:

超级模块的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>

【问题讨论】:

  • 第一点意味着你永远不应该在已经定义为子模块的项目中执行&lt;scope&gt;import&lt;/scope&gt;,这意味着列出了一个&lt;module&gt;..&lt;/module&gt;条目。导致导入范围的解析将在其余部分之前完成,这将导致构建失败。

标签: java maven maven-3 dependency-management


【解决方案1】:

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

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

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

khmarbaise 在 cmets 中添加:

你不应该在已经定义为子模块的项目中使用&lt;scope&gt;import&lt;/scope&gt;,这意味着列出了一个&lt;module&gt;..&lt;/module&gt;条目。因为 import scope 的解析会先于其余部分完成,这将导致构建失败。

还有:

a) 第 3 点是什么意思?

这意味着&lt;dependencies&gt; 部分应该声明一些&lt;dependency&gt;,只有&lt;group&gt;&lt;artifact&gt; &lt;version&gt;
版本将从导入的&lt;dependencyManagement&gt;中获取。
另请参阅“Tracking managed dependency versions in Maven”。
illustrated here,托管依赖用于锁定父pom的版本。

【讨论】:

  • 当然它没有安装,也不应该在多模块构建的构建过程中。
  • @khmarbaise 谢谢。我已将您的评论包含在答案中以提高知名度。
  • @VonC ,我已经编辑了问题以使其更清楚。请看一看。
  • @BreakingBenjamin 我怀疑如果您将子模块 GAV 导入其父项目,它将被忽略,因为子模块 GAV 在子模块 pom.xml 的开头指定其属性 (GAV)。但是如果你要在 另一个 项目 pom dependenciesManagement 中导入/声明它,那么这将失败。
  • @BreakingBenjamin 我想在这个小例子中,有效的 pom 不需要实际导入该依赖项,因为没有显式的依赖项(不是 dependenciesManagement 而是依赖项)。这都是关于有效 pom 的:如果为了计算有效 pom,P1 需要 P2 pom,而 P2 pom 又需要 P1 pom……那将失败。
猜你喜欢
  • 2012-11-04
  • 1970-01-01
  • 2012-12-27
  • 2019-10-16
  • 2023-02-14
  • 2017-11-28
  • 1970-01-01
  • 2016-03-05
  • 2017-01-09
相关资源
最近更新 更多