1

I'm using JavaEE 7 but I want to use the CDI 2.0 API (with Weld and Tomcat, Jersey too). When I have the following Maven dependency, it uses the old CDI API for Event (so no fireAsync):

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>

Adding this below it does not fix the issue (since the first maven dependency overrides it):

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0</version>
    </dependency>

How would I get all the things I need (Jersey/Jax-RS, Servlets, ServerEndpoint, CDI 2.0/Weld in Tomcat 8) via Maven?

1
  • try removing the javaee-api and replace it with implementation <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-core</artifactId> <version>3.0.0.Final</version> </dependency>
    – walkeros
    May 29, 2017 at 18:36

2 Answers 2

3

As of now (May 2017), EE 8 still wasn't released. And any EE 7 API dependency will bring in CD 1.2. Therefore, you need to make use of Maven to resolve the conflicting dependency versions. I can think of two ways to this:

1) Define CDI dependency before you define EE 7 API

When declaring versions in your <dependencyManagement> section of your pom.xml, Maven resolves dependencies from top to bottom, e.g. first encountered defines version. This means if you first define javax.exterprise:cdi-api:2.0, then it will take 2.0 and once you reach EE API, CDI version will be ignored. Hacky as this sounds, this is quite common way of manipulating dep. management section.

When using classical <dependency> the algorithm of dep resolution differs and uses the principal of 'nearest definition'. Image the dependencies as a tree with your project being the root. The shortest path (lowest depth) in the tree from root to the version of given dependency is always taken. In case of equal depths, first encountered is taken (since Maven 2.0.9).

Therefore in this case, simply defining cdi-api dependency with version directly should do the trick (depth will be 1, while with EE API depth would be 2).

2) Use exclusion(s) from artifact

I haven't tried this myself but theoretically, it should be possible to exclude cdi-api from EE 7 API dependency. Something along these lines:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.exterprise</groupId>
            <artifactId>cdi-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
7
  • Actually your first answer is not really right. Maven uses the nearest-wins strategy, meaning that if you have twice the same dependency in your tree, the nearest one (not meaning the first one) is picked up. However it looks like it does not work this way with provided dependencies (or optional dependency).
    – Rouliboy
    May 30, 2017 at 7:52
  • Hmm provided and optional are a different story I guess. But otherwise I have more than few times encountered what I describe here - the top-most dep was used. I guess that makes it "nearest" in a sense. But I don't really feel like reading whole Maven doc to find the paragraph defining this. If you know where to look for it, please share.
    – Siliarus
    May 30, 2017 at 7:57
  • I agree with you, I also encounter situations like you describe, but always (from what I remember) with provided or test scope (never had the problem with runtime or compile scope). Here is the documentation : maven.apache.org/guides/introduction/… , see "Dependency mediation".
    – Rouliboy
    May 30, 2017 at 8:04
  • @Rouliboy I think I know the cause of our disagreement - there is a difference between having a classical <dependency> and a <dependencyManagement> section. The latter does not use the nearest dep based on depth but rather the closets as I described. And since the OP commented on your answer saying that swapping helps, I guess he does have it in management section. As for your comments - you are right for the classical dependecies but also note that in case of equal depth, first encountered is taken. Will edit the answer to make it clearer.
    – Siliarus
    May 30, 2017 at 8:20
  • thanks for the clarification. However it seems that it does not work this way regarding the original problem of this post. I tested without dependencyManagement section and maven does not pick up the nearest but the first dependency. I will give another try.
    – Rouliboy
    May 30, 2017 at 8:44
0

If you put the cdi-api dependency before the javaee-api, it will solve your problem and maven will pick up the "good" cdi-api dependency.

For your second question, javaee-api in 7.0 version is enough to have all the Java EE 7 API. Note that CDI 2.0 is not part of the Java EE 7 specification, that's why you need to add it manually.

2
  • 1
    this does work but it seems to be wrong or a bad idea. it's really just loading those definitions first and letting the others "fail". but the javaee-api loaded jars might have issues with the cdi 2 classes loaded.
    – Don Rhummy
    May 29, 2017 at 23:13
  • 1
    This is what you need to do until the 8.0 release of the Java EE spec is finalised.
    – Steve C
    May 30, 2017 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.