Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to add local dependency to Maven project

There are a lot of questions about this, but the answers seem to contradict each other. So I wanted to ask it for my version of Maven (3.0.4).

I have a JAR file that's not part of any maven repository. It's a local dependency. I understand there are two ways to add it to my Maven project.

  1. Add it as a dependency and specify the file path in the <systemPath> property. (No other work needed.)
  2. Create a local repository <my-project>/repo and install the JAR in this repository. Then, add the new repository in my pom.xml file and add the new dependency.

I'm curious which way is better practice? I've heard some negative things about the <systemPath> property, although that way looks faster.

like image 861
ktm5124 Avatar asked Feb 05 '15 19:02

ktm5124


People also ask

How do you install custom library jar into Maven local repository?

In this case you can perform mvn initialize and jar will be installed in local maven repo. Now this jar is available during any maven step on this machine (do not forget to include this dependency as any other maven dependency in pom with <dependency></dependency> tag).


1 Answers

The answer is, it depends...

  1. If you add it as a system dependency it is likely to become path dependent which makes it more difficult to share among other developers. Sure you can also distribute the 3rd party jar relative to your POM via your SCM but the intention of systemPath is more for dependencies that are provided by the JDK or the VM. From the docs about systemPath:

    They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK, but where available as separate downloads earlier.

  2. To install the jar in the local repo is also not friendly for sharing. It requires all developers to "upload" the jar to their local repo before building. You can of course add a script that handles this for you but it is always easy to forget to upload and IMO a better solution is point 3 below. The local install process is described here.

  3. If you are working in an organization where the developers share the POM you should upload the 3rd party jar to a local repository manager. That way you can easily work with the 3rd party jar as if using any other maven enabled jar. Check this link for a comprehensive list on repository managers.

To summarize, if you are sharing the POM with others I would suggest that you upload it to a local repository manager. Otherwise the second option can work for you. As a last option, I would fall back to option number 1.

Also note that this has nothing to do with which version of Maven you are running.

like image 66
wassgren Avatar answered Sep 28 '22 14:09

wassgren