How to include jars that load other jars in maven? - java

I am developing a Java application in NetBeans and using maven for the dependencies. I have a bunch of jars located in a folder. These jars load other jars located in a path known to them. It seems to me that, when i load a local jar in maven, it is actually copying the jar to another location. The problem is that the moved jar can't find the other jars anymore because now the relative path to them jars is broken.
Is it possible to use maven in such way that the included jars are not moved from their original place so they can find other jars? Or if this is not possible, is there a way to give maven an entire folder that contains subfolders, and if maven moves the entire folder, it would also move all the subfolders with the jars inside them?
I am not sure if I was clear enough. I am also new to maven. It seems to me that ant is more flexible in this regard.
Edit: After reading the comments it seems I was not very clear. Basically the company I work for has two applications that share some common jars that load other jars with URLClassLoader. I don't want to distribute these common jars again, i want my second application to find and load those jars from the location where my first application has put them. I found a solution by using maven to import one jar that uses URLClassLoader with a hard-coded path to load the other jars.

I have a module which pom file contains these lines in the beginning:
<groupId>com.domain.project</groupId>
<artifactId>MyFirstProgect</artifactId>
<packaging>jar</packaging>
<version>1.2.3-snapshot</version>
After I compile this project maven will put it into the local repository (mine is situated in the C:\Users\MyUser.m2\repository, in linux it should be somewhere in /home/myUser/.m2/repository).
Afterwards I can add a dependency in the second project like this:
<dependency>
<groupId>com.domain.project</groupId>
<artifactId>MyFirstProgect</artifactId>
<version>1.2.3-snapshot</version>
</dependency>
In my case this helped me.
P.S. But this will lead the whole first project to be added as a dependency. So maybe this is not the perfect solution for you.

Related

Is it possible to import a jar locally through Maven and also include that Jars dependencies?

I am trying to get a third party library to work in my application. It has one main jar file, core.jar. It is not available through Maven's online repository which is how we normally include third party libraries. We already have a local repository set up for maven:
<repository>
<id>project.local</id>
<name>Local Project Repo</name>
<url>file:${project.basedir}/local-repo</url>
</repository>
And the project is recognizing core.jar that I have put in the local-repo folder (along with a pom file). I can call methods from it, etc.
My issue is that I was provided the core.jar file along with many other jars that are apparently dependencies of core.jar. I cannot figure out how, if it is even possible, to get core.jar that I imported through the local-repo to recognize it's dependencies. Right now they are all sitting in the same folder as core.jar, but it is throwing errors saying that classes do not exist.
I tried to edit the pom.xml file for core.jar and add some dependencies there, but it does not seem to be working.
I have done a lot of googling around in the last few days but I have not been able to find any answers for how to do this. Maybe I am blind.
Is it even possible to make this work in this way?
The best approach is to set up a Maven .mirror for the use of all developers. A tool like Nexus is designed to do this.
Release your core jar to this mirror (or just copy it into the correct place in the repository file system). Then you can include your core jar in the same way that you include third party jars in your projects. Maven will automatically load any third party dependencies specified in your core jar's pom.xml.

Maven: How to add all jar files in lib folder during a web application build

I have an old project. I am trying to add maven build to the project.
All the jar files in the project are present currently in the WEB-INF/lib folder.
Is there a way to add these jar files to the classpath during source compilation and then have it in the war file lib
This is one of the places where switching to Maven hurts. There are a lot of suggestions where most short-circuit something you will need later, and hurt you there. I would suggest that you
Move your jar files out of the lib folder into another project folder not meaning anything special to Maven.
For all jars that you easily recognize, make the proper Maven dependency in your pom file. This will allow Maven to download sources and javadoc if present.
For the remaining jars, you can tell Maven to install them as part of your normal build as custom dependencies. I asked the same question years back and got a very useful response at Multiple install:install-file in a single pom.xml. This will allow you to get up and running quickly.
When you have the time, locate proper replacements for your custom dependencies.
Take your time doing this. It is tedious work but it pays off quickly.

For an external jar, is it necessary to add a dependency in pom.xml file with a system path if jar already present/copied to lib folder

I have downloaded ojdbc14 jar from the internet and copied it to the lib folder of my maven project. Is it necessary to add dependency in pom.xml as well. Currently working without adding.
You can use tricks to manually upload a jar into the lib folder, but it does not make sense. And it would work cause the build process will just look for that jar into the lib and if found everything will compile nicely. BUT....
Maven is a useful tool that helps you handle dependencies, internal, external, third parties, any kind, it's one of his benefits, don't need anymore to look around for jars, and put them manually into the lib dir, but why? You would override one of the basic behaviour of Maven.
Maven set lots of rules to give you the ability to manage them the way you want, you have options about how to handle every single dependency of your project, you can point to a local jar within a dependency, you can set the scope of the dependency, the type you can exclude some of the inherited transitives, and so on...
But this is the standard approach for standard situation
You should simply define the dependency, maven will downloaded from the configured repo or the default one, maven central, and retrieved from your local repo if there are no updates on that artifacts all the other time you will build that artifact.
If you have issues with licenses for ojdbc14 then the solution is configure the oracle repo where you can easily download it.

Where to put extension dependencies in neo4j 3

Where should I be putting the plugin dependencies in neo4j 3 (not the actual plugin jars).
In neo4j 2 there was a lib folder where the system would load the jars from but this no longer exists.
I've tried putting them in the plugin folder but then it requires me to add every dependency of the jars which becomes unmanageable and it also seems weird to have them there.
I've tried putting them in the neo4j bin directory but they aren't detected.
I've tried adding a command line argument to the vmoptions file with a wildcard pointing to a lib folder I created and this didn't work either.
Use maven shade plugin to package all the dependencies (except that one with scope test or provided) in the jar.
I think the documentation has just been updated (although I may have missed it). It says
Having built your code, the resulting jar file (and any custom dependencies) should be placed in the $NEO4J_SERVER_HOME/plugins directory. We also need to tell Neo4j where to look for the extension by adding some configuration in neo4j.conf:
So it turns out they should go in the plugins folder or you do as Daniela said and build a fat jar.
EDIT: I eventually came to the conclusion for several reasons including this one to use the server edition which has the same directory structure as the version 2.x.

Maven : Copy only selected jars inside WEB-INF/lib

In my Maven project , I have certain dependencies which should be present inside the WEB-INF/lib . I cannot put all the jars inside WEB-INF/lib , only the selected ones . How to go about doing this?
I cannot use the maven-resources plugin since then I would have to mention the entire jar's name inside <include> tag and I need to keep it dynamic.
I tried using <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> but this not give me an option to insert only selected jars inside the lib folder.
I also tried using <scope>provided</scope> for some of the jars but due to this the name of the jar doesn't get added to the classpath field inside manifest.mf file.
Please suggest some solution. Thanks in advance!
You can use Assembly plugin and customized assembly descriptor. In descriptor you can set which jars you want to copy with their artifact id, group id, version and scope. it also supports regex. https://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html
This may not be the best way to package the content for the WEB-INF/lib/ nested directory in a WAR file, but the implementation presented in the question for How to put all required jars in a lib folder inside the final jar with maven (using the maven-dependency-plugin plugin) solved my JAR needs for bundling all dependent jars into the lib/ directory of my final jar. Oddly it's a commonly asked question, with many different solutions. This implementation actually bundled the jars inside the nested lib/ directory, while others, for example, extracted the contents of my dependent jars and laid their classes beside my classes. So this might get you a little closer but probably not the official way of handling a WAR files' library deps.

Categories

Resources