gradle 无法访问Kotlin构建脚本中的org.apache.hc.client5.http.utils.URLEncodedUtils

kcrjzv8t  于 2023-01-13  发布在  Kotlin
关注(0)|答案(1)|浏览(235)

既然添加了依赖项,为什么org.apache.hc未解析?

package com.blah

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory

// Error seen when import line is uncommented:
// 'Unresolved reference: hc'
// import org.apache.hc.client5.http.utils.URLEncodedUtils <--------

import org.w3c.dom.Element
import org.xml.sax.InputSource
import java.io.FileWriter
import java.io.StringReader
import java.net.URI
import java.util.*

val projectVersion: String by project
val liquibaseVersion: String = "4.13.0"
val liquibaseGradlePluginVersion: String = "2.0.4"

plugins {
    java
    id("org.springframework.boot") apply false
    id("org.liquibase.gradle") apply true
}

dependencies {
    implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
    liquibaseRuntime(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

    implementation("org.postgresql:postgresql")
    implementation("org.liquibase:liquibase-core:$liquibaseVersion")
    implementation("org.liquibase:liquibase-gradle-plugin:$liquibaseGradlePluginVersion")
    implementation("org.hibernate:hibernate-core")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("com.fasterxml.jackson.core:jackson-core")
    implementation("com.fasterxml.jackson.core:jackson-databind")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")

    // No errors when building the Gradle model during import into IDE
    // https://mvnrepository.com/artifact/org.apache.httpcomponents.core5/httpcore5
    implementation("org.apache.httpcomponents.core5:httpcore5:5.2.1")

    liquibaseRuntime("org.liquibase:liquibase-core:$liquibaseVersion")
    liquibaseRuntime("org.postgresql:postgresql")
    liquibaseRuntime("org.springframework.boot:spring-boot-starter-data-jpa")
    liquibaseRuntime(sourceSets.getByName("main").output)
}
yzuktlbb

yzuktlbb1#

dependencies {}块中声明的依赖项被添加到项目的类路径中,而不是构建脚本的类路径中。
如果要在Gradle构建脚本中使用依赖关系,则有两个选项
1.在build.gradle(.kts)中使用buildscript {}

// build.gradle.kts

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
     classpath("org.apache.httpcomponents.core5:httpcore5:5.2.1")
  }
}

plugins {
  java
  id("org.springframework.boot") apply false
  id("org.liquibase.gradle") apply true
}

dependencies {
  // project dependencies...
}

1.在included build或buildSrc中定义依赖项

// ./buildSrc/build.gradle.kts

plugins {
  `kotlin-dsl` // a Kotlin or Java plugin is required, so that 'implementation' is available
}

dependencies {
  // dependencies defined here will be available in all of the project's build scripts
  implementation("org.apache.httpcomponents.core5:httpcore5:5.2.1")
}

相关问题