Update 构建工具.md

更新 Gradle 示例和依赖冲突的处理方法。
This commit is contained in:
CyC2018
2026-10-26 22:04:09 +08:00
parent 22eb77fe4e
commit d57319b29e
+10 -3
View File
@@ -49,12 +49,14 @@ Gradle 和 Maven 的区别是,它使用 Groovy 这种特定领域语言(DSL
而 Gradle 只需要几行代码:
```java
```groovy
dependencies {
testCompile "junit:junit:4.10"
testImplementation "junit:junit:4.10"
}
```
早期版本中的 testCompile 在 Gradle 7 中已被移除,改用 testImplementation。Gradle 也可以用 Kotlin 编写构建脚本(build.gradle.kts),Gradle 8.2 起新建的项目默认使用 Kotlin。
## 三、Maven
### 概述
@@ -114,11 +116,16 @@ A -> C -> X(2.0)
### 解决依赖冲突
找到 Maven 加载的 Jar 包版本,使用 `mvn dependency:tree` 查看依赖树,根据依赖原则来调整依赖在 POM 文件的声明顺序。
先用 `mvn dependency:tree` 查看依赖树,找到冲突的版本分别是从哪条路径引入的,再用以下方法确定使用的版本:
- 在 dependencyManagement 中声明版本,所有传递依赖都使用这个版本;
- 用 exclusions 排除某个依赖传递过来的版本;
- 在当前 POM 中直接声明需要的版本,它的路径最短。
## 参考资料
- [POM Reference](http://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification)
- [Introduction to the Dependency Mechanism](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)
- [What is a build tool?](https://stackoverflow.com/questions/7249871/what-is-a-build-tool)
- [Java Build Tools Comparisons: Ant vs Maven vs Gradle](https://programmingmitra.blogspot.com/2016/05/java-build-tools-comparisons-ant-vs.html)
- [maven 2 gradle](http://sagioto.github.io/maven2gradle/)