65

If I was to use a 3rd party library that was not in the maven public repository, what is the best way to include it as dependency for my project so that when someone else checks out my code it will still be able to build?

i.e.

My Application "A" depends on jar "B" which does not exist in the public repository. I, however, wish to add "B" as a dependency to "A" such that when a person on the other side of the world could check out the code and still be able to build "A"

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
digiarnie
  • 22,305
  • 31
  • 78
  • 126
  • possible duplicate of [Can I add jars to maven 2 build classpath without installing them?](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them) – Jesse Glick Feb 06 '14 at 16:00

6 Answers6

65

You can install the project yourself.

Or you can use the system scope like the following:

<dependency>
    <groupId>org.group.project</groupId>
    <artifactId>Project</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/project-1.0.0.jar</systemPath>
</dependency>

systemPath requires the absolute path of the project. To make it easier, if the jar file is within the repository/project, you can use ${basedir} property, which is bound to the root of the project.

notnoop
  • 58,763
  • 21
  • 123
  • 144
  • This works well for proofing use of a jar if it's unavailable in a repository manager. – awhie29urh2 May 15 '12 at 18:06
  • 1
    Why i use this work-around in my maven web project,The project can not deploy to tomcat,but compile ok.Any advice?(Do not tell me install to local maven repository),thanks – Mr Lou Jun 26 '12 at 07:02
  • You save my day, thanks, notnoop. Adding external jar by project properties somehow just doesn't work. – oohtj Jan 16 '15 at 07:56
  • In my tests, this method prevents the local jar from being included in the war file. Maven does not give an error message, it simply does not include the war. – Haroldo_OK Sep 14 '17 at 16:56
18

If you have a parent project with a module that is in this situation (requires a dependency not in a repository) you can setup your parent project to use the exec-maven-plugin plugin to auto-install your dependent file. For example, I had to do this with the authorize.net jar file since it is not publicly available.

Parent POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <id>install-anet</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>mvn</executable>
                <arguments>
                    <argument>install:install-file</argument>
                    <argument>-Dfile=service/lib/anet-java-sdk-1.4.6.jar</argument>
                    <argument>-DgroupId=net.authorize</argument>
                    <argument>-DartifactId=anet-java-sdk</argument>
                    <argument>-Dversion=1.4.6</argument>
                    <argument>-Dpackaging=jar</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

In the above example, the location of the jar is in the lib folder of the "service" module.

By the time the service module enters the validate phase, the jar will be available in the local repository. Simply reference it in the way you set up the groupid, artifact, etc in the parent pom. For example:

<dependency>
    <groupId>net.authorize</groupId>
    <artifactId>anet-java-sdk</artifactId>
    <version>1.4.6</version>
</dependency>
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Domenic D.
  • 5,276
  • 4
  • 30
  • 41
  • 1
    This seems to be the right answer. Is there a way to install multiple dependencies on the example above? – javydreamercsw Oct 23 '12 at 13:50
  • You should be able to have multiple elements in the plugin. – Domenic D. Oct 23 '12 at 13:57
  • 2
    I was able to figure it out. I moved the configuration within the execution part and then had multiple executions, one for each dependency. Thanks! – javydreamercsw Oct 23 '12 at 14:11
  • 2
    So, my issue is that by following this method, if you include the dependency in your pom before you've initially run an `install`, then it tries to collect and resolve dependencies before the plugin has had a chance to install the third-party jar, thus failing the build. I I remove the third-party dependency declaration, run an `install`, then the jar gets installed to my local repository, I can add the jar declaration back to the pom, and *then* the build succeeds. – liltitus27 Nov 08 '13 at 18:40
  • Not sure what you situation is @liltius27. If you are installing the file in the parent pom, and use the validate phase in the module, it should work fine. If you're trying to do this all in one self-contained module, it may not work. Though, I'd consider running the install plugin code in the pre-clean phase. I have not tried this, but that is the earliest phase you can hook into to perform your own task. – Domenic D. Nov 09 '13 at 02:50
15

Using system scope may work but it is not recommended even in the Maven specification. it is not portable.

from Maven book:

system- The system scope is similar to provided except that you have to provide an explicit path to the JAR on the local file system. This is intended to allow compilation against native objects that may be part of the system libraries. The artifact is assumed to always be available and is not looked up in a repository. If you declare the scope to be system, you must also provide the systemPath element. Note that this scope is not recommended (you should always try to reference dependencies in a public or custom Maven repository).

The best approach is to install to your local repository or to your enterprise repository to be accessible to all your peers.

this is very easy if you are using a repository manager such as Nexus.

Kimball Robinson
  • 3,287
  • 9
  • 47
  • 59
rperez
  • 8,430
  • 11
  • 36
  • 44
  • If I were to install it to my local repo, how would others build the project if they did not have access to my local repo? for example, if I had an application that I made publicly available online and then you (rperez) wanted to download that project and build it, how would you access my local repo to build it? – digiarnie Aug 31 '09 at 22:23
  • 4
    that is why I suggested using an enterprise repository. when you have an ent. repository like Nexus that we are using, it function in two ways: 1) proxy to the public repositories - download all needed 3'rd party libraries and store them for uture use 2) store all of your organization's artifacts in addition you can also store libraries that are not on the public domain. this repository is accessible to whoever you want. you can put it in the company intranet or giv it a public URL - your choice. – rperez Sep 01 '09 at 19:38
  • I think you mean "stable" or "reproducible", not portable. Though you can ensure stability by making sure that the binary's source is stable, i.e. stable network share, or download from stable server. I think the Maven advice is being overly conservative here. (That said, just putting it in a local Maven repo is still the easiest approach, Maven ensures you have a consistent local cache, and more plugins can read GAV coordinates than file paths.) – toolforger Jun 21 '18 at 08:55
1

This solution worked for me; 1. Created a local-maven-repo in my project's root directory and copied all my jars in the 2. Executed the following command to generate the necessary pom files and metadata etc for each and every jar that I needed to use;

mvn deploy:deploy-file -DgroupId=<somegroupid> -DartifactId=<someartifact> -Dversion=1.0.0 -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=<path to jar file>

This generated a new jar file with a pom file inside the local-maven-repo and I was able to include into my project as a dependency like this;

    <dependency>
        <groupId>somegroupid</groupId>
        <artifactId>someartifact</artifactId>
        <version>1.0.0</version>
    </dependency>

Then mvn package ensured that my project dependencies are resolved and packaged with my war file.

0

If you are using groovy/grail tool suite (GGTS) then you can directly import that third party dependency (but be sure you have that third party dependency in your local repository) using below steps :

  1. Go to the Project Explorer and right click on project.
  2. Click on import option.
  3. Expend the maven option and select Install or deploy an artifact to a maven repository and click next.
  4. Brows and select that third party dependency using Artifact File option and enter the detail of Group Id, Artifact Id and Version using POM.xml file and click on finish

Wait some moment and possibly error would have gone for that problem.

0

Generally speaking, you should first put the 3rd party jar into your local repository. After that you can use it by adding the dependency into pom.xml.

For example.

1.put the jar into your local repository first:

mvn install:install-file -Dfile=<path-to-file>

Note: this command requires maven-install-plugin version 2.5 or later. If not, You can refer to Here

2.use the jar by adding the dependency into you project's pom.xml.
just add this into the pom.xml of your project:

<dependency>
  <groupId>${the groupId in the jar's pom.xml}</groupId>
  <artifactId>${the artifactId in the jar's pom.xml}</artifactId>
  <version>${the version in the jar's pom.xml}</version>
</dependency>

3.you can then package or deploy your project by running mvn package or mvn deploy

The 3rd party jar will also be included in the package.

Chen Zhu
  • 199
  • 1
  • 5