Signup/Sign In

Managing External Dependencies in Maven

In the previous tutorial, we understood on how to build and test a maven project and the commands used for the same. In this chapter, we will learn about managing external dependencies for a project in maven.

As explained earlier, all the basic list of dependencies in maven is handled by the maven repository at which a project downloads the required dependencies. But, there will be certain scenario at which some particular dependencies may not be available in the maven remote and central repositories. Maven still answers this scenario by providing the feature of external dependency management.

An external dependency can be such as sqljdbc.jar or log4j. In maven, any external dependencies can be easily configurable as other dependencies in the pom.xml file. So, let us see a simple example to add sqljdbc.jar as an external dependency into a maven project.

  • Create a folder called lib in the maven project.
  • Download the sqljdbc.jar and put it inside the lib folder.
  • Now, open the pom.xml of the project and add a new dependency as shown below:
    • Specify groupId as name of the jar file
    • Specify artifactId as name of the jar file
    • Specify the scope of the system
    • Specify the relative path of the jar file to project location.
<dependency>
	<groupId>sqljdbc</groupId>
	<artifactId>sqljdbc</artifactId>
	<scope>system</scope>
	<version>1.0</version>
	<systemPath>${basedir}\src\lib\sqljdbc.jar</systemPath>
</dependency>

Dependency Scope

Dependency scope in maven is classified into 5 different types. Each of these scope controls the dependencies that are available in the class path and dependencies that are included in the application.

Dependency ScopeDescription
compileThis is the default scope for any external dependency in maven. These dependencies are made available in the classpath and they are packaged well.
providedThese dependencies are expected to be provided by the JDK or a container. The best example is the servlet api. These dependencies are made available on the classpath but not at runtime. Also, they are not packaged.
runtimeThese are the dependencies which are not required for the compilation, but required to execute and test the system. E.g. JDBC API
testThese dependencies are required only during the test compilation and execution phase.
systemThis scope is similar to "provided". The only thing is that developer need to explicitly provide the path of the jar file on the local file system.