Search code examples
jakarta-eemavendependenciesm2eclipsem2e

How to exclude a Maven test-scoped dependency from Eclipse (but keep it in the path for unit tests)


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:

dependency listed on 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.


Solution

  • 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.