Maven - Maven Manage Dependencies - maven tutorial



maven tutorial tags : apache maven , maven repository , maven central

How are the libraries interdependent in Maven

  • Maven is a excellent tool which helps you explore the world of open source in Java. But sometimes, you do not understand enough about Maven to control it. And so there may be some unexplained error occurred.
  • In this document, I would guide you how to avoid duplication library in Maven.
  • To make things easier you should see the contents of following pom.xml file:

Sample Code

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>org.o7planning</groupId>
   <artifactId>SimpleMavenWebApp</artifactId>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>SimpleMavenWebApp Maven Webapp</name>
   <url><http://maven.apache.org/></url>
 
 
 
   <dependencies>
 
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
       </dependency>
 
       <!-- JSTL Library -->
       <!-- <http://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl> -->
 
       <dependency>
           <groupId>org.glassfish.web</groupId>
           <artifactId>javax.servlet.jsp.jstl</artifactId>
           <version>1.2.4</version>
       </dependency>
 
   </dependencies>
 
 
   <build>
       <finalName>SimpleMavenWebApp</finalName>
   </build>
 
</project>
  • pom.xml file above you declared using javax.servlet.jsp.jstl library, this library depends on jstl-api. And jstl-api depends on two libraries servlet-api and jsp-api. The dependence is like the illustration below:
 learn maven tutorial - dependencies tree on eclipse - maven example

learn maven tutorial - dependencies tree on eclipse - maven example

 learn maven tutorial - dependencies in maven- maven example

learn maven tutorial - dependencies in maven - maven example

 learn maven tutorial - dependency hierarchy in maven maven example

learn maven tutorial - dependency hierarchy in maven maven example - maven example

  • The library that will be used is the library you declared to Maven and dependent libraries.

Exclude duplicate library

  • When declaring library in Maven, library duplication may be happened. For example, you declare use of two library A and B with Maven. A depends on C and D, and B depends on C and E.That means the C library duplicate. You can see the illustration below.
 learn maven tutorial - exclude duplicate library - maven example

learn maven tutorial - exclude duplicate library - maven example

  • The illustration above shows that there are two versions of C library will be used, which may cause errors in runtime of the application. You need to configure to remove one of two versions of the library C. Normally, you would remove the older version.

Using <exclusions> to remove the dependent libraries that you do not want to use.

Sample Code

<dependencies>
 
    <dependency>
        <groupId>aa</groupId>
        <artifactId>A</artifactId>
        <version>1.1</version>
    </dependency>
 
    <dependency>
        <groupId>bb</groupId>
        <artifactId>B</artifactId>
        <version>1.1</version>
 
        <exclusions>
            <exclusion>
                <artifactId>C</artifactId>
                <groupId>cc</groupId>
            </exclusion>
        </exclusions>
    </dependency>
     
</dependencies>

Eclipse tools support you in detecting and eliminating duplication. Take an example:

maven tutorial tags : apache maven , maven repository , maven central

Sample Code

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>org.o7planning</groupId>
   <artifactId>SimpleMavenWebApp</artifactId>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>SimpleMavenWebApp Maven Webapp</name>
   <url>http://maven.apache.org</url>
 
 
 
   <dependencies>
 
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
       </dependency>
 
       <!-- Servlet API - Servlet Library -->
       <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
           <scope>provided</scope>
       </dependency>
        
        
       <!-- JSTL Library -->
       <!-- http://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl -->
 
       <dependency>
           <groupId>org.glassfish.web</groupId>
           <artifactId>javax.servlet.jsp.jstl</artifactId>
           <version>1.2.4</version>
       </dependency>
 
   </dependencies>
 
 
   <build>
       <finalName>SimpleMavenWebApp</finalName>
   </build>
 
</project>
 learn maven tutorial - exclude duplicate library step1- maven example

learn maven tutorial - exclude duplicate library step1- maven example

Right-click the dependent library that needs to exclude, and choose Exclude Maven Artifact ..

 learn maven tutorial - exclude duplicate library step2- maven example

learn maven tutorial - exclude duplicate library step2- maven example

 learn maven tutorial - exclude duplicate library step3- maven example

learn maven tutorial - exclude duplicate library step3- maven example

Eclipse will automatically add the <exclusions> code in pom.xml:

 learn maven tutorial - exclude duplicate library step4- maven example

learn maven tutorial - exclude duplicate library step4- maven example

Here, there is no duplication library in your pom.xml file

 learn maven tutorial - exclude duplicate library step5- maven example

learn maven tutorial - exclude duplicate library step5- maven example


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 Manage Dependencies