如何从Eclipse中排除Maven测试范围的依赖项(但将其保留在单元测试的路径中) [英] How to exclude a Maven test-scoped dependency from Eclipse (but keep it in the path for unit tests)

查看:171
本文介绍了如何从Eclipse中排除Maven测试范围的依赖项(但将其保留在单元测试的路径中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用Java EE库的示例

我一直在使用 javaee-api-6.0 库来编译我的Java EE应用程序。



然而,为了单元测试,我想要访问的不仅仅是api - 我需要一个实现。因此,我将嵌入的glassfish libs包含如下测试范围:

 < repositories> 
< repository>
< id> glassfish-extras-repository< / id>
< url> http://download.java.net/maven/glassfish/org/glassfish/extras< / url>
< / repository>
< / repositories>

<依赖关系>
<依赖关系>
< groupId> org.glassfish.extras< / groupId>
< artifactId> glassfish-embedded-all< / artifactId>
< version> 3.1.1< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> javax< / groupId>
< artifactId> javaee-api< / artifactId>
< version> 6.0< / version>
< scope> compile< / scope>
< type> jar< / type>
< / dependency>
< / dependencies>



按照预期的方式使用Maven



从我的理解是因为glassfish依赖关系的< scope> test< / scope> ,它不会包含在常规的 compile 阶段。



由于这两个依赖关系将被包含在 test-compile 阶段,我一定会将glassfish依赖放在javaee-api的依赖关系使得前者在编译测试类时将被优先使用。因此,当使用Maven构建时,此配置不是问题。



在Eclipse中不能按预期方式工作



但是,当使用m2e和Eclipse时,我的构建路径中列出了glassfish依赖项:





由于glassfish依赖关系在java-ee-api依赖之前列出, Eclipse正在使用错误的lib(glassfish,而不是java-ee-api)来验证/编译/查找javadoc。 (实际上,我不是100%肯定编译使用错误的lib - 这取决于Eclipse是否使用Maven来执行验证代码时使用的编译,而我不知道它是否是 - 但是javadoc查找肯定是引用错误的lib)



问题



如何阻止Eclipse使用glassfish lib除了运行单元测试?我想确保我的编译/ javadoc查找是在api上发生的,而不是api的特定实现。

解决方案

有一段时间,因为我第一次发布这个问题,但是:


如何在运行单元测试时停止使用glassfish lib?


你不能。 Eclipse与每个项目的一个构建路径的概念相关,m2e / m2e-wtp不能(或不会)克服此限制,如以下错误所述:



依赖关系的范围对Eclipse编译没有影响



更新08-June-2016



随着JEE7的发布, javaee-api jar文件现在包含实际可用的类文件。这意味着它可以在测试中使用,我不需要在pom文件中指定 glassfish-embedded-all jar文件。所以现在eclipse正在从正确的jar文件(即 javaee-api 而不是 glassfish-embedded-all )我不在乎测试范围 glassfish-embedded-all 仍然是在Eclipse中的类路径上。



这不是我最初提出的问题的解决方案,但它是解决我所基于的问题的解决方案当时正在体验。也许这会帮助别人。


I am having some problems getting Eclipse to honour a test-scoped Maven dependency - it is showing up on the build path and messing with eclipse's compilation / javadoc resolution.

An example with Java EE libs

I have been using the javaee-api-6.0 library to compile my Java EE application against.

However, for unit testing purposes, I wanted to have access to more than just the api - I needed an implementation. So I included the embedded glassfish libs with a test scope like so:

<repositories>
    <repository>
        <id>glassfish-extras-repository</id>
        <url>http://download.java.net/maven/glassfish/org/glassfish/extras</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
</dependencies>

Works as expected with Maven

From my understanding, because of the <scope>test</scope> of the glassfish dependency, it will not be included in the regular compile phase.

Because both dependencies would be included in the test-compile phase, I was sure to place the glassfish dependency before the javaee-api dependency so that the former would be used in preference to the latter when compiling the test classes. And so, when using just Maven to build, this configuration is not a problem.

Does not work as expected within Eclipse

However, when using m2e and Eclipse, the glassfish dependency is listed in my build path:

Because the glassfish dependency is listed before the java-ee-api dependency, it appears that Eclipse is using the wrong lib (glassfish, instead of the java-ee-api) to validate / compile / look up javadocs. (Actually, I am not 100% sure that compilation is using the wrong lib - it depends on whether under the hood Eclipse is using Maven to perform the compilation used when validating code, and I don't know if it is or not - but the javadoc lookup is definitely referencing the wrong lib)

The Question

How can I stop Eclipse from using the glassfish lib except for when running unit tests? I want to ensure my compilation / javadoc lookups are ocurring on the api, not a particular implementation of that api.

解决方案

It's been a while since I first posted this question, but:

How can I stop Eclipse from using the glassfish lib except for when running unit tests?

You can't. Eclipse is tied to the concept of one build path per project and m2e/m2e-wtp cannot (or will not) overcome this limitation, as described in the following bug:

Scope of dependencies has no effect on Eclipse compilation

Update 08-June-2016

With the release of JEE7, the javaee-api jar file now contains real usable class files. This means it can be used in tests, and I need not specify the glassfish-embedded-all jar file before it in the pom file.

So now that eclipse is pulling source and javadoc from the right jar file (ie javaee-api and not glassfish-embedded-all) I don't care as much that the test-scoped glassfish-embedded-all is still on the classpath in Eclipse.

It's not a solution to the question I originally proposed, but it is a solution to the underlying problem I was experiencing at the time. Perhaps it'll help someone else, too.

这篇关于如何从Eclipse中排除Maven测试范围的依赖项(但将其保留在单元测试的路径中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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