From d57319b29e9477ed5c92083019155a23dc85d08b Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Mon, 26 Oct 2026 22:04:09 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=9E=84=E5=BB=BA=E5=B7=A5=E5=85=B7.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 Gradle 示例和依赖冲突的处理方法。 --- notes/构建工具.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/notes/构建工具.md b/notes/构建工具.md index bda24a11..99cb232e 100644 --- a/notes/构建工具.md +++ b/notes/构建工具.md @@ -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/)