Maven - Maven External Dependencies - maven tutorial



What is External Dependencies in Maven?

  • External Dependencies template is used to capture external dependencies, which are tasks, events, or deliverables that are outside of the Project Manager's control (performed by someone not on the project team) that must happen before the project can progress.
  • If dependency is not available in any of remote repositories and central repository. Maven provides answer for such scenario using concept of External Dependency.
 
maven dependency

learn maven tutorial - maven dependency - maven example

Example for Creating Project section in Maven.

  • Add lib folder to src folder
  • Copy any jar into the lib folder. We've used ldapjdk.jar, which is a helper library for LDAP operations.

Now our project structure should look like following:

 Maven External Dependencies

learn maven tutorial - maven dependency - maven example

  • Here you are having your own library specific to project, which is very usual case and it can contain jars which may not be available in any repository for maven to download from.
  • If your code is using this library with Maven then Maven build will fail because it cannot download or refer to this library during compilation phase.
  • To handle the situation, let's add this external dependency to maven pom.xml using following way.
maven tutorial tags : apache maven , maven repository , maven central

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi="http://www.wikitechy.com/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>
maven tutorial tags : apache maven , maven repository , maven central

Managing External Dependencies in Maven

  • 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.

Wikitechy provides an indepth knowledge on the below maven tutorial items such as maven download , maven install , maven goals , maven build , maven commands , maven plugin , maven search , maven eclipse , maven deploy , eclipse maven , maven junit , maven java , hibernate maven , maven project , java maven , maven version , maven proxy , maven 2 , maven pom , maven eclipse plugin , maven java version , maven properties , maven install windows , how to create maven project in eclipse , eclipse maven plugin , maven dependencies , what is maven in java , maven apache , maven project structure , how to install maven in eclipse

Related Searches to Maven - External Dependencies