What will the version be when I don't specify it in the Maven's pom.xml?

JavaMavenVersion

Java Problem Overview


Recently I wrote a project with maven, but I have a question about the version in maven pom.xml.

If I write such a dependency

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
     <!--No version here-->
</dependency>

what will the version be

  1. in a simple project, no subproject

  2. as a dependency in another project and that project use foo-bar-1.0.0

Java Solutions


Solution 1 - Java

Part 1. of your question: If you don't specify a version there are various different outcomes:

a) you will get an error...

> [ERROR] The project org.example:myproject:0.5-SNAPSHOT (D:\src\myproject\pom.xml) has 1 error > [ERROR] 'dependencies.dependency.version' for foo:bar:jar is missing. @ line 39, column 14

b) if you have defined the version in the dependency management of the project's parent's pom, then that version is taken. The parent of the project doesn't have to be an enclosing superproject. It can simply be a collection of appropriate definitions.

c) if another dependency of your project also depends on foo:bar, and specifies a version, then that version is taken. Maven 2 has a mechanism called transitive dependencies. If the version of a dependency is not explicitly specified for an artifact, it searches the dependency tree and uses the nearest definition in the tree. If there are two nearest definitions, then the first declaration wins (since maven 2.0.9).

Part 2.: I am not sure, but maybe the transitive dependency mechanism also works that way. But the documentation (as far as I understood it) doesn't mention that case.

But somehow I feel that the second part of your question doesn't make sense: I guess that in order to use an artifact as a dependency, you would have to build it first. So you'd have to know the version of the dependencies well before it is used as a dependency itself.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxnnyygnView Question on Stackoverflow
Solution 1 - JavaHokView Answer on Stackoverflow