Home >Java >javaTutorial >How to avoid circular dependencies in Java projects

How to avoid circular dependencies in Java projects

王林
王林forward
2023-05-04 15:55:061349browse

For example:

How to avoid circular dependencies in Java projects

In the picture, our API library imports some libraries, but these libraries also depend on different versions of library X , this will prevent dependency conflicts.

How to solve this problem

Before there were Maven & Gradle library management tools, when we developed a Java project, we needed to spend a lot of time resolving dependency conflicts in our project. The introduction of Maven & Gradle tools has greatly solved our dependency problems, but many times we will still see problems such as: ClassNotFoundException, MethodNotSupportedException, NoClassDefNotFound. In many cases, we rely on the experience of developers to solve it manually. At the same time, we can also rely on Some development tools help us troubleshoot problems, such as:

Dependency Analyzer

in Eclipse/IntelliJ development tools

How to avoid circular dependencies in Java projectsIt can display all dependencies and assist developers in locating to the conflicting library.

When you detect which libraries contain dependencies that are causing problems, they need to be excluded from our dependency management files (pom.xml or build.gradle) All libraries with different versions of dependencies.

maven-enforcer-plugin plug-in

Maven provides the Maven-Enforcer-Plugin plug-in to verify compliance with the agreement (or verify the development environment). For example, JDK version, Maven version, development environment (Linux, Windows, etc.), dependent jar package version, etc.

To use the plug-in, you only need to introduce it in the pom: ###
<plugin>     <groupid>org.apache.maven.plugins</groupid>     <artifactid>maven-enforcer-plugin</artifactid>     <version>1.3.1</version>     <executions>         <execution>             <id>enforce</id>             <configuration>                 <rules>                     <dependencyconvergence></dependencyconvergence>                 </rules>             </configuration>             <goals>                 <goal>enforce</goal>             </goals>         </execution>     </executions> </plugin>
###In When performing mvn clean package, the conflicting jar version and its parent pom will be printed in the console, as follows: ###
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce) @ uaf-uafei-provider --- [WARNING]  Dependency convergence error for com.google.guava:guava:16.0 paths to dependency are: +-com.uaf.uafei:uaf-uafei-provider:0.2.09-SNAPSHOT   +-com.uaf:microservice-sleuth:0.2.09-SNAPSHOT     +-org.springframework.cloud:spring-cloud-starter-openfeign:2.1.5.RELEASE       +-io.github.openfeign:feign-hystrix:10.4.0         +-com.netflix.archaius:archaius-core:0.7.6           +-com.google.guava:guava:16.0 and +-com.uaf.uafei:uaf-uafei-provider:0.2.09-SNAPSHOT   +-com.uaf:microservice-apollo:0.2.09-SNAPSHOT     +-com.google.inject:guice:4.1.0       +-com.google.guava:guava:19.0  [WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message: Failed while enforcing releasability the error(s) are [ Dependency convergence error for com.google.guava:guava:16.0 paths to dependency are: +-com.uaf.uafei:uaf-uafei-provider:0.2.09-SNAPSHOT   +-com.uaf:microservice-sleuth:0.2.09-SNAPSHOT     +-org.springframework.cloud:spring-cloud-starter-openfeign:2.1.5.RELEASE       +-io.github.openfeign:feign-hystrix:10.4.0         +-com.netflix.archaius:archaius-core:0.7.6           +-com.google.guava:guava:16.0 and +-com.uaf.uafei:uaf-uafei-provider:0.2.09-SNAPSHOT   +-com.uaf:microservice-apollo:0.2.09-SNAPSHOT     +-com.google.inject:guice:4.1.0       +-com.google.guava:guava:19.0 ]

The above is the detailed content of How to avoid circular dependencies in Java projects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete