Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed name of <exclusions> element (was <excludes>)

...

A dependency is identified through its artifact key. The artifact key is that artifact's groupId, artifactId, and version. The groupId identifies under which group of artifacts that artifact belongs to, while the artifactId is the id of that artifact itself. The relationship between groupId and artifactid is analogous to that of Java's package and class. And of course, the version defines what version that artifact is. The version format maven uses by default is  following:

Panel

x[.y[.z]][-classifier][-i].


In some cases, artifact keys would be asked from you, and these are in this format.

Panel

 groupId:artifactId:version


But most of the time, you would be specifying dependencies in your POM.

...

 In your POM, you can specify your dependencies inside the dependencies tag. For example, if we have a project my.group:my-project:1 that declares sample.group:sample-artifactA:1, sample.group:sample-artifactB:1, and sample.group:sample-artifactC:1 as its dependencies, we would declare it as

Panel

<project>
  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-project</artifactId>
  <version>1</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactC:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactC</artifactId>
      <version>1</version>
    </dependency>
  </dependencies>

</project> 


Our dependency tree would then be

Panel

 my.group:my-project:1
 |--  sample.group:sample-artifactA:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1


  This tree shows that our maven project my.group:my-project:1 has three dependencies directly under it: sample.group:sample-artifactA:1, sample.group:sample-artifactB:1 and sample.group:sample-artifactC:1. All of which would be automatically downloaded by Maven2 into your local repository and loaded in your classpath.

...

Depencdencies of your super poms are also inherited. Thus, if we have a my-group:my-parent:1 whose dependencies are sample.parent.group:my-artifactA:1 and sample.parent.group:my-artifactB:1

Panel

<project>
  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-parent</artifactId>
  <version>1</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.parent.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.parent.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

  </dependencies>

</project> 


and if we make our my.group:my-project:1 inherit my.group:my-parent:1

Panel

<project>

  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of the parent project -

...

>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>my.group</artifactId>
    <version>1</version>
  </parent>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-project</artifactId>
  <version>1</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactC:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactC</artifactId>
      <version>1</version>
    </dependency>
  </dependencies>

</project> 


our dependency tree would be

Panel

 my.group:my-project:1
 |--  sample.parent.group:sample-artifactA:1
 |--  sample.parent.group:sample-artifactB:1
 |--  sample.group:sample-artifactA:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1


These inherited dependencies are automatically downloaded and included in your classpath by Maven2 as well.

...

On the other hand, a transitive dependency is a dependency that is a dependency of your maven project's direct and inherited dependency. In our previous examples, my.group:my-project:1 has three direct dependencies and two inherited dependencies. However, those dependencies, may also have other dependencies. For example, sample.parent.group:sample-artifactA:1 my depend on sample.parent.group:sample-artifactAA:1 , sample.group:sample-artifactA:1 may depend on sample.group:sample-artifactAA:1, and sample.group:sample-artifactAB:2, while sample.group:sample-artifactC:1 may depend on sample.group:sample-artifactCA:1.

Panel

 sample.group:my-project:1
 |--  sample.parent.group:sample-artifactA:1
 |     `--  sample.parent.group:sample-artifactAA:1
 |--  sample.parent.group:sample-artifactB:1
 |--  sample.group:sample-artifactA:1
 |    |--  sample.group:sample-artifactAA:1
 |    `-- sample.group:sample-artifactAB:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1
       `-- sample.group:sample-artifactCA:1


Moreover, these transitive dependencies may also have other dependencies, and so on.

Panel

 sample.group:my-project:1
 |--  sample.parent.group:sample-artifactA:1
 |     `--  sample.parent.group:sample-artifactAA:1
 |            |-- sample.parent.group:sample-artifactAAA:1
 |            |-- sample.parent.group:sample-artifactAAB:1
 |            |    |-- sample.parent.group:sample-artifactAABA:1
 |            |    |    `-- sample.parent.group:sample-artifactAABAA:1
 |            |    `-- sample.parent.group:sample-artifactAABB:1
 |            |-- sample.parent.group:sample-artifactAAC:1
 |            |    `-- sample.parent.group:sample-artifactAACA:1
 |            `-- sample.parent.group:sample-artifactAAD:1
 |                  `-- sample.parent.group:sample-artifactAADA:1
 |--  sample.parent.group:sample-artifactB:1
 |--  sample.group:sample-artifactA:1
 |    |--  sample.group:sample-artifactAA:1
 |    `-- sample.group:sample-artifactAB:1
 |          `-- sample.group:sample-artifactABA:1
 |               `-- sample.group:sample-artifactABAA:1
 |--  sample.group:sample-artifactB:1
  `--  sample.group:sample-artifactC:1
         `-- sample.group:sample-artifactCA:1
               |-- sample.group:sample-artifactCAA:1
               `-- sample.group:sample-artifactCAB:1 


These transitive dependencies are taken care of by Maven2 as well. Therefore, you don't have to declare in your pom these transitive dependencies because Maven2 will already traverse trhough your depedency tree and download these dependencies and include them in your classpath.

...

To exclude a transitive dependency from your direct dependency, you can use the <exclude> <exclusions> tags. For example, to exclude sample.group:sample-artifactAB:1 and all its dependencies (direct and transitive), we can do the following

Panel

<project>

  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of the parent project -

...

>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>my.group</artifactId>
    <version>1.1</version>
  </parent>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-project</artifactId>
  <version>1</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
      

...

<exclusions>
        

...

<exclusion>
           <groupId>sample.group</groupId>
           <artifactId>sample-artifactAB</artifactId>

...

         </

...

exclusion>
       </

...

exclusions>
    </dependency>

    <!-

...

sample.group:sample-artifactC:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactC</artifactId>
      <version>1</version>
    </dependency>
  </dependencies>

</project>


 Dependency Management

As we can see, dependencies must always declare their groupId, artifactId, and version. However, if you have several maven projects, and you want to control the version of their dependencies, you can use the <dependencyManagement> tag.

Revising our my.group:my-parent:1 into my.group:my-parent:2 to use dependencyManagement, we will have the following:

Panel

<project>
  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-parent</artifactId>
  <version>2</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.parent.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactA</artifactId>
    </dependency>

    <!-

...

sample.parent.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactB</artifactId>
    </dependency>

  </dependencies>

  <!-

...

dependency management -

...

>
  <dependencyManagement>

    <!-

...

sample.parent.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.parent.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

  </dependencyManagement>

</project> 


As for the dependeny tree of our my.group:my-project:2 (which has the same pom content as version 1 except that version 2 inherits from my.group:my-parent:2 instead of my.group:my-parent:1), this would have no difference. Our depdency tree (excluding the transitive nodes) would still be

Panel

 my.group:my-project:1
 |--  sample.parent.group:sample-artifactA:1
 |--  sample.parent.group:sample-artifactB:1
 |--  sample.group:sample-artifactA:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1


However, if we create a my.group:my-parent:3 such that there is no dependencies tag,

Panel

<project>
  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-parent</artifactId>
  <version>3</version>

  <!-

...

dependency management -

...

>
  <dependencyManagement>

    <!-

...

sample.parent.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.parent.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

  </dependencyManagement>

</project> 


and let it be inherited by my.group:my-project:3, its dependency tree would be

Panel

 my.group:my-project:1
 |--  sample.group:sample-artifactA:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1


Note that  both sample.parent.group:sample-artifactA:1 and sample.parent.group:sample-artifactB:1 were not inherited. That's because they are not dependencies of the parent project. The parent project simply declared how its subprojects would resolve the versioning of these dependencies.

But if we would have my.group:my-project:3.1 that has the following POM

Panel

<project>

  <modelVersion>4.0.0</modelVersion>

  <!-

...

artifact key of the parent project -

...

>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>my.group</artifactId>
    <version>3</version>
  </parent>

  <!-

...

artifact key of this maven project -

...

>
  <groupId>my.group</groupId>
  <artifactId>my-project</artifactId>
  <version>3.1</version>

  <!-

...

list of dependencies -

...

>
  <dependencies>

    <!-

...

sample.group:sample-artifactA:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactA</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactB:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactB</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.group:sample-artifactC:1 -

...

>
    <dependency>
      <groupId>sample.group</groupId>
      <artifactId>sample-artifactC</artifactId>
      <version>1</version>
    </dependency>

    <!-

...

sample.parent.group:sample-artifactA -

...

>
    <dependency>
      <groupId>sample.parent.group</groupId>
      <artifactId>sample-artifactA</artifactId>
    </dependency>

  </dependencies>

</project> 


The versionless artifact key, sample.parent.group:sample-artifactA, would be resolved via the dependencyManagement thus resulting into the following dependency tree: 

Panel

 my.group:my-project:1
 |--  sample.parent.group:sample-artifactA:1
 |--  sample.group:sample-artifactA:1
 |--  sample.group:sample-artifactB:1
 `--  sample.group:sample-artifactC:1


Notice that even though the version for sample.parent.group:sample-artifactA was not mentioned in the dependencies tag, it was still resolved by matching it with the one declared in the dependencyManagement. Without the dependencyManagement, this project would have a build failure stating that a POM dependencies is lacking version.

...