Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a maven dependecy can be defined without version tag?

I have a pom which works without defining dependency version in pom works fine and another one without dependency version which does not work.

The one which works:

<project>
    <parent>
        <artifactId>artifact-parent</artifactId>
        <groupId>group-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-a</artifactId>
        </dependency>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-b</artifactId>
        </dependency>
    </dependencies>
</project>

This one which does not work:

<project>
    <dependencies>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-a</artifactId>
        </dependency>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-b</artifactId>
        </dependency>
    </dependencies>
</project>

The only thing differ in these two seems to be:

<parent>
    <artifactId>artifact-parent</artifactId>
    <groupId>group-parent</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

Second one does not work it seems fine to me but my question is why the first one works ?

Quoted from maven pom reference :

This trinity represents the coordinate of a specific project in time, demarcating it as a dependency of this project.

So my question is how the first one is working?

like image 665
MD. Sahib Bin Mahboob Avatar asked Feb 03 '15 12:02

MD. Sahib Bin Mahboob


1 Answers

The main thing to notice here is:

<parent>
    <artifactId>artifact-parent</artifactId>
    <groupId>group-parent</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

Version of the dependency looks like to be defined in the parent pom. This can be something like this:

<project>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>group-a</groupId>
                <artifactId>artifact-a</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>group-a</groupId>
                <artifactId>artifact-b</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Again quoting from the doc :

This is because the minimal set of information for matching a dependency reference against a dependencyManagement section is actually {groupId, artifactId, type, classifier}.

Here we did not need to define the {type, classifier} as it is same as default value which is as follows :

<type>jar</type>
<classifier><!-- no value --></classifier>

If this value differ form the default, you need to define it explicitly in both parent pom and child pom.

like image 182
MD. Sahib Bin Mahboob Avatar answered Oct 14 '22 04:10

MD. Sahib Bin Mahboob