Search code examples
kotlingradledependenciesgradle-kotlin-dsl

Cannot access org.apache.hc.client5.http.utils.URLEncodedUtils in kotlin build script


Why is org.apache.hc unresolved considering the dependency has been added?

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)
}

Solution

  • Dependencies declared in the dependencies {} block are added to the classpath of the project, not the classpath of the buildscript.

    If you want to use dependencies in a Gradle build script, then there are two options

    1. Use the buildscript {} block in build.gradle(.kts)

      // 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...
      }
      
    2. Define dependencies in an included build, or 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")
      }