Poorly handled Maven dependencies often lead to problems that can be annoying. Today I want to share with you a dependency problem that you may have encountered before.

The problem background

There was an ES search project that was fine at the beginning, and after a while, it turned out that the startup times were wrong. Look at Git commit log, no changes, amazing code world.

The error is shown in the figure below. The obvious error, which is often encountered, must be a version dependent problem.

The picture

Kitty-spring-cloud-starter-elasticsearch was packaged by me and used version 6.8.7. It was normal at the beginning of the test, but suddenly it wasn’t. I looked at the dependency of the current project and found that it was 6.4.3. That’s why we can’t find the CountRequest class.

The picture

Question why

There is a version limitation somewhere in my project that covers version 6.8.7 of the Kitty-Spring-cloud-starter-ElasticSearch definition.

There is no corresponding configuration in the parent POM of the project, the only possible one is in Spring Boot.

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.1.6. RELEASE < / version > < relativePath / > < / parent >Copy the code

The configuration 6.4.3 is found in spring-boot-Dependencies.

< elasticsearch version > 6.4.3 < / elasticsearch version > < the dependency > < groupId > org. Elasticsearch < / groupId > <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.distribution.integ-test-zip</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> <type>zip</type> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>${elasticsearch.version}</version> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>Copy the code

If the parent POM has a version defined by dependencyManagement, the child module can use the version defined by the parent POM directly.

Here is our POM dependency:

The picture

Problem solving

Defining the version directly in the POM that uses the project takes precedence over the parent POM definition so that we can force the version we need to use. This can only be defined in kitty-cloud-search.pom.

<dependency> <groupId>com.cxytiandi</groupId> <artifactId>kitty-spring-cloud-starter-elasticsearch</artifactId> <exclusions> <exclusion> <artifactId>elasticsearch-rest-high-level-client</artifactId> <groupId>org.elasticsearch.client</groupId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> < version > 6.8.7 < / version > < exclusions > < exclusion > < artifactId > elasticsearch - rest - client < / artifactId > <groupId>org.elasticsearch.client</groupId> </exclusion> </exclusions> </dependency> <dependency> < the groupId > org. Elasticsearch. Client < / groupId > < artifactId > elasticsearch - rest - client < / artifactId > < version > 6.8.7 < / version > </dependency>Copy the code

It solves the problem. It doesn’t feel good all the time.

If you want to get around this problem, unless you want to use the version that Spring Boot has already defined, then it will be consistent, but there will always be some special requirements, although you define ES as 6.4.3 in spring Boot 2.1.6.RELEASE. But I think it’s pretty common to use other versions.

If you want to add spring-boot-Dependencies to your application, you can add spring-boot-Dependencies to your application. If you want to add spring-boot-Dependencies, you can add spring-boot-Dependencies to your application. Add elasticSearch.version to your project and use your own dependencies.

About the author: Yin Jihuan, a simple technology lover, the author of Spring Cloud Micro-service – Full stack Technology and Case Analysis, Spring Cloud Micro-service Introduction combat and Progress, the founder of the public account Monkey World. Personal wechat jihuan900, welcome to hook up.

If you are interested, you can pay attention to my wechat public number, Simtiandi, and read more technical articles for the first time. I also have some open source code on GitHub github.com/yinjihuan