Manually installing dependencies that are not available in a repository

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.

How to do it...

Use the following steps to manually install the dependencies that aren't available in a repository:

  1. Add the following dependency to the simple project that we created earlier:
    <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

  2. Now run the following Maven command:
    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
    
  3. Note the result:
    [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
    

How it works...

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.

There's more...

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:

  1. Create a repository inside your project by adding the following in your pom file:
    <repository>
        <id>in-project-repo</id>
        <releases>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
        <url>file://${project.basedir}/lib</url>
    </repository>
  2. Use the following command to install the dependency to this 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243