简体   繁体   English

Java EE7和CDI 2.0对Maven的依赖性是什么?

[英]What is the Maven dependency for Java EE7 and CDI 2.0?

I'm using JavaEE 7 but I want to use the CDI 2.0 API (with Weld and Tomcat, Jersey too). 我正在使用JavaEE 7,但我想使用CDI 2.0 API(也与Weld和Tomcat,Jersey一起使用)。 When I have the following Maven dependency, it uses the old CDI API for Event (so no fireAsync ): 当我具有以下Maven依赖项时,它将为事件使用旧的CDI API(因此,没有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): 在下面添加它不能解决问题(因为第一个maven依赖项会覆盖它):

    <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? 如何通过Maven获得所需的所有内容(Jersey / Jax-RS,Servlets,ServerEndpoint,CDI 2.0 / Tomcat 8中的Weld)?

As of now (May 2017), EE 8 still wasn't released. 截至目前(2017年5月),仍未发布EE 8。 And any EE 7 API dependency will bring in CD 1.2. 并且任何EE 7 API依赖项都将附带CD 1.2。 Therefore, you need to make use of Maven to resolve the conflicting dependency versions. 因此,您需要使用Maven来解决冲突的依赖版本。 I can think of two ways to this: 我可以想到两种方法:

1) Define CDI dependency before you define EE 7 API 1)在定义EE 7 API之前先定义CDI依赖项

When declaring versions in your <dependencyManagement> section of your pom.xml , Maven resolves dependencies from top to bottom, eg first encountered defines version. pom.xml <dependencyManagement>部分中声明版本时 ,Maven从上到下解析依赖关系,例如,首先遇到的是定义版本。 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. 这意味着,如果您首先定义javax.exterprise:cdi-api:2.0 ,那么它将花费2.0,一旦到达EE API,CDI版本将被忽略。 Hacky as this sounds, this is quite common way of manipulating dep. 听起来很,这是操纵dep的相当普遍的方式。 management section. 管理部分。

When using classical <dependency> the algorithm of dep resolution differs and uses the principal of 'nearest definition'. 当使用经典<dependency> ,深度分辨率的算法会有所不同,并使用“最近定义”的原理。 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). 如果深度相等,则首先遇到(从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). 因此,在这种情况下,只需直接使用version定义cdi-api依赖就可以解决问题(深度为1,而EE API的深度为2)。

2) Use exclusion(s) from artifact 2)使用人工制品中的排除

I haven't tried this myself but theoretically, it should be possible to exclude cdi-api from EE 7 API dependency. 我自己还没有尝试过,但是从理论上讲,应该可以将cdi-api从EE 7 API依赖项中排除。 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>

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. 如果将javaee-api cdi-api依赖关系放在javaee-api之前,它将解决您的问题,并且maven将获得“良好”的cdi-api依赖关系。

For your second question, javaee-api in 7.0 version is enough to have all the Java EE 7 API. 对于第二个问题, 7.0版的javaee-api足以包含所有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. 请注意,CDI 2.0不属于Java EE 7规范的一部分,因此您需要手动添加它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM