Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 371
xnnyygn Avatar asked Sep 07 '12 09:09

xnnyygn


People also ask

What happens if you don't specify version in POM xml?

of your question: If you don't specify a version there are various different outcomes: a) you will get an error... 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.

What happens if you don't specify a version in Maven?

Maven won't allow any other either. Build will fail if version is not found.

What is version in Maven POM xml?

version. It is the sub element of project. It specifies the version of the artifact under given group. File: pom.xml.

What is model version in POM xml?

model version is the version of project descriptor your POM conforms to. It needs to be included and is set. The value 4.0. 0 just indicated that it is compatible Maven 3.


1 Answers

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.

like image 79
Hok Avatar answered Oct 18 '22 07:10

Hok