Importing and Managing Dependencies in Maven

I found out about import scope in maven and made a sample project below,

Project_1 POM file:

    <dependencyManagement>  
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies> 
    </dependencyManagement>
</project>

      

project_2 POM file:

<parent>
<groupId>com.sample</groupId>
  <artifactId>project1</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>    
...
...
<dependencyManagement>  
      <dependencies>
        <dependency>
          <groupId>com.sample</groupId>
        <artifactId>project1</artifactId>
        <version>1.0-SNAPSHOT</version>
           <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

      

However, this raises an error indicating that JUnit packages are not available in Project2. When I remove the dependencyManagement tag from the POM file project_1. Everything works fine. But according to the maven docs ,

This scope is only supported depending on the pom type in the section. It indicates that the dependency should be replaced with an effective list of dependencies in the specified POM. Since they are replaced, import volume dependencies do not actually participate in limiting the transitivity of the dependency.

I did as mentioned in the docs, what's wrong here?

+3


source to share


2 answers


First, make sure you are using Maven version 2.0.9 or newer that supports this feature. Secondly, your Project1 Pom.Xml is missing the "packing" attribute. Make sure your packaging and type have the same name in order to inherit all dependencies.

Also your id and artifact id must be the same as your project 1 Pom.xml file for it to import the dependency



If in doubt refer to this link https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies

0


source


It looks like you are trying to use a BOM POM and import that.

In this case yours Project_1

is the BOM POM and you include all project dependent dependencies in the item <dependencyManagement>

. It looks like you are doing it right.

To import BOM POM in your project, you need both <dependencyManagement>

, and <dependencies>

.

For example, yours Project_2

pom.xml

should include:

<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>com.sample</groupId>
    <artifactId>project1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencyManagement>
        <dependencies>
            <!-- Imports the bill-of-materials POM. -->
            <dependency>
                <groupId>com.sample</groupId>
                <artifactId>bom</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Add a dependency from the BOM POM.
        Note: no version or scope is required, it inherited from the BOM POM! -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

      

Here is the BOM POM definition for the above example:

<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>com.sample</groupId>
    <artifactId>bom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

      



There is documentation about this on the Apache Maven website (search for "bom").

Update:

BOM POM and parent POM are similar but different. BOM POMs are solely for importing dependencies into your project. Parent POMs are for multi-module projects and allow you to define Maven coordinates, plugins, and dependencies that all modules will inherit using the parent POM.

Below is an example of using the inherited dependency on the parent POM. Note that there are a few elements here that don't make sense in the spec spec.

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

    <parent>
        <groupId>com.sample</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>project1</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Inherited from the parent POM. -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

      

Below is the parent POM definition for the above example:

<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>com.sample</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--Global dependencies...-->
    </dependencies>

    <build>
        <pluginManagement>
            <!--Plugins...-->
        </pluginManagement>
    </build>
</project>

      

0


source







All Articles