jar-with-dependencies和托管依赖项 [英] jar-with-dependencies and a managed dependency

查看:412
本文介绍了jar-with-dependencies和托管依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个父项目,其中有大约十二个子项目.大多数子项目都有测试,因此它们依赖于JUnit.我认为将其作为托管依赖项发布到父pom是有道理的:

We have a parent project with about dozen child projects. Most of the child projects have tests, and so they depend on JUnit. I thought it would make sense to pull this out to the parent pom as a managed dependency:

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

在我的一个子项目中-仍在清理此项目-在构建过程中需要JUnit.所以在那个pom中,我现在有:

In one of my child projects - still working on cleaning up this one - I need JUnit during build. So in that pom I now have:

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

这很好. 事情分崩离析的部分是,该项目还需要使用以下方式构建为具有依赖项的jar:

This works fine. The part where things fall apart, is that this project also needs to be built as a jar with dependencies, using:

        <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作为托管依赖项,并在该项目中将其指定为常规依赖项,那么一切都会很好.

The jar-with-dependencies is missing JUnit. Note that if I remove JUnit as a managed dependency, and specify it as a normal dependency in that project, then everything builds fine.

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

How do I get jar, as a managed dependency scoped for test, into a jar-with-dependencies?

推荐答案

托管依赖项用于将版本锁定在父pom处.这并不意味着junit将自动成为子项目的依赖项.

Managed dependency is used to lock the version at the parent pom. It does not mean that junit will automatically be a dependency of child projects.

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

You need to specify it as a dependency in the child or in parent pom for it to be included in a uber jar

更新:我误解了这个问题,并认为OP正在询问仅使用在依赖项mgmt中声明的依赖项.

Update: I misunderstood the question and thought that the OP was asking about using a dependency declared in dependency mgmt only.

我如何将jar(作为受测试的作用域的托管依赖项)放入 有依赖关系的罐子?

How do I get jar, as a managed dependency scoped for test, into a jar-with-dependencies?

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

Dependency management takes precedence over dependencies declared in dependency section - so redefining the dependency in dependencies with scope compile will not work. To fix this, redefine the dependency in the child pom's dependency management section.

所以您的父pom具有:

So your parent pom has:

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

将此添加到您的孩子pom:

Add this to your child pom:

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

这篇关于jar-with-dependencies和托管依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆