Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Including jar not found in public repository

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"

like image 956
digiarnie Avatar asked Aug 31 '09 02:08

digiarnie


4 Answers

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.

like image 116
notnoop Avatar answered Oct 03 '22 07:10

notnoop


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> 
like image 41
Domenic D. Avatar answered Oct 03 '22 06:10

Domenic D.


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.

like image 36
rperez Avatar answered Oct 03 '22 06:10

rperez


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.

like image 44
Chen Zhu Avatar answered Oct 03 '22 06:10

Chen Zhu