Girlfriend their project uses spring-boot, with spring-boot-parent as parent:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.0.9 < / version > < / parent >Copy the code

My girlfriend recently wanted to use ElasticSearch as a search engine and added dependencies to the project

< the dependency > < groupId > org. Elasticsearch < / groupId > < artifactId > elasticsearch < / artifactId > < version > 7.10.2 < / version > </dependency>Copy the code

Write the code, a run, reported that there is no exception class:

java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/DeprecationHandler at com.lv.springboot.datasource.ClientUTis.main(ClientUTis.java:13)  Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.xcontent.DeprecationHandler at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 moreCopy the code

MVN Dependency: Tree elasticSearch (MVN Dependency :tree)

Org. Elasticsearch. Client: elasticsearch - rest - high - level - client: 7.0.1 | -- org. Elasticsearch: elasticsearch: 5.6.16 | - org. Elasticsearch. Client: elasticsearch - rest - client: 7.0.1 | -- org. Elasticsearch. Plugin: parent - join - client: 7.0.1 | - org. Elasticsearch. Plugin: aggs - matrix - stats - client: 7.0.1 | -- org. Elasticsearch. Plugin: rank - eval - client: 7.0.1 | - org. Elasticsearch. Plugin: lang - mustache - client: 7.0.1Copy the code

[bug Mc-10866] – dependencies for elasticSearch are specified as the root poM of the project. Shouldn’t this dependency prevail?

My girlfriend asked me for help, and in the spirit of “object oriented”, I immediately put down my work to help check. A look at carefully, the original SpringBoot DependencyManagement, org. Elasticsearch: elasticsearch has been included (the following excerpt) :

<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> < version > 2.0.9. RELEASE < / version > < properties > < elasticsearch. Version > 5.6.16 < / elasticsearch version > < / properties > <dependencyManagement> <dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> </dependencies> </dependencyManagement>Copy the code

Spring-boot actually takes into account that the user may want to change the version, so it puts the version in . Properties also has the shortest path rule, so you can add the same key version to the properties in your project root POM:

< the properties > < elasticsearch. Version > 7.10.2 < / elasticsearch version > < / properties >Copy the code

All the Properties that can be replaced in this way are listed in the spring-Boot documentation. Refer to the documentation appendix: Version Properties

You can also use the shortest path principle of dependencyManagement by adding a dependencyManagement to your project root pom:

<dependencyManagement> <dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId> <version>7.10.2</version> </dependency> </dependencies> </dependencies> </dependencies> </dependencyManagement>Copy the code

Finally, remember the following principles to know which version of the project depends on:

Maven dependencies can be broken down into the following parts:

  1. Direct dependencies, as in the Dependencies section of this project
  2. Indirect dependencies are the dependencies contained in the Dependencies section of this project
  3. Dependency management is the dependency in this project dependency Management
  4. Parent’s direct dependency
  5. Parent’s indirect dependencies
  6. Dependency management for parent
  7. Direct dependency on BOM (usually not)
  8. Indirect dependency on BOM (generally not)
  9. Bom dependency management

Dependence can be understood as follows:

  1. First, insert parent’s direct dependency, indirect dependency, and dependency management into this project before the direct dependency, indirect dependency, and dependency management of this project
  2. For direct dependencies, if version is present, then place it in DependencyMap in sequence. If there is no version, the version is retrieved from dependency management and placed in the DependencyMap. Key is the dependent groupId + artifactId, value is version, the value of the last one will be replaced by the value of the same key
  3. For each dependency, load its own pom file as 1,2, but if there is a dependency version in step 1, use the dependency version of this project’s dependency management. Generate TransitiveDependencyMap, which contains all indirect dependencies.
  4. All indirect DependencyMap (s) are placed in a DependencyMap of the project if a key is not present in the project’s DependencyMap
  5. If there are indirect dependencies in TransitiveDependencyMap, perform 3, 4 recursively.

This DependencyMap is placed first and then TransitiveDependencyMap is removed, which explains the shortest path principle maven relies on.

“Bom” has the same effect as “Parent” except that “Bom” has only dependencyManagement dependencies

After solving the problem and combing to sister understand, sister promised to give me more than 100 pieces of pocket money this month, happy ~~~~~