There may be situations where a library, which is not present in any Maven repository, needs to be used. We have seen one way to use it, that is, specifying it as a dependency with system
scope and explicitly specifying the path to it.
The problem with this approach is that this dependency will not be available if you need to distribute your project as a library.
Maven provides a mechanism to install an artifact to your local repository so that you can declare and use it like other dependencies.
Use the following steps to manually install the dependencies that aren't available in a repository:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>apache-tomcat</artifactId> <version>8.0.14</version> <type>tar.gz</type> </dependency>
The project will fail to compile with the error of a missing dependency
C:projectsapache-maven-cookbookproject-with-dependency-not-in-repo>mvn install:install-file -DgroupId=org.apache.tomcat -DartifactId=apache-tomcat -Dversion=8.0.14 -Dpackaging=tar.gz -Dfile=C:Users
aghuDownloadsapache-tomcat-8.0.14.tar.gz -DgeneratePom=true
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ project-with-dependency-not-in-repo --- [INFO] Installing C:Users aghuDownloadsapache-tomcat-8.0.14.tar.gz to C:softwaremavenorgapache omcatapache-tomcat8.0.14apache-tomcat-8.0.14.tar.gz [INFO] Installing C:Users aghuAppDataLocalTempmvninstall8295760271813162395.pom to C:softwaremavenorgapache omcatapache-tomcat8.0.14apache-tomcat-8.0.14.pom
The install-file
goal of the Maven Install plugin allows dependencies to be installed to the local repository. It takes groupId
, artifactId
, version
, and packaging
type as parameters so that it can place the dependency suitably in the repository as well as create a simple pom file for it.
This method is not ideal in a project with multiple developers, as each developer needs to perform this step manually. One way to deal with this is to install this dependency in a repository manager that is used by the organization. As the developers will be using this repository manager as a mirror
, Maven will find the dependency from the mirror
and proceed.
In such a case, we could use the deploy
goal of the Maven deploy plugin to install the artifact to the remote repository.
Some remote repositories have access control. Maven allows access details to be specified in the server
element. It is best to specify this in settings.xml
as this file is specific to each user.
Projects with dependencies that are installed by this method are again not distributable, as those using them will fail to find the dependencies.
Where projects are expected to be distributed and included by others as dependencies, a different approach needs to be followed—the static in-project repository solution. Use the following steps to follow the in-project repository approach:
<repository> <id>in-project-repo</id> <releases> <checksumPolicy>ignore</checksumPolicy> </releases> <url>file://${project.basedir}/lib</url> </repository>
mvn install:install-file -DgroupId=org.apache.tomcat -DartifactId=apache-tomcat -Dversion=8.0.14 -Dpackaging=tar.gz -Dfile=C:Users
aghuDownloadsapache-tomcat-8.0.14.tar.gz -DgeneratePom=true -DlocalRepositoryPath=lib
What have we achieved? Now, the dependency is packaged along with the source code in the lib
folder of our project and available for distribution. This is transparent to the user as they do not need to do anything special to access it.
3.144.19.243