Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why order of Maven dependencies matter?

I thought that the order of Maven dependencies doesn't matter before and regard this as a pro of it. And this is my old pom.xml's dependencies:

<dependencies>      <dependency>         <groupId>org.glassfish.jersey.containers</groupId>         <artifactId>jersey-container-servlet</artifactId>         <version>2.19</version>     </dependency>      <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-web</artifactId>         <version>4.1.7.RELEASE</version>     </dependency>      <dependency>         <groupId>org.glassfish.jersey.ext</groupId>         <artifactId>jersey-spring3</artifactId>         <version>2.19</version>     </dependency>      <dependency>         <groupId>org.glassfish.jersey.media</groupId>         <artifactId>jersey-media-moxy</artifactId>         <version>2.19</version>     </dependency>  </dependencies> 

It works well, and today I wanna move spring dependency to the bottom so that those jersey related can be together. However then I can no longer make it working, my Jetty complains:

[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run (default-cli) on project mtest: Execution default-cli of goal org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run failed: A required class was missing while executing org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run: org/apache/commons/logging/LogFactory 

That is really confusing, so do I have to concern about dependencies order? How do I know the correct order?

like image 965
Elderry Avatar asked Jul 31 '15 07:07

Elderry


People also ask

Does order of Maven dependencies matter?

The order of dependencies does matter because of how Maven resolves transitive dependencies, starting with version 2.0.

In which order will Maven search for dependencies for a project?

Maven searches for the dependencies in the following order: Local repository then Central repository then Remote repository. If dependency is not found in these repositories, maven stops processing and throws an error.

What is the importance of dependency management in Maven?

What Is Maven Dependency Management? Dependency management in Maven allows teams to manage dependencies for multi-module projects and applications. These can consist of hundreds or even thousands of modules. Using Maven can help teams define, create, and maintain reproducible builds.


1 Answers

The order of dependencies does matter because of how Maven resolves transitive dependencies, starting with version 2.0.9. Excerpt from the documentation:

(...) this determines what version of a dependency will be used when multiple versions of an artifact are encountered. (...) You can always guarantee a version by declaring it explicitly in your project's POM. (...) since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

like image 59
kryger Avatar answered Oct 11 '22 06:10

kryger