简体   繁体   中英

"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. We are using Gradle to build our project.

The issue is that VSCode Language Support for Java does not recognize the dependencies / imports in build.gradle or in our Java source code.

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.

The errors I'm seeing in the Java source code are:

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

Here is the Java file with the errors:

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 :

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

Here is the build.gradle file:

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 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 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 :

➜  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 .

在此处输入图片说明

It seems like Java Language Server is holding to old version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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