为什么 vs code 不导入我添加的与 gradle init 默认配置不同的新依赖项?

问题描述

我使用的是 gradle 7.02(并且使用 gradle init 和选项来创建一个使用 groovy 作为构建语言的单个 java 项目) -VS 代码版本:

版本:1.46.1 提交:cd9ea6488829f560dc949a8b2fb789f3cdc05f5d 日期:2020-06-17T21:17:14.222Z(11 个月前) 电子:7.3.1 铬:78.0.3904.130 Node.js:12.8.1 V8:7.8.279.23-electron.0 操作系统:达尔文 x64 18.7.0

我有以下插件

-Chrome 调试器 -Java 调试器 -ESLint -Gradle 语言支持 -Java 扩展包 -Java 测试运行器 -redhat 对 Java 的语言支持 - Maven for Java -Java 项目经理

当我在 gradle 中运行任务以显示缓存时,它会显示正确的依赖项:

任务:app:showMeCache /Users/xxx/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.18.20/18bcea7d5df4d49227b4a0743a536208ce4825bb/lombok-1.18.20.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/30.0-jre/8ddbc8769f73309fe09b54c5951163f10b0d89fa/guava-30.0-jre.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.12.3/7275513412694a1aafd08c0287f48469fa0e6e17/jackson-annotations.22755513412694a1aafd08c0287f48469fa0e6e17/jarson-annotations.2 /Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.12.3/d6153f8fc60c479ab0f9efb35c034526436a4953/jackson-databind-32 /Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.12.3/deb23fe2a7f2b773e18ced2b50d4acc1df8fa366/jackson-core-2.12.3.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39/list9-enable-future-u避免冲突与番石榴.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.2. /Users/xxx/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.5.0/2f50520c8abea66fbd8d26e481d3aef5c673b510/checker-qual-3.5.0.jar /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.3.4/dac170e4594de319655ffb62f41cbd6dbb5e601e/error_prone_annotations.jar-2.3 /Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar

当我运行调试时,它显示它在我运行 gradle clean build --refresh-dependencies -debug 时下载了依赖项

如果我使用 gradle init 做了一个简单的项目,我能够让依赖项工作,但由于某种原因,当你做应用程序与代码时,没有在 java 项目->项目和外部依赖项下添加依赖项。

这是我的应用程序的构建文件

  /*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.0.2/userguide/building_java_projects.html
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
    
}

apply plugin: 'java'

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit Jupiter API for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'

    // Use JUnit Jupiter Engine for testing.
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:30.0-jre'

    implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.3'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
    compileOnly group: 'org.projectlombok',name: 'lombok',version: '1.18.20'

}

application {
    // Define the main class for the application.
    mainClass = 'query.App'
}

tasks.named('test') {
    // Use junit platform for unit tests.
    useJUnitPlatform()
}

task showMeCache doLast() {
    configurations.compileClasspath.each { println it }
}

这是在 vs 代码中正确下载依赖项的简单项目的构建文件

    apply plugin: 'java'


defaultTasks 'clean','compileJava'

repositories {
    mavenCentral()
}

dependencies{
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-api:5.7.1'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.3'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
}

test {
    useJUnitPlatform()
}

我在网上搜索了 vs code 的解决方案,但似乎找不到任何人遇到我的问题。我做错了什么?

解决方法

但是由于某种原因,当您执行应用程序与代码时没有添加 java项目->项目和外部依赖项下的依赖项。

添加依赖后,会弹出一个通知询问你

A build file was modified. Do you want to synchronize the Java classpath/configuration?

点击现在,您的项目将被重建,然后依赖项将被添加到项目和外部依赖项。如果没有,在重建工程后,点击刷新按钮:

enter image description here