简体   繁体   中英

Forcing maven spring boot project to use older version of a dependency instead of a new version from another dependency

I'm running in to a problem where I cannot start a spring boot server due to the same problem listed in this question:

How to set up Spring Boot and log4j2 properly?

I am encountering this scenario because the spring boot project has a dependency on a jar that includes elasticsearch, which includes a new version of slf4j that isn't compatible with spring boot

I tried the recommended solution by implementing every exclusion in the elasticsearch project dependency definition possible, but for some reason the new version keeps being picked up. I cannot seem to force the spring boot project to ignore the logging packages used by the elasticsearch project.

Here is my pom for the spring-boot project, see the dependency for problematic.project.import : http://pastebin.com/Yeq2qk9Y

Here is the pom for the project that is being imported into the spring boot project: http://pastebin.com/gknmf6Tt

The error I am getting is:

Caused by: java.lang.NoSuchMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:165)
at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadDefaults(Log4J2LoggingSystem.java:148)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:75)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:50)

Any tips on how to get this issue cleared? Is this possible for two versions of this set of libraries to be loaded, each module ignorant to the version they don't need?

You can exclude the cyclic dependencies by using the <exclusions> tag in your pom.xml like this:

<dependency>
  <groupId>sample.ProjectB</groupId>
  <artifactId>Project-B</artifactId>
  <version>1.0-SNAPSHOT</version>
  <exclusions>
    <exclusion>
      <groupId>sample.ProjectE</groupId> <!-- Exclude Project-E from Project-B -->
      <artifactId>Project-E</artifactId>
    </exclusion>
  </exclusions>
</dependency>

You should exclude the cyclic dependency of the newer version from the dependency which is having it and that way only the older version will be loaded and not both.

Here is the link for more information:

https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM