Glassfish deployed project's dependency libraries - java

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.

Related

Eclipse's "serve modules without publishing" equivalent in IntelliJ

When I checked the "serve modules without publishing" in eclipse,eclipse would add all of my maven dependencies to context xml like
And the tomcat would run with adding this jars to command line classpath.
But in Idea,I set deployment to exploded,all of my maven dependencies were copided to output/WEB-INF/lib
Project Structure
How can i acheive running tomcat without coping maven dependencies?

How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.
2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository
3) When I unzipped my .JAR, I only saw my single class that I created custom.
This brings up two questions:
1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?
and
2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?
Thanks
SOLUTION:
The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.
I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.
No, they are never included (unless you use a special plugin which does that).
See 1.
If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.
For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

Only deploy classes JAR from a Maven WAR Project

I have a Maven project which creates a WAR file as well as a JAR file (with the -classes classifier, using <attachClasses>true</attachClasses> in the maven-war-plugin config). When I run mvn deploy, both of these artifacts gets deployed to Maven Central (via Sonatype Nexus).
That's all very well, but it's not really necessary to deploy the WAR to Maven Central - just deploying the -classes JAR would be sufficient, since the WAR is always uploaded to our github site for download, and only the JAR will ever be used as a dependency to another project.
Is there a way to configure this in Maven? I thought that <primaryArtifact>false</primaryArtifact> in the maven-war-plugin config might fix it, but it did not.
According to the Maven WAR Plugin FAQs, this problem needs a bit of refactoring in order to be solved. You would have to split the project into one "classes" module that only produces the classes JAR, and a second module which builds the whole webapp into a WAR file. The WAR module can then include the JAR module as a dependency.
Different Maven profiles (one for building and deploying the JAR, another one to build the WAR and skip deployment) might be a solution too. But to be honest, that seems rather hacky and harder to maintain. I am not sure that it would make your life easier in the long run.

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

Add Maven repository to Tomcat's classpath

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...

Categories

Resources