Maven 测试依赖的依赖 [英] Maven test dependencies of dependency

查看:83
本文介绍了Maven 测试依赖的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父项目,它具有范围 test

I have a parent project which has dependencies with scope test

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.31.0</version>
        <scope>test</scope>
    </dependency>

现在,当我将此父项目作为依赖项包含在我的子项目中时,我mvn install"了父项目

Now I "mvn install"ed the parent project, when I include this parent project as dependency in my child project

    <dependency>
        <groupId>group</groupId>
        <artifactId>parentproject</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

selenium-firefox-driver 不可用.compile 范围内的依赖项可用.

selenium-firefox-driver is not available. The compile scoped dependencies are available.

我怎样才能使它可用?我不会在父项目和子项目中更改范围.因为我在运行时需要父类的一些类

How can I make it available? I won't change the scope neither in the parent project nor in the child project. Because I need some classes of the parent in at runtime

推荐答案

正如 Charlee Chitsuk 在他的回答中所述,您的范围(子项目中的编译)将省略测试 包含项目的依赖项.

As outlined by Charlee Chitsuk in his answer, your scope (compile in the child project) will omit the test dependencies of the included project.

一个干净的解决方案是将项目的关注点分成两个单独的项目:

A clean solution would be to separate the concerns of the parent project into two separate projects:

  • parent-test:该项目包含具有 compile 范围的测试依赖项(例如 selenium-firefox-driver).此外,该项目包含您所有的通用测试资源,例如src/main/java 中的 JUnit 基类.
  • parent-business:该项目仅包含父项目的业务功能.在 test 范围内包含 parent-test.
  • parent-test: This project includes your test dependencies (e.g. selenium-firefox-driver) with a compile scope. Additionally, this project contains all of your generic test resources, e.g. JUnit base classes, in src/main/java.
  • parent-business: This project contains only the business functionality of the parent project. Include parent-test here with test scope.

在您的子项目中,您现在还可以将 parent-test 包含在范围 test 中,让您可以在正确的范围内访问其资源.

In your child project, you can now include parent-test with scope test as well, giving you access to its resources with the right scope.

此设置将提供清晰的项目结构和清晰的范围,避免您提到的问题.是的,由于额外的项目,它有点复杂,但它也更干净.

This setup will provide a clear project structure with clean scoping, avoiding issues like you have mentioned. Yes, it's a bit more complex due to the additional project, but it's a lot cleaner as well.

这篇关于Maven 测试依赖的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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