Maven
Maven is a project-centric automated build tool , mainly for Java project management and build . It provides a unified way to describe the structure of the project , dependencies and build process , simplifying the project build and management .
Key features of Maven:
-
Project Object Model (POM): Maven Usage
file to define the project's dependencies, plug-ins, and build configurations.The POM is the core of a Maven project and describes basic information about the project.
- Dependency management: Maven allows developers to easily manage the libraries and frameworks needed for their projects. By declaring dependencies, Maven automatically downloads the required libraries and their dependencies, resolving version conflicts.
- plug-in system: Maven provides a rich set of plug-ins to perform various tasks during the build process, such as compiling code, packaging, and running tests.
- life cycle management: Maven standardizes the build process by defining a project's lifecycle, which mainly consists of cleanup, compilation, testing, packaging, and deployment phases.
- Multi-module project support: Maven supports multi-module projects, allowing related modules to be organized in the same project structure for easy management and building.
- Community Support: Maven has huge community support and provides a large number of plugins and resources to help developers work efficiently.
1. Common dependency conflict errors
1.1. Version Conflict Error Reporting:
[WARNING] Found version conflict(s) in library dependencies; some are suspected to be binary incompatible:
[WARNING]
[WARNING] :commons-lang3:jar:3.4 is referenced from more than one dependency.
[WARNING] - :commons-lang3:jar:3.4 (compile)
[WARNING] - :commons-lang3:jar:3.5 (compile)
1.2. Class not found or method not found:
NoClassDefFoundError orClassNotFoundExceptionorNoSuchMethodException
Common but not limited to the following exceptions: .
: org/apache/commons/lang3/StringUtils
or
: . (Ljava/lang/CharSequence.
NoSuchMethodException
Dependency conflicts may indirectly cause a NoSuchMethodException. for example:
Different versions of libraries: If different versions of the same library are introduced into the project, Maven may choose a version that may be missing certain methods, leading to a NoSuchMethodException at runtime.
Dependency Passing: Some dependencies may introduce specific versions of other libraries, which may also result in a NoSuchMethodException if there are incompatible methods between those versions.
1.3 Conflicts in the dependency tree:
for example:
[INFO] +- :spring-boot-starter-web:jar:2.3.:compile
[INFO] | +- :spring-boot-starter:jar:2.3.:compile
[INFO] | | +- :spring-boot:jar:2.3.:compile
[INFO] | | +- :spring-boot-autoconfigure:jar:2.3.:compile
[INFO] | | +- :spring-boot-starter-logging:jar:2.3.:compile
[INFO] | | | +- :logback-classic:jar:1.2.3:compile
[INFO] | | | | +- :logback-core:jar:1.2.3:compile
[INFO] | | | | \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] | | | +- .log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] | | | | \- .log4j:log4j-api:jar:2.13.3:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] | | +- :-api:jar:1.3.5:compile
[INFO] | | +- :spring-core:jar:5.2.:compile
[INFO] | | | \- :spring-jcl:jar:5.2.:compile
2. Check if the maven dependencies are conflicting
2.1, pom dependencies, here to show some of the dependencies for disassembly
2.2. Use idea's own tools for troubleshooting and analyzing:The red line.It would indicate a conflict.
2.3 Tools: maven helper
First of all I use the idea tool, which allows you to install the plugin maven helper.
2.4. Restart the idea
This thing is loaded, we close the window, it may ask you to restart it, so you do as you're told. After that we open the pom file and click on Dependency Analysis.Switch to: Dependency Analyzer
3. Analyzing conflicts
3.1. Right click ----- "Jump to Source" will jump back to your own pom file.(I'm jumping to line 56)
At this point we can just keep clicking down and go to themybatis 3.5.14 dependency path andmybatis 3.5.15 Conflict Dependency Path Versions
4. Solutions
There are a total of four solutions:
1. Principle of priority of the first statement
In the configuration file, if there are two dependency declarations with the same name and different versions, the one written first will take effect (same file).
So, just declare the version of the jar package you want to use first.
2, Path proximity is preferred
Direct dependencies take precedence over pass-through dependencies. If a pass-through dependency has a conflicting version of a jar package, then you can declare a dependency jar of the specified version yourself to resolve the conflict.
3. Principle of exclusion
Passing dependency conflicts can be resolved by declaring exclusions in the passing dependencies of unneeded jars.
The exclusion flag is automatically generated in the pom file.
<dependency>
<groupId></groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
<!-- rule out conflicting mybatis(3.5.15) -->
<exclusions>
<exclusion>
<artifactId>mybatis</artifactId>
<groupId></groupId>
</exclusion>
</exclusions>
</dependency>
Results:
Refresh: Go back to pom and click Refresh, then click Refresh UI in maven help.
In the end there are only three dependency conflicts left
4, the principle of version locking
In the configuration file to declare which version of the corresponding jar package to be used, after the declaration of other versions of the jar package are not dependent. This solves the dependency conflict.
<dependencyManagement>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
</dependencies>
</dependencyManagement>
Result: Dependency conflicts can still be eliminated
Feel free to point out anything wrong with the article at the end!!!!
If you feel it would help youkudosorfocusA little bit of it!!!