Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven external dependency


May 26, 2021 Maven


Table of contents


Maven - External dependency

Now, as you know, Maven's dependency management uses the Maven-warehouse concept. B ut what if dependencies cannot be satisfied in remote and central warehouses? Maven uses the concept of external dependencies to solve this problem.

For example, let's make the following changes to a project created in the Maven - Create Engineering section:

  • Add the lib folder under the src folder
  • Copy any jar files under the lib folder. We're using the ldapjdk .jar, which is a help library for LDAP operations

Now, our engineering structure should look like the following:

Apache Maven external dependency

Now that you have your own library, it usually contains jar files that no repository can use and maven can't download. If your code is using the library, Maven's build process will fail because it cannot be downloaded or referenced during the compilation phase.

To handle this situation, let's add this external dependency to the maven pom .xml.

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.companyname.bank</groupId>
       <artifactId>consumerBanking</artifactId>
       <packaging>jar</packaging>
       <version>1.0-SNAPSHOT</version>
       <name>consumerBanking</name>
       <url>http://maven.apache.org</url>

       <dependencies>
          <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
          </dependency>

          <dependency>
             <groupId>ldapjdk</groupId>
             <artifactId>ldapjdk</artifactId>
             <scope>system</scope>
             <version>1.0</version>
             <systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
          </dependency>
       </dependencies>

    </project>

In the example above, the second element, <dependency> illustrates the key concepts of external dependency. <dependencies>

  • External dependencies (library jar locations) can be configured in the pom .xml other dependencies.
  • Specify the name of the groupId as the library.
  • Specify the name of the artifactId as library.
  • Specify scope as the system.
  • Specify the system path relative to the project location.

Hopefully now that you know about external dependencies, you will be able to specify them in your Maven project.