4

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.

corneliu
  • 656
  • 13
  • 37
  • Is your goal to have a single jar with all dependencies in it, that you can run later? Maybe you should check this out: http://stackoverflow.com/questions/16222748/how-to-build-fat-jar-with-maven – jbarrueta Apr 20 '15 at 20:26
  • Possible duplicate of [Maven: Export jar with 3rd-party jars added to classpath in manifest](http://stackoverflow.com/questions/13882468/maven-export-jar-with-3rd-party-jars-added-to-classpath-in-manifest) – durron597 Apr 20 '15 at 20:31
  • It's possible with dependencies on system path, see this answer http://stackoverflow.com/a/765032/2417043 – arghtype Apr 20 '15 at 21:40
  • Do these mysterious jars load classes from those other unnamed jars using something like the Java `URLClassLoader` class? So you're trying to preserve the path to keep that back-door hack alive? – chrisinmtown Apr 21 '15 at 00:36
  • Thank you all for your comments. I have added an Edit section to my original post. At jbarrueta: I hoped to avoid redistributing the common jars be it inside or outside of the main jar. At durron597: I hoped to avoid redistributing the dependency jars. At arghtype: I hoped to avoid manually importing every single jar in the pom file (there are lots of jars). At chrislott: Yes I am using URLClassLoader but i am not trying to keep any back-door hack alive. The fact that you do that doesn't mean everybody does that. – corneliu Apr 21 '15 at 15:07

1 Answers1

0

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.

Dmitry
  • 121
  • 4
  • Thank you Dmitry. The whole project being a dependency would not be a problem. However your solution is good if the first project uses maven. However the first project in my case uses ant and creates the jars in an arbitrary place unknown to maven. I will mark your answer as solution because it is the closest to what i was looking for. – corneliu Apr 21 '15 at 15:21
  • You are welcome and thank you for marking my answer as a reply! – Dmitry Apr 21 '15 at 15:55