Maven external dependencies


Maven external dependencies

Maven uses the idea of Repositories to handle dependencies. But what if the dependency isn't present in any remote repositories or the central repository? Maven uses the idea of External Dependency to give a solution for such a scenario.

For example, let's make the following changes to the project we built in the 'Creating Java Project' chapter.

To the src folder, add the lib folder.

Any jar can be placed in the lib folder. We used the ldapjdk.jar package, which is an LDAP assistance library.

Our project's structure should now look like this:

Image Not Found

This is a common scenario when you have your library exclusive to the project and contains jars that may not be available in any repository for Maven to download from. If your code uses this library with Maven, the build will fail since Maven cannot download or refer to it during the compilation phase.
To deal with the scenario, add this external dependency to maven pom.xml as shown below.

<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 aforementioned example, the second dependent element under dependencies clarifies the following crucial facts concerning External Dependency.

External dependencies (the location of the library jar) can be defined in the same way as other dependencies in pom.xml.

The groupId must be the same as the library's name.

The artifactId must be the same as the library's name.

Set the scope to system.

Set the system path to the project's location.

I hope you now have a better understanding of external dependencies and will be able to declare them in your Maven project.