Library dependencies outside WAR file - java

I have a requirement where i need to pack all the common classes of various web services and make them as dependencies(To avoid duplicates for multiple services). The structure of my EAR file is as follows:
META-INF/MANIFEST.MF
META-INF/application.xml
commons-codec-1.2.jar
MyWebService1.war
MyWebService2.war
MyWebService1.jar
MyWebService2.jar
EJB_Bean.jar
Now i have included the dependency jar's under WAR file's Manifest as follows(Not sure if appropriate),
`
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_51-b13 (Oracle Corporation)
Class-Path: MyWebService1.jar
In the stated structure when i try to deploy the EAR file onto web-logic server, i get java.lang.NoClassDefFoundError, But whereas if i move the dependency jars under WEB-INF/lib folder of my WAR and repack my EAR file, Deployment goes fine as expected.
Is there any way i can include my WAR file dependency libraries outside WAR file or even outside my EAR file is my question. Any help or suggestion would be appreciated.

A simple way to set the libraries of an EAR package is to create a lib folder right under the the EAR root and put your libraries inside.
In this case you will have something like this in your EAR:
META-INF/MANIFEST.MF
META-INF/application.xml
MyWebService1.war
MyWebService2.war
lib/commons-codec-1.2.jar
lib/MyWebService1.jar
lib/MyWebService2.jar
EJB_Bean.jar

In tomcat jar files can be placed inside the lib directory and these jar files gets available to all the applications deployed on that server. In web-logic there must be the similar way to do it. A lib folder from where the web-logic server loads all the jar files.

My first advice would be to use some kind of dependency management such as Maven, Gradle or Ivy - doing that by yourself is very difficult and error prone.
A simpler solution in your case would be to take your "common jars" (especially if they are shared between multiple applications), and add them to the shared library of your webserver. As an example, Tomcat will load all librairies that are placed under tomcat-dir/common/lib directory.

Related

how to refer a jar(this jar is part of other war file) file in .war manifest.mf file

I have a war file, this war contains multiple jar files. Now my requirement is I have another war file which has dependency on jar files of first war. So Could any one help me how to refer the first war's jar files in MANIFEST file of the second war.
I think you can deploy those WARs in an EAR, and put the common resources in the EAR. Then you can update the manifest in each WARto use the resources in the EAR.
First of all, interdependent WAR applications that are not a part of the same EAR, sound like an awful idea. With that said, nothing is preventing you from extracting the common jars into application server's lib directory. But, why wouldn't each application contain it's own dependencies?

Getting class from ear/war in ear/lib/jar returns ClassNotFoundException

My project structure is the following. Server is Glassfish 4. IDE - netbeans 7.4. Project temp is used by project a.
EAR
+ lib/temp.jar
+ a.war
Temp.jar contains core classes. They are used by war. Temp.jar doesn't import any files from war, and it must know about war nothing. But, temp.jar has classes that creates instances of war classes by reflection (according to xml file). Here I get ClassNotFoundException. How can it be fixed? Is it possible to fix or I have wrong project structure?
Note: I didn't put temp.jar in a.war as there can be different war files that can use this jar.
I found the answer.
If we put jar into ear/lib folder then the classes in it can be accessed but they can't access the classes out of lib (in this case there is no need to add something to manifest file of war).
If we put jar in root of ear or in any other folder ear/bum than the classes of these jar can access war classes.
So I conclude, that /lib is monodirectional, any other are bidirectional.
Hope it will save someone a lot of time.
Add jar file to the you lib folder.
Change MANIFEST.MF and add the particular jar path to Bundle-ClassPath.
That worked for me.

How do I make tomcat include libs from the current project?

I've read somewhere as a good practice, that it's better to have the required libs included in each project, then just add all the libs you'll ever need to the tomcat folder. Well right now Tomcat seems to need all the required libs to be in lib folder of Tomcat. How can I make it use the libs from the build path of the project? i'm using tomcat 7.032 in through Eclipse.
The libraries of a webapp must be in the WEB-INF/lib directory of the deployed web application directory or war file.
Every jar file in this directory will be in the classpath of the webapp, and won't be in the classpath of the other deployed webapps. You indeed shouldn't put webapp libraries into Tomcat's classpath.

How to add jars available in a particular folder to classpath at runtime only for specific wars:

I am using maven build tool.
The following two are my intentions.
1) To move some of the third party library jars out of my war from WEB-INF/lib folder [note: These jars are common between more than 2 war files (or artifacts)]
2) To make the war file small in size.
Is it possible to move those jars out of war and put it into a folder and these jars should be referred in the classpath only by the wars which require it.
I have tried adding the path to the jars in the Class-Path: of MANIFEST.MF of war files but it did not work out. Please help me out.
I assume the jars in question are necessary for compiling the code that make up the web application. If the third-party jars are necessary for compilation but you don't want them in your war file, you have two options:
In the <dependency></dependency> section for the third-party jar, add "<scope>provided</scope>".
Configure the maven war plugin to exclude the jars you don't want in the war. See http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html for the details.
How you get the jar files that you exclude from the war into the web container's classpath depends on your environment. If you are using ear files, the Maven docs above talk about how to get the jars included in the ear file. If you are not using ear files, you will have to come up with your own way to get the jars into the container's lib directory.

ant: Need help packaging an EJB and its dependencies into an EAR

My goal is pretty simple: to use ant to build an EAR which contains 1 EJB and 1 jar containing all of the dependencies. This jar, called common.jar for the sake of example has vendor jar files in it as well as other xml files that the EJB depends on and will need to be able to see during runtime....
So far I have everything packaged correctly as an EAR like this:
EARFILE.ear
-EJBFILE.jar
/META-INF
-MANIFEST.MF
-common.jar
/META-INF
-MANIFEST.MF
/lib
-(all vendor jars inside here)
-(All the xml config files are inside the root of the common.jar)
Inside the MANIFEST.MF for the EJBFILE.jar is...
Class-path: ../../common.jar
Inside the MANIFEST.MF for the common.jar is...
Class-path: ../lib/some_common.jar
When I deploy this the appserver (websphere) cannot find the JAR file when I try to start the server. I am getting the ClassDefNotFoundError because the classes inside the EJB cant find the vendor JAR files when I try to start the instance. However I know that common.jar is setup correctly though, else the EJB wouldn't have compiled since it needed to have those vendor jars on the classpath for javac.
So what I want to know is this:
How can I get the runtime to correctly see the Vendor jar files.
Will the EJB be able to see the xml files at run-time? I am concerned about this because these xml files are located outside of the EJB inside of a jar that is just in the EAR, it isn't even a module its just a jar inside the EAR.
Does it even matter when using websphere? From what I gather some containers don't even care what is in the Class-path of MANIFEST.MF.
There are several improvements I can suggest, based on running into similar problems.
First and most importantly, use the appxml attribute of the Ant ear task to specify your deployment descriptor (usually named application.xml); also include references to the vendor JAR files bundled as defined below
I would recommend you not put your vendor JAR files into another JAR - instead, just copy them into the EAR at the same level as EJBFILE.jar
The configuration XML files can go in a sub-directory of the EJBFILE.jar (such as config), and then you can reference them as /config/filename.xml.
The application.xml file will tell WebSphere where to find your JAR files. Classpath traversal in an application server is not the same as that of a compiler, which JBoss has taught me the hard way.
I am using all of the above patterns, and my in-container code (deployed in the EAR) can see all my XML files, as well as find all my dependencies.

Categories

Resources