Third Party Libraries in EJB-JAR - java

at time i try to add third party libraries in a EJB-JAR. So far it seems to me, that it is not possible, but i am not sure, and why it is not possible.
I want to create single EJB-JARs and WARs, without put them together in one EAR.
Arjan Tijms shows in Java EE - EAR vs separate EJB+WAR three ways, and to clearify it, i want option 2.
My own research comes mostly to questions with following answers J2EE: How to package 3rd party JARs into an EJB jar?, but i don't want an EAR file.
On the oracle site itself shows following packaging for an EJB-JAR: EJB packaging, which don't have a own lib directory like a WAR. I already tried to put the libary in the root directory (Using eclipse + JBoss 7). My first question is, is it possible to add extra libaries into a EJB-JAR, like in a WAR file? This confuses me, because it is possible to deploy a simple EJB-JAR to a application server, but it seems you cannot add third party libaries. For me a EAR seems mostly "overkill", because it have for me no real advantage and second, if i put a library into a EAR, different EJB-JARs connot use different versions from third party libraries. I want the EJB-JAR complete independent from other EJB-JARs and WARs. I only created a additional simple jar project that contains the interfaces, that have to be implemented, and is added to each EJB project and the WAR project. That brings me to question 2: Have i use to an EAR, if yes, why? It seems to me that i missing something oder don't understand yet.

You can put third party JARs in a lib/ folder in the EAR and add
<library-directory>lib</library-directory>
to your application.xml.

Jars aren't supposed to contain jars.
I would just package the EJBs into a WAR (either directly in WEB-INF/classes or as separate ejb-jars in WEB-INF/lib), put the 3rd party libs into WEB-INF/lib and call it a day. Your approach is pretty limiting. You won't be able to inject your EJBs, and you would have to use a remote lookup.

Related

Where is the "right" place to put WAR dependencies?

I'm creating build scripts for an existing project. All of their EAR files are built (manually) and the way they do it is put the JAR dependencies of the WAR into the EAR lib folder. The WEB-INF/lib folder of the WAR files are empty. When I'm building with gradle it's putting the JAR dependencies of the WAR into WEB-INF/lib folder and nothing in the EAR lib folder.
It seems like either would work and further it seems (to me) that the more logical path is to put the WAR dependencies in WEB-INF/lib. Is there a preferred or more correct way?
If you only have one WAR file in the EAR, then I would definitely recommend using WEB-INF/lib. Further, I would recommend deploying the WAR directly without the EAR file, as that removes a layer of unnecessary complexity.
The case where you would want to put dependency JARs in the EAR is when you have multiple WAR files that need the same dependencies. Then you can save space by sharing the same dependencies with all of the WARs in the app.
Hope this helps!
As you noted, either works technically, if it's consistent - assuming the EAR just consists of a single web module, all that's really necessary is to put them all in one place, since stuff in the EAR can't "see" stuff in the WAR under default Java EE class loading configuration.
Conceptually, though, you are correct - it's more elegant and cleaner to put everything used by a WAR in the WAR itself, rather than putting it in an otherwise-vacant EAR. While not a big deal now, it becomes important if you expand your application to multiple modules (web or EJB), because putting stuff in EAR/lib eliminates your ability to manage multiple dependencies across the modules. If you need one version of a library in WAR1 and a different version in WAR2, that's impossible if you're packaging everything at the EAR level.

EJBs in libraries - Reusing EJBs

Let's say I want to create a library which I will use in future projects but I also want to include EJBs in that library referencing other EJBs etc.. That library would also contain simple java classes. What is the best way to do that? How do I define the dependencies in this case? I thought I would define them with annotations. If the user of the library wants to configure other dependencies he will be able to do so by overriding them inside the ejb-jar.xml of his project. Has anyone done something like this in the past? How would you go about it when developing in Eclipse?
Basically my problem is that as far as I can tell if I simply create an ejb-jar which I am going to include in all my projects the ejb container is going to instantiate my e.g. MDBs at deployment time even if I don't need all of the MDBs that are contained inside my library but only some of them.
Is a solution to not define MDBs as EJBs with annotations or inside the ejb-jar.xml but only their dependencies?
What about the session beans? Will they be automatically instantiated even if I don't use them inside a project?
EAR files. Although not very common, you could include your library and its dependencies in an EAR file and distribute that.
What I'd like to do is to distribute the library in its own Jar file along with a documentation of dependencies (e.g.: a Maven' POM file or ivy's xml file). Either way, you'll need a dependency manager.
There is also an option to build a fat JAR file in which all the dependencies are exploded. I don't really like that. If I have to include dependencies, I'd go with EAR files.

Where to put a shared library in JBoss AS 7

I have multiple web applications each using spring-hibernate and other open source libraries and portlets, so basically now each war file includes those jar files. How do I move these jars to a common location so that I don't have to put these in each war file? My jars are places in D:/ directory.
I tried creating modules but no success. e.g. if I added jar
<resources>
<resource-root path="mylib.jar"/>
</resources>
and mylib.jar needs another ABC class. That ABC class is in my WAR class-path. Here I get exception while loading this module. mylib.jar could not find ABC class and throws exception.
If those libraries are reused in several applications, probably the best solution would be to create JBoss modules.
For example, OJDBC library is used in several projects I'm developing. So, I added a new module to JBoss 7: https://community.jboss.org/wiki/CreateAModuleForOracleDatasourceInJBoss711Final (it's just an example).
But you said, that you tried creating modules, but with no luck. What was the problem? Did you get some errors?
EDIT
Answer updated in connection with updated question.
So, if I understood correctly, we can divide your libraries into two categories:
First category is "standard libraries": Spring, Hibernate, Log4j etc. So, these libraries might be added as modules into JBoss AS and reused in every WAR (scope=provided in Maven's dependency).
Any other non-standard libraries (i.e. written by yourself) might be added as modules as well. If these libraries require some other dependencies - these dependencies must be listed in module's XML file, as described in: https://docs.jboss.org/author/display/MODULES/Module+descriptors
Hope this helps at least a bit :)

JAVA EE glassfish include WAR inside EJB

I have a JAVA EE Project, containing both EJB and WAR projects inside of it.
I want to be able to access WAR project class from the EJB project class.
I have access the other way ( I can access ejb class from war).
Is that possibble? How can this be done?
Thank's In Advance.
I assume that you have got an EAR with two modules inside, WAR and EJB JAR. As both modules are independent, they shouldn't depend on each other. What you want to do is possible via MANIFEST.MF Class-Path entry in module META-INF folder, but I strongly discourage you to do so.
You can re-factor you application to following structure:
EAR/
ejb-app.jar
war-app.jar
lib/
common-libraries.jar
Just putyour common libraries to separate JAR (regular java project), and add it to ejb-app and war-app classpath.
Alternatively you can implement EJB's in WAR project as they are supported in WAR since Java EE6.

Tomcat 6 conversion to Glassfish v3: servlet-api, el-api jars

I used ant when building my web app from eclipse to deploy to Tomcat 6 and referred to servlet-api.jar and el-api.jar within the Tomcat 6 release tree rather than pulling them in to my deployed library folder.
I am trying to deploy to Glassfish v3. I've pulled the jars from the www.java2s.com website.
Was this the right place to get them?
Did I need to do it at all? I searched within Glassfish and the jars weren't there
If I didn't need to do it at all, is there another course of action to follow to ensure the same functionality is available?
Seeing this question and the other questions you posted I have the feeling that you're doing things completely wrong. Here's just an answer which should get it all straight.
You should never have separate copies of servletcontainer-specific libraries wandering around in the classpath.
You should never put copies of servletcontainer-specific libraries in webapplication's WEB-INF/lib.
In an IDE like Eclipse, you should never add servletcontainer-specific libraries separately in project's build path.
In a nut: just do not touch servletcontainer-specific libraries at all. Don't even think of downloading them separately. That's plain recipe for trouble. Having separate libraries of different servletcontainer makes will only result in collisions in classpath. The servletcontainer should be downloaded and treated as its whole own.
In Eclipse, when integrating a servletcontainer (Tomcat or Glassfish), just add it in the Servers view.
To associate a dynamic web project with a specific servletcontainer (server) so that you can compile servlets and so on, you need to select it in the Targeted Rumtimes section of the project properties. Then everything will go well automagically, thanks to Eclipse smartness. That's also the place to change the servletcontainer implementation whenever necessary. When you're creating a brand new dynamic web project, you can just choose the desired servletcontainer implementation from the servers dropdown in the wizard.
When you want to create a WAR, simply rightclick the dynamic web project, choose Export and then WAR file. No need for a separate ant task or so.

Categories

Resources