Add Maven repository to Tomcat's classpath - java

I'm working with Maven and Tomcat. Some of the web applications I have to deploy use a lot of dependencies that are marked as "provided" in Maven. One example of these dependencies is spring-context.
So, when I package the project, those dependencies are not included in the lib folder of the WAR file.
Because of this, I'm getting
NoClassDefFoundError: org/springframework/context/ApplicationContext
I can't change the scope of the dependencies, and if possible, I don't want to include the dependencies JARs in the WAR file.
How can I add the Maven repository as a classpath to Tomcat, so it can resolve all the "provided" dependencies? Without copying the JARs to Tomcat's lib folder.
I tried the shared.loader property in catalina.properties, but it doesn't work recursively: I have to add each JAR path to the property's value.

A dependency is marked as provided when the app server or container already has it, and you don't have to put it in the war. This is the case i.e. for the servlets jar, but not for the spring-context. I think the better solution would be to mark this dependencies as "compile" instead of "provided".

You have few options here. Like #Andres said, you either add the JARs in the WAR or you add them to the classpath of Tomcat (ie lib folder).
While the concept of having a Maven-aware classloader is interesting, imagine all the possible jar version conflicts that could occur. War A having a provided dependency on Lib v1.0.1 and War B having a provided dependency on Lib v2.1.0, with Tomcat silently resolving these...

Related

How to remove jar files from war file when deploying a java web application with jboss

I built my netbeans project and it created a .war file including all the .jar libraries. I need to remove my all libraries from .war file. I tried to untick them from library folder but then the project does not deployed. How can I remove my libraries from the .war file and if I remove them where should I put them correctly. In jboss also there is a folder called lib in standalone folder.Should I put them there? If so how to do it. I am not using maven.
If you are using Maven set the dependency scope to the libraries you would like omitted to have scope provided. You can add the dependencies of your WAR to the MANIFEST.MF file or the jboss-deployment-structure.xml file using Maven. If the lirbaries are not JBoss modules by default, eg Orcale JDBC driver, then you will need to create these modules yourself. See the JBoss AS 7 documentation on how to do this.
You can try following approach. I haven't worked on Jboss so don't have detail idea about it.
Deploy each logical library (like "OpenJPA" or "Log4J") as a module, including its api and impl jars and any dependency JARs that aren't already provided by other AS7 modules. If there's already a module add a dependency on it rather than adding a JAR to your module. If several different libraries share some common dependencies, split them out into modules and add them as module dependencies in module.xml.
Use jboss-deployment-structure.xml to have your deployment .war / .ear / whatever declare a dependency on the module if it isn't autodetected and autoloaded.
Courtesy #Craig Ringer.
For complete thread go here

Order of resource loading in WAR

I have a Java webapp (WAR) that has several dependencies. Two of them contain resources to be exposed in the war (with the same name).
So my dependency tree looks like
The my issue is that the custom.css from the core.jar dependency is used if I access index.html
Is there a way to force the usage of the custom.css? I cannot modify core.jar
Thanks,
michael
Environment: Maven 3, Java 1.8, Tomcat 8
There's no guarantee as to the relative ordering of JAR files in WEB-INF/lib, but it is guaranteed that WEB-INF/classes will be ahead of all the WEB-INF/lib/*.jar files in the classpath. So if you can get the right custom.css into WEB-INF/classes/META-INF/resources in your final WAR file it will be used in preference to the ones in the lib JARs by ClassLoader.getResource.
You can extract and put necessary resources into myWAR itself.
As long as it's loaded by classloader (which is likely in your case) you're guarantied to not be able to force ordering.
You might be able to unpack the core.jar dependency and exclude the custom.css file as for example specified in Building a WAR project with unzipped JAR dependency?
In such case, you would exclude the core.jar dependency in maven-war-plugin (see https://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html) and configure maven-dependency-plugin to unpack core.jar to target/classes while excluding META-INF/resources/custom.css.

servlet-api-2.5 jar is causing error on deployment of war file

Building a simple web application. I see that servlet-api-2.5-6.1.9.jar is automatically added under Maven dependencies in Eclipse, even though this jar is not listed as a dependency in my pom.xml. Along with some other dependencies. This creates an issue when I try to deploy the war file on tomcat.
The method getDispatcherType() is undefined for the type HttpServletRequest
This goes once I remove that jar from WEB-INF/lib. But it's too cumbersome to do that each time I deploy the war file.
So I figured it out. I was building a selenium application. And servlet-api-2.5.jar was a dependency for selenium-server.jar. I added an exclusion in the pom.xml. And it solved the issue for me.

Glassfish deployed project's dependency libraries

I'm new to Java and dependency management.
I have an EJB jar project with a few maven dependencies. When deploying the project to glassfish I get exceptions, that the classes from those dependencies are not found.
So I've added a maven plugin to copy over the dependencies from the local repository to {glassfish_dir}/glassfish/domains/domain1/lib every build.
I'm also using Netbeans.
Is copying over the the dependencies the proper way to go about this? Is there a better way to make dependencies work with glassfish?
The best way is to make the Maven pom.xml that builds the .war declare a dependency on your EJB jar project. That way your EJB jar and any libraries that it has a dependency on, will be included in the .war file that Maven builds.

Spring - Ways to include a jar which itself is using Spring in a Spring project

I need to use some 3rd party jar in my project. The project is a Spring project and the jar is also using Spring.
Is there a way by which I can include the 3rd party jar in my project? I am finding it difficult to find each and every dependency of the 3rd part jar and inject it.
Shouldn't be a problem. Application contexts can be loaded independent of one another in the same JVM, generally. But if you're loading your bean definitions from a resource file in the classpath (e.g. using ClasspathXmlApplicationContext), make sure the location and name of your file does not conflict with the third-party JAR. For example, if they're both located at "/applicationContext.xml" in different JARs in the classpath, you will have a problem. Make yours unique.
I'm not clear exactly what you mean by "include".
If the 3rd party jar is defined as a dependency in your war project, it will automatically be bundled into the WEB-INF/lib folder of the war when it is packaged by the war plugin. Any class in the jar would then be on the classpath and therefore available to be referenced in your Spring configuration. Do you have a more specific requirement than this?
Also note that if the 3rd-party jar is a properly-defined Maven project, it's dependencies will be defined in its pom. Those transitive dependencies are also bundled into the war (unless you have them defined with a non-default scope in which case they might not be).
Any of the jars you find on the Maven central repository should be defined with all their transitive dependencies. If you're having trouble resolving them, please update your answer so that the relevant Maven credentials can be located.
Update based on your comment. Once the jar is on the war's classpath, you can reference any Spring configuration files it declares by importing them into your war's application context. You just specify the import in the form: "jar:file://jarName!/path/to/config.xml"

Categories

Resources