Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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



...

 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.

 Inherited Dependencies

Depencdencies Dependencies of your super pom's parent poms are also inherited. (I use the term "parent poms" to refer to a poms parent and the parent's parent and so forth through all the ancester poms.) 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



...

A direct dependency is a dependency that is defined your maven project  ( your pom, plus the dependencies of its super parent poms ).

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 



...

To exclude a transitive dependency from your direct dependency, you can use the <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>



...

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



...

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



...

Maven2 first merges your pom with its super parent poms. (And the parent's parent and so forth.) The dependencies declared in your pom would override those declared in any of its super parent poms. The same goes for the dependencyManagement. The dependencyManagement would be merged with the dependencyManagement of your super parent poms, and if there are conflicts, the dependencyManagement of your pom would be prioritized.

...