简体   繁体   English

使用 Artifactory 依赖项时,VS Code Java 语言支持中的“未解析的依赖项”/“无法解析导入”

[英]"Unresolved dependency" / "import cannot be resolved" in VS Code Java language support when working with Artifactory dependecies

We have a bunch of private Java dependencies published to Artifactory.我们有一堆私有的 Java 依赖发布到 Artifactory。 We are using Gradle to build our project.我们正在使用 Gradle 来构建我们的项目。

The issue is that VSCode Language Support for Java does not recognize the dependencies / imports in build.gradle or in our Java source code.问题是 VSCode Language Support for Java 无法识别build.gradle或我们的 Java 源代码中的依赖项/导入。

Note: All the snippets below were copy-pasted exactly from the source code / VS Code messages, with the exception of the company domain being replaced with mycompany.com and the Artifactory credentials redacted.注意:下面的所有片段都是从源代码/VS Code 消息中完全复制粘贴的,除了公司域被替换为 mycompany.com 并且 Artifactory 凭据被编辑。

The errors I'm seeing in the Java source code are:我在 Java 源代码中看到的错误是:

The import com.mycompany.observability cannot be resolved
MycompanyLogger cannot be resolved

Here is the Java file with the errors:这是带有错误的 Java 文件:

package com.mycompany.sample;

import com.mycompany.observability.logging.MycompanyLogger;

public class SampleApplication {

  public static void main(String[] args) {
    MycompanyLogger.setServiceName("SERVICE_NAME");
  }
}

And the errors in build.gradle :以及build.gradle的错误:

Unresolved dependency: com.mycompany:core-observability-spring:0.0.37

Here is the build.gradle file:这是build.gradle文件:

plugins {
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'java-library'
    id 'application'
    id 'checkstyle'
    id 'pmd'
    id 'jacoco'
    id 'com.jfrog.artifactory' version "4.17.2"
    id "org.sonarqube" version "3.0"
}

group = 'com.mycompany'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

repositories {
    mavenLocal()
    mavenCentral()
}

jar.archiveBaseName = "sample-service"
mainClassName = 'com.mycompany.sample.SampleApplication'

apply plugin: 'com.jfrog.artifactory'
apply plugin: "java"

artifactory {
    contextUrl = "https://artifactory.mycompany.com/artifactory/"
    resolve {
        repository {
            repoKey = 'gradle-release'
            username = project.findProperty('artifactory_user')
            password = project.findProperty('artifactory_key')
            maven = true
        }
    }
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-aop')
    implementation('org.codehaus.janino:commons-compiler:3.1.0') // Conditional expressions in logback
    implementation('org.codehaus.janino:janino:3.1.0') // Conditional expressions in logback
    implementation("ch.qos.logback:logback-classic:1.2.3")
    implementation('net.logstash.logback:logstash-logback-encoder:6.3')
    implementation('com.datadoghq:dd-java-agent:0.51.0')

    implementation('com.mycompany:core-observability-spring:0.0.37')
}

checkstyle {
    toolVersion "8.29"
    configFile = file("${rootDir}/config/checkstyle/google_checks.xml")
}

jar {
    enabled = true
}

pmd {
    ignoreFailures = false
    ruleSetFiles = files("config/pmd/mycompany_custom_ruleset.xml")
    ruleSets = []
    sourceSets = []
}

and here are the relevant files in my ~/.gradle/ directory:这是我的~/.gradle/目录中的相关文件:

➜  .gradle cat gradle.properties
artifactory_user=<redacted>
artifactory_key=<redacted>
➜  .gradle cat init.gradle.kts  
settingsEvaluated {
    pluginManagement {
        val artifactory_user: String? by settings
        val artifactory_password: String? by settings
        val artifactory_key: String? by settings

        repositories {
            maven(url = "https://artifactory.mycompany.com/artifactory/gradle-release") {
                credentials {
                    username = artifactory_user
                    password = artifactory_password ?: artifactory_key
                }
            }
            gradlePluginPortal()
        }
    }
}

Lastly, here are my .vscode config files for this project:最后,这里是我这个项目的.vscode配置文件:

➜  .vscode git:(vscode-settings) cat settings.json 
{
    "[java]": {
        "editor.formatOnSave": false,
    },
    "editor.formatOnSave": true,
    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,
    "files.trimTrailingWhitespace": true,
    "terminal.integrated.scrollback": 10000,
    "java.configuration.updateBuildConfiguration": "automatic",
    "java.import.gradle.enabled": true,
    "java.import.gradle.wrapper.enabled": true,
    "java.import.maven.enabled": false,
}
➜  .vscode git:(vscode-settings) cat extensions.json 
{
    "recommendations": [
        "gabrielbb.vscode-lombok",
        "pivotal.vscode-spring-boot",
        "richardwillis.vscode-gradle-extension-pack",
        "vscjava.vscode-java-pack"
    ]
}

Everything works fine when compiling the app using ./gradlew build :使用./gradlew build编译应用程序时一切正常:

➜  sample-service git:(master) ✗ ./gradlew build

BUILD SUCCESSFUL in 5s
11 actionable tasks: 8 executed, 3 up-to-date

Any ideas on what should be my next steps in debugging this?关于调试这个的下一步应该是什么的任何想法?

I had the same issue and what helped me was running我有同样的问题,帮助我的是跑步

Java: Clean Java Language Server Workspace from Command Palette . Java: Clean Java Language Server WorkspaceCommand Palette Java: Clean Java Language Server Workspace

在此处输入图片说明

It seems like Java Language Server is holding to old version.似乎 Java Language Server 保留旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM