Software DevelopmentLearn How to Handling External Dependencies in Maven

Learn How to Handling External Dependencies in Maven

Handling External Dependencies

In the last article, we discussed how to build and test the JAVA application project created through Maven. In this article, we going to discuss on handling external dependencies in Maven. In the earlier articles, we have discussed about different types of Maven repositories and through these repositories, the actual dependency management is taken care by Maven. Consider the scenario when the required project dependency is not available in local, remote as well as in the central repository. In such scenario, the concept of external dependency comes into picture.

Let’s quickly recap what are local, central and remote repositories in Maven and then we will jump to external dependencies concept.

Local Maven Repository
A local Maven repository is nothing but a folder which is located on your local machine. The Maven plugin creates a folder for when you execute any maven command for the very first time. The purpose of a local repository is to keep all your project related dependencies such as library JARs, plugin JARs, etc. at one place. These dependencies are automatically downloaded into your local repository when you kick in a Maven build. Such a practice is very beneficial as it helps to mitigate the references to dependencies again and again which are available on remote machine during building a Maven project.

By default, a local Maven repository is created at %USER_HOME% directory by Maven plugin. If you want to override this default location, then you need to provide a different path in then Maven’s setting.xml file which is present at %M2_HOME%\conf directory. The following will be the content of setting.xml file which has the customized path defined for local Maven repository.

<settings 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/xsd/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal artifact Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <localRepository>C:/CustomizedLocalRepository</localRepository>
</settings>

Upon execution of the Maven command, the required project dependencies will be downloaded at the given customized path on your local machine.

Central Maven Repository
Central Maven repository is referred to the repository which is provided by Maven community. It is a giant repository that contains a large number of library JARs which are used very commonly. The following are the key concepts of central Maven repository.
• It is a repository which is wholly managed by Maven community.
• No configuration is required for this repository.
• Central repository can be searched through internet access. Its content can browsed through a URL − https://search.maven.org/#browse provided by Maven community. All the library JARs which are available at central repository can be searched and used by developer through this URL.

Remote Maven Repository
Remote Maven repository is very useful and comes into picture when the required library JARs are not even spotted in the central Maven repository that may result into the failure of build process after throwing the error message on the console. In order to mitigate such situations, the concept of remote Maven repository comes into the picture which is nothing but a customized repository that contains required project’s library JARs. In the following example, the POM.xml has number of remote repositories which will download the required dependencies (i.e. library JARs) when they are not present on the local repository as well as central repository.

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.eduonix</groupId>
	<artifactId>springframework.myfirstapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springframework.myfirstapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<targetPath>${project.build.directory}</targetPath>
			</resource>
		</resources>
	</build>
	<repositories>
		<repository>
			<id>org.springframework.spring-webmvc-portlet</id>	<url>https://mvnrepository.com/artifact/org.springframework/spring-webmvc-portlet</url>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
	</dependencies>
</project>

Handling External Dependencies
As an example, we have an existing Java project created in the last article where we are going make the following changes.
• Adding a lib folder under the src folder.
• Copying any customer jar (say hamcrest-core-1.3.jar) into the lib folder.
After making above changes in the project, our project structure should look as shown below.
our project structure
Here, we have our custom lib folder that contains a JAR library. This library is specific to our project and it is not present in the local, central and remote repositories. If we build our code with this library through Maven, then our Maven build is going to fail as it cannot refer to or download this library from any of its available repositories during the compilation phase. We can still handle this situation by adding this external dependency into maven pom.xml in the following manner.

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.eduonix</groupId>
	<artifactId>project</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>project</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<fork>true</fork>
					<executable>C:\Program Files\Java\jre1.8.0_91\bin\javacpl.exe</executable>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest-core</artifactId>
			<version>1.3</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/src/lib/hamcrest-core-1.3.jar</systemPath>
			<type>jar</type>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

Source code for about how to handle external dependencies in Maven

Conclusion: –
In this article, we discussed about handling the external dependencies in Maven when they are not present in local, remote and central repositories.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -