I created 2 spring boot applications.
I used 1 of these jars in the other as a dependency.
Now, both these applications have their own db configurations in their application.properties file respectively.
But when I run the parent project and access classes which use the dependency jar.
The properties defined in the dependency project are not found and the dependency project refers the parent project's (application in which the dependency is used) application.properties file.
How can I make the dependency maven project to use its own application.properties file.
If you are using these dependencies as normal jars, you are probably misunderstanding the way classloaders work.
At any given time you can consider all files provided by the jars on the classpath to be thrown in together in a single large pool (scope/namespace). This mean that if you have TWO "application.properties" files from two different jars, only one of them will be visible.
You need to rethink your deployment scheme so your dependencies does not bring in their own "application.properties" files but they are baked in as part of your deployment packaging.
Related
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.
I've an EAR project that contains a lot of dependencies.
Some dependencies are big jar (more than 4 mb), so i preferred to install them as modules on wildfly and add a module-dependency in jboss-deployment.xml.
For istance we have org.mypackage.MyClass, it's better to load that class from a regular dependency inside the ear/lib dir or from JBOSS_HOME/module ? Are there loss of efficiency?
According to Wilfly Doc:
Class Loading Precedence
A common source of errors in Java applications is including API classes in a deployment that are also provided by the container. This can result in multiple versions of the class being created and the deployment failing to deploy properly. To prevent this in WildFly, module dependencies are added in a specific order that should prevent this situation from occurring.
In order of highest priority to lowest priority
System Dependencies - These are dependencies that are added to the
module automatically by the container, including the Java EE api's.
User Dependencies - These are dependencies that are added through
jboss-deployment-structure.xml or through the Dependencies: manifest
entry.
Local Resource - Class files packaged up inside the deployment
itself, e.g. class files from WEB-INF/classes or WEB-INF/lib of a
war.
Inter deployment dependencies - These are dependencies on other
deployments in an ear deployment. This can include classes in an
ear's lib directory, or classes defined in other ejb jars.
More on: Class Loading in Wildfly
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
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...
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"