【问题标题】:Maven modules are trying to use parent dependenciesMaven 模块正在尝试使用父依赖项
【发布时间】:2013-10-12 13:18:08
【问题描述】:

我有一个 maven 项目,其中父模块有一个 lib 目录,其中包含编译所需的各种 jar,但不包含在最终产品中。当我尝试让子模块构建时,它失败了。它说“无法解析以下工件”,然后最终说“在 C:\path\to\project\modules\module_name\lib\local_dependency.jar 找不到工件 local_dependency”。

子模块不依赖于父模块使用的库,但它仍然希望包含它们。我需要设置一个选项来防止这种情况发生吗?

父 Pom sn-p:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <currentVersion>1.0.0</currentVersion>
</properties>

<groupId>com.project</groupId>
<artifactId>project_artifact</artifactId>
<packaging>pom</packaging>
<version>${currentVersion}</version>

<modules>
    <module>modules/module_name</module>
</modules>

<dependencies>
    <dependency>
        <groupId>group.id</groupId>
        <artifactId>local_dependency</artifactId>
        <version>1.0</version>
        <systemPath>${basedir}/lib/local_dependency.jar</systemPath>
        <scope>system</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

孩子 pom sn-p:

<parent>
    <groupId>com.project</groupId>
    <artifactId>project_artifact</artifactId>
    <version>${currentVersion}</version>
    <relativePath>../../</relativePath>
</parent>

<dependencies>
    <dependency>
        <groupId>net.some.dependency</groupId>
        <artifactId>artifact_name</artifactId>
        <version>1.0.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.project</groupId> <!-- The child depends on the parent for the parent's API-->
        <artifactId>project_artifact</artifactId>
        <version>${currentVersion}</version>
        <type>jar</type>
    </depdencency>
</dependencies>

因此,子 pom 将尝试从 project_base/modules/module_name/lib/local_dependency.jar 包含 group.id:local_dependency 但它不存在,也不需要存在。

【问题讨论】:

    标签: maven module scope dependencies system


    【解决方案1】:

    无论您是否明确包含依赖项,子项都会继承父项的依赖项。解决此问题的两种可能方法是:

    • 不要在父级中构建任何 jar 工件 - 为此创建一个子模块并将子模块用作其兄弟姐妹中的依赖项。
    • 使用固定路径(与 ${basedir} 无关,因为这会在每个模块构建中发生变化,从而尝试重新解析该位置)。如果你总是从父目录构建,你可以使用 ${user.dir}。

    【讨论】:

    • 我选择了您的第一个选项,我将父级移到了它自己的模块中,现在一切正常。
    【解决方案2】:

    您可以在依赖声明中排除特定的传递依赖。在您的情况下,子 pom 对父级的依赖性的以下更改应该使构建工作:

    <dependency>
        <groupId>com.project</groupId> <!-- The child depends on the parent for the parent's API-->
        <artifactId>project_artifact</artifactId>
        <version>${currentVersion}</version>
        <type>jar</type>
        <exclusions>
            <exclusion>
                <groupId>group.id</groupId>
                <artifactId>local_dependency</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    【讨论】:

    • 现在我仔细观察,我意识到从孩子到父母的依赖并不是问题的原因。由于项目结构,无论是否声明了显式依赖项,子项都将继承父项的依赖项,这就是为什么可选元素和特定传递排除都不起作用的原因。我会添加另一个(更好的)答案...
    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2017-06-06
    • 2020-10-30
    相关资源
    最近更新 更多