【问题标题】:jar-with-dependencies and a managed dependencyjar-with-dependencies 和托管依赖项
【发布时间】:2015-07-29 21:00:03
【问题描述】:

我们有一个包含十几个子项目的父项目。大多数子项目都有测试,因此它们依赖于 JUnit。我认为将其作为托管依赖项拉到父 pom 中是有意义的:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在我的一个子项目中 - 仍在清理这个项目 - 我在构建期间需要 JUnit。所以在那个 pom 中我现在有:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>compile</scope>
    </dependency>

这很好用。 事情崩溃的部分是,该项目还需要构建为具有依赖关系的 jar,使用:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>TestIntegration</finalName>
            </configuration>
        </plugin>

jar-with-dependencies 缺少 JUnit。请注意,如果我将 JUnit 作为托管依赖项删除,并将其指定为该项目中的正常依赖项,那么一切都会正常构建。

如何将 jar 作为测试范围的托管依赖项放入 jar-with-dependencies?

【问题讨论】:

    标签: maven junit


    【解决方案1】:

    托管依赖用于锁定父 pom 中的版本。这并不意味着junit会自动成为子项目的依赖。

    您需要将其指定为子 pom 或父 pom 中的依赖项,以便将其包含在 uber jar 中

    更新:我误解了这个问题,并认为 OP 是在询问仅使用在依赖管理中声明的依赖。

    如何将 jar 作为测试范围的托管依赖项放入 jar-with-dependencies?

    依赖管理优先于依赖部分中声明的依赖 - 因此使用范围编译重新定义依赖中的依赖将不起作用。要解决此问题,请在子 pom 的依赖项管理部分中重新定义依赖项。

    所以你的父 pom 有:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    将此添加到您的孩子 pom:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    【讨论】:

    • 对。所以根据我的第二段:“在我的一个子项目中......”我已经有了。
    • 子pom也需要指定版本。
    • @SiKing 它在没有版本的情况下对我有用 - 它需要来自父母的 4.11
    • 我正在运行 Maven 3.0.5,我得到:[错误]内部错误:org.apache.maven.artifact.InvalidArtifactRTException:对于工件 {junit:junit:null:jar}:版本不能为空。
    • 只有当我也指定了一个版本(使用 maven 3.3.3)时,范围覆盖才对我有用
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 2017-09-02
    • 2011-09-06
    • 2012-01-31
    • 1970-01-01
    • 2020-08-02
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多