具有Kotlin源代码的多模块项目曾经可以正常工作,但由于未找到来自编译项目('depend-test')依赖项的Kotlin类,因此在升级到Gradle 5.2后便停止工作。
我在project('depend-test')中定义了Test
类
object Test {
const val test = "123"
}
我想在项目(“测试-测试”)中使用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)
}
}
当我要在使用gradle bootJar的jar上构建项目('test-test')时,出现此错误:
> 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
预期行为 应该找到编译项目(“ depend-test”)依赖项中的Kotlin类。
当前行为 找不到编译项目('depend-test')依赖项中的Kotlin类:
答案 0 :(得分:1)
尝试将其添加到您的build.gradle文件中
bootJar {
enabled = false
}
jar {
enabled = true
}
以防万一有人遇到这个问题。
答案 1 :(得分:0)
我创建了两个模块,测试测试和依赖测试。 depends-test项目是test-test的依赖项。 我尝试调用depends-test的参数,但是无法编译和打包。
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")
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()
}
为测试项目构建jar,我使用了两种方法,但是结果是相同的。
./gradlew :test-test:bootJar
找不到kotlin在子模块中编写的类文件。
我不知道缺少必要的插件是否会导致无法正确打包。