Rainy
Rainy

Reputation: 1

Gradle kotlin in Gradle5.2 Unresolved reference: dependtest

A multi module project with Kotlin source code, which used to work, stops working after upgrading to Gradle 5.2, because the Kotlin classes from the compile project('depend-test') dependency are not found.

  1. Attempted to change plugin version
  2. already viewed https://github.com/gradle/gradle/issues/8980

i defind Test class in project('depend-test')

object Test {
    const val test = "123"
}

i want to use Test class in project('test-test')

package com.example.test.controller

import com.example.dependtest.Test
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping
class TestController {

    private val log = LoggerFactory.getLogger(TestController::class.java)

    @GetMapping(value = ["/test"])
    fun test() {
        log.info(Test.test)
    }
}

when i want to build project('test-test') to jar where i used gradle bootJar。 I get this error:


> Task :test-test:compileKotlin FAILED
e: /Users/houshuai/Documents/dev/demo/test/test-test/src/main/kotlin/com/example/test/controller/TestController.kt: (3, 20): Unresolved reference: dependtest
e: /Users/houshuai/Documents/dev/demo/test/test-test/src/main/kotlin/com/example/test/controller/TestController.kt: (22, 18): Unresolved reference: Test

Expected Behavior The Kotlin classes in the compile project('depend-test') dependency should be found.

Current Behavior The Kotlin classes in the compile project('depend-test') dependency are not found:

Upvotes: 0

Views: 2776

Answers (2)

iglen_
iglen_

Reputation: 311

Try adding this to your build.gradle file

bootJar {
  enabled = false
}

jar {
  enabled = true
}

Just in case someone else comes across this problem.

Upvotes: 1

Rainy
Rainy

Reputation: 1

I created two modules, test-test and depend-test. The depend-test project is test-test 's dependency. I tried to call the parameters of depend-test, but it failed to compile and package.

Env

  • gradle-5.2.1
  • Kotlin 1.3.31
  • Springboot 2.1.4
  • java 1.8

step one

  • Edit settings.gradle
rootProject.name = 'demo'
include ":depend-test"
include ":test-test"
project(":depend-test").projectDir = file("depend/depend-test")
project(":test-test").projectDir = file("test/test-test")
  • I used the 1.3.31 version of the kotlin plug-in. The build.gradle file reads as follows
buildscript {
    ext {
        kotlinVersion = '1.3.31'
    }
    repositories {
        mavenCentral()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
    }
}


plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'org.jetbrains.kotlin.jvm' version '1.2.71'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}


allprojects {
    apply plugin: 'idea'
    apply plugin: 'kotlin'

    repositories {
        mavenCentral()
    }
}

subprojects {
    apply plugin: 'kotlin'
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: "application"
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    repositories {
        mavenLocal()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven { url "https://plugins.gradle.org/m2/" }
        mavenCentral()
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url 'http://maven.springframework.org/release' }
        maven { url 'http://maven.springframework.org/milestone' }
    }

    version = '1.0'
    apply plugin: 'io.spring.dependency-management'

    group = 'com.mutil.test'
    sourceCompatibility = '1.8'

    compileKotlin {
        kotlinOptions {
            freeCompilerArgs = ['-Xjsr305=strict']
            jvmTarget = '1.8'
        }
    }

    compileTestKotlin {
        kotlinOptions {
            freeCompilerArgs = ['-Xjsr305=strict']
            jvmTarget = '1.8'
        }
    }

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
        implementation 'org.jetbrains.kotlin:kotlin-reflect'
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
}

dependencies {
    subprojects.forEach {
        archives(it)
    }
}

repositories {
    mavenCentral()
}

step two

build jar for test-test project ,I used two ways, but the results were the same.

  • terminal use cmd is ./gradlew :test-test:bootJar
  • user IDEA gradle tool

result

The class file written by kotlin in the submodule cannot be found.

I do not know if the lack of necessary plug-ins caused the failure to package properly.

Upvotes: 0

Related Questions