问题描述
我对Gradle(和Java 9,说实话)相当新,我正在尝试使用Gradle构建一个简单的库项目,它是Java 9和Kotlin的混合体。更详细地说,Java中有一个接口,Kotlin中有一个实现;我会在Kotlin做一切,但 modules-info.java
无论如何都是java,所以我决定这样做。
I'm fairly new to Gradle (and Java 9, to be honest), and I'm trying to use Gradle to build a simple library project that is a mix of Java 9 and Kotlin. More in detail, there is an interface in Java and an implementation in Kotlin; I'd do everything in Kotlin, but the modules-info.java
is java anyway, so I decided to do things this way.
我在IntelliJ Idea上构建,外部定义了1.2.0 kotlin插件和gradle 4.3.1。
I'm building on IntelliJ Idea, with the 1.2.0 kotlin plugin and gradle 4.3.1 defined externally.
文件系统架构是:
+ src
+ main
+ java
+ some.package
- Roundabout.java [an interface]
- module-info.java
+ kotlin
+ some.package.impl
- RoundaboutImpl.kt [implementing the interface]
module-info.java
是:
module some.package {
requires kotlin.stdlib;
exports some.package;
}
和 build.gradle
是:
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'some.package'
version '1.0-PRE_ALPHA'
apply plugin: 'java-library'
apply plugin: 'kotlin'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
sourceCompatibility = 9
compileJava {
dependsOn(':compileKotlin')
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: "$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
请注意,我必须在java编译任务中指定模块路径,否则编译将失败:
Notice that I had to specify a module path on the java compile task, or the compilation fails with:
无论如何,现在这个构建失败并出现此错误,我无法弄清楚如何解决它:
Anyway, now this build fails with this error, and I can't figure out how to solve it:
import some.package.impl.RoundaboutImpl;
import some.package.impl.RoundaboutImpl;
错误:找不到符号
返回新的RoundaboutImpl<>(queueSize,parallelism,worker,threadPool);
return new RoundaboutImpl<>(queueSize, parallelism, worker, threadPool);
我认为编译的Kotlin部分正常,然后java部分失败,因为它没有看到kotlin方面,可以这么说。
I think that the Kotlin part of the compilation is going ok, then the java part fails because it doesn't "see" the kotlin side, so to speak.
我想我应该以某种方式告诉它在类路径中加载已编译的kotlin类;但是(首先)我如何在gradle中做到这一点?和(第二)甚至可能?我认为你不能在Java 9中混合模块路径和类路径。
I think I should tell it somehow to to load the already compiled kotlin classes in the classpath; but (first) how do I do this in gradle? and (second) is it even possible? I think you can't mix module path and class path in Java 9.
我该如何解决这个问题?我认为这是一种非常常见的情况,因为每个java9风格的模块都是混合语言模块(因为 module-info.java
),所以我想我我在这里遗漏了一些非常基本的东西。
How can I solve this? I think it is a pretty common situation, as every java9-style module will be a mixed-language module (because of module-info.java
), so I think I'm missing something really basic here.
提前致谢!
推荐答案
解决了!将kotlin编译目录设置为与Java相同的目录就足够了:
Solved! It was sufficient to set the kotlin compilation dir to the same dir as Java:
compileKotlin.destinationDir = compileJava.destinationDir
现在可以使用同一棵树或不同树木中的来源;但有一个怪癖: jar
任务会生成一个包含所有条目重复的jar。我将继续努力解决这个问题。
It works now, both with the sources in the same tree or in different trees; but with a quirk: the jar
task produces a jar with all the entries duplicated. I'll work on fix this, next.
感谢所有人!
这篇关于使用Gradle构建Kotlin + Java 9项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!