Best way to add local dependency to Maven project

2019-03-16 17:57发布

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.

3条回答
戒情不戒烟
2楼-- · 2019-03-16 18:13

You can add jar to your local maven repository. Usually it located at:

$home_directory/.m2/repository 

For example you have expample.jar and you want to add it to your pom as:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
</dependency>

Then you must add example.jar to:

$home_directory/.m2/repository/com/example/1.0/example.jar

In my case NetBeans do it for me.

查看更多
成全新的幸福
3楼-- · 2019-03-16 18:18

The best way I see so far is to use install:install-file goal with maven. You can use the mvn command line to execute it directly with appropriate parameters, or if you are using eclipse EE, you can do so by leveraging the embedded maven and creating a Run Configuration as follows:

enter image description here

Then, you include the jar as follows:

    <dependency>
        <groupId>mylocal.weka</groupId>
        <artifactId>weka</artifactId>
        <version>1.0</version>
    </dependency>

Of course adjust the parameters as per your needs.

Best, Haytham

查看更多
时光不老,我们不散
4楼-- · 2019-03-16 18:24

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.

查看更多
登录 后发表回答