简体   繁体   English

Maven测试依赖关系

[英]Maven test dependencies of dependency

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>

Now I "mvn install"ed the parent project, when I include this parent project as dependency in my child project 现在,当我将此父项目作为依赖项包含在我的子项目中时,我“ MVN安装”了父项目

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

selenium-firefox-driver is not available. selenium-firefox-driver不可用。 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 因为我在运行时需要一些父类

Instead of including the parent project as dependency in your child pom, you should make the parent project as the parent of your child pom. 不应将父项目作为子pom的依赖项包含在内,而应将父项目作为子pom的parent项。

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

You cannot call it a parent project otherwise. 否则,您不能将其称为parent project

As outlined by Charlee Chitsuk in his answer, your scope ( compile in the child project) will omit the test dependencies of the included project. 正如Charlee Chitsuk在他的回答中所概述的那样,您的范围(在子项目中编译 )将忽略所包含项目的测试依赖项。

A clean solution would be to separate the concerns of the parent project into two separate projects: 一个干净的解决方案是将项目的关注点分为两个单独的项目:

  • parent-test: This project includes your test dependencies (eg selenium-firefox-driver ) with a compile scope. parent-test:该项目包括具有编译范围的测试依赖项(例如selenium-firefox-driver )。 Additionally, this project contains all of your generic test resources, eg JUnit base classes, in src/main/java . 另外,该项目在src/main/java包含所有通用测试资源,例如JUnit基类。
  • 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. 现在,在子项目中,您还可以将parent-test包括在范围测试中,从而可以使用正确的范围访问其资源。

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. 是的,由于增加了项目,所以它有点复杂,但是也更干净。

The Dependency Scope told that the compile is the default scope, used if none is specified. Dependency Scope告知compile是默认范围,如果未指定,则使用默认范围。 It affects transitive dependencies in different ways. 它以不同的方式影响传递依赖。 By overview if the dependency is compile scope, the transitive test scope will be omitted. 通过概述,如果依赖项是compile范围,则将忽略传递test范围。 Please see further information at Maven: The Complete Reference: Project Dependencies . 请在Maven:完整参考:项目依赖项中查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发布阶段的Maven测试依赖 - Maven test dependency in release phase Maven测试中的persistence.xml依赖项 - persistence.xml dependency in maven test 具有依赖性的组件的单元测试具有依赖性,有什么问题? - Unit test for a component with dependency which has dependencies, what's wrong? 覆盖 FastAPI 依赖项以进行测试的最佳方法,每个测试使用不同的依赖项 - Best way to override FastAPI dependencies for testing with a different dependency for each test 如何在两个项目中的测试文件夹之间设置Maven依赖项? - How to setup Maven dependencies between test folders in two projects? Maven无法识别测试范围中的apklib依赖项 - Maven doesn't recognize apklib dependencies in test scope Maven:如何添加为运行时提供的依赖关系,但明确地用于测试? - Maven: how to add a dependency as provided for runtime, but explicitly for test? 解决test,testhelper和test-in-test之间的Maven循环依赖关系 - Resolving Maven circular dependencies between test, testhelper, and project-under-test 如何使Maven API测试模块具有对实现的隐式依赖以在IDE中进行测试? - How to make an maven API test module with implicit dependency on implementation to test within IDE? 在java中解决与maven的依赖关系 - Resolve dependencies with maven in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM