Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:srcmyprojectpom.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...