What are “Java EE 7 API Library” and “Java EE Web 7 API Library” and when to use them?

笑着哭i 提交于 2020-01-03 19:31:25

问题


I have a full-fledged Java EE project running on GlassFish 4.1 / Java EE 7 (NetBeans 8.0.2) not using Apache Maven.

Depending upon the project functionality, the CDI dependency has to be added to both the projects/modules namely the EE module and the Web module (and a class library, if any).

I have been confusing for a long time seeing people recommending to add either "Java EE 7 API Library" or "Java EE Web 7 API Library" to the compile-time class-path as a CDI dependency (these libraries are bundled in NetBeans and readily available out of the box, when using NetBeans).

Since these libraries contain a collection of APIs possibly the entire Java EE stack starting from the Servlet API, adding one of these libraries to the compile-time class-path (especially in the EE project) does not make sense, when the CDI functionality is needed in Java EE applications.

Why is it suggested many a times especially in NetBeans projects to add one of these libraries, when only cdi-api.jar as a CDI dependency is sufficient?

I do not find a canonical answer on this site nor somewhere else as to which library exactly is to be added in NetBeans projects, when the CDI functionality is required in Java EE applications. Adding only cdi-api.jar goes fine, by the way.


回答1:


All of javaee-api, javaee-web-api and cdi-api are just API definitions. They do not contain the functinality, they contain only necessary interfaces instead to make your code compile. The result is that none of javaee-api nor javaee-web-api should be included in your application as they already are included with the application server. The implementation is also provided by the application server, which itself is quite big, sometimes with over 100MB of libraries.

If your application just depends on CDI, you are free to put just cdi-api as a dependency. If you want more from javaee, then it is better to choose one of the profiles (full or web). However, mind that the server always provides at least all API included in web profile, therefore it is worth to consider using it too. Picking dependencies selectively is worth only with application servers, which do not support Java EE completely (e.g. Tom EE). In that case, you sometimes even need to include the implementation with your application or put it inside the server.




回答2:


As far as I can tell, the Java EE 7 API Library provides the full spec profile, whereas Java EE Web 7 API Library provides only the web profile API. If you're deploying to something like Tomcat, then you only need the web profile (Jax-RS, JSF, JPA). If you plan on doing more enterprise level stuff with Jax-WS (heavy SOAP-based web services), EJBs, JMS, etc, then use the full spec. Do search for Java EE profiles. Last I checked the spec only defined two: FULL and WEB.

In either case, you should mark those as provided in your pom.



来源:https://stackoverflow.com/questions/32304478/what-are-java-ee-7-api-library-and-java-ee-web-7-api-library-and-when-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!