Kotlin 1.2.10和Java 9在自动模块方面是否有相反的规则? [英] Do Kotlin 1.2.10 and Java 9 have opposite rules regarding automatic modules?

查看:154
本文介绍了Kotlin 1.2.10和Java 9在自动模块方面是否有相反的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Kotlin Gradle插件的Gradle项目.我想构建一个Java 9模块,所以我的目录结构如下所示:

I have a Gradle project using the Kotlin Gradle plugin. I want to build a Java 9 module, so my directory structure looks like this:

src/main/java/
    - module-info.java
src/main/kotlin/
    - Foo.kt
    - Bar.kt
build.gradle
...

我的build.gradle声明以下依赖项:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.10"
    compile "org.jetbrains.kotlin:kotlin-reflect:1.2.10"
    compile "org.junit.jupiter:junit-jupiter-api:5.0.2"
}

,我在Kotlin源码中使用了所有这些依赖项(Foo.ktBar.kt,...).

and I use all of these dependencies in my Kotlin source (Foo.kt, Bar.kt, ...).

如果我像这样写我的module-info.java,一切都可以正常工作:

Everything works hunky-dory if I write my module-info.java like so:

module my.module {
    requires kotlin.stdlib;
    exports my.module.pkg;
}

,如果我在compileJava任务期间使用这项技术.

and if I supply all my compile-time dependencies to javac during the compileJava task using this technique.

但是,如果在compileJava任务(用于编译module-info.java)期间为Java编译器打开-Xlint:all,则会收到以下警告:

However if I turn on -Xlint:all for the Java compiler during the compileJava task (to compile module-info.java), I get the following warnings:

/path/to/my.module/src/main/java/module-info.java:26: warning: requires directive for an automatic module
    requires kotlin.stdlib;
                   ^

所以这里有Java编译器,javac抱怨说kotlin.stdlib是自动模块,所以我不应该有requires子句.

So here we have the Java compiler, javac complaining that kotlin.stdlib is an automatic module so I shouldn't have a requires clause for it.

但是如果我删除requires子句以使javac高兴,它会使kotlinc甚至比javac生气(我得到一个错误而不是警告):

But if I delete the requires clause to make javac happy, it makes kotlinc even angrier than javac was (I get an error not a warning):

e: /path/to/my.module/src/main/java/module-info.java: The Kotlin standard library is not found in the module graph. Please ensure you have the 'requires kotlin.stdlib' clause in your module definition

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':my.module:compileKotlin'.

现在,我也可以通过编辑compileKotlin任务来解决此问题:

Now I can fix that, too, by editing my compileKotlin task thus:

compileKotlin {
    doFirst {
        kotlinOptions.freeCompilerArgs = ['-Xallow-kotlin-package']
    }
}

但这只会导致在compileKotlin任务期间出现更多错误,所有错误看起来都像这样:

But this only leads to MORE errors during the compileKotlin task, all looking like this one:

e: /path/to/my.module/src/main/kotlin/Foo.kt: (27, 30): Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on

然后如果我尝试通过将"-Xmodule-path=${classpath.asPath}"添加到freeCompilerArgs并将classpath设置为空来强制compileKotlin采取模块路径而不是类路径,则Kotlin编译器根本找不到任何内容,最终导致成千上万个无法解决的参考错误!

And then if I try to force compileKotlin to take a module path rather than a classpath by adding "-Xmodule-path=${classpath.asPath}" to freeCompilerArgs and setting classpath to be empty, the Kotlin compiler can't find anything at all, and I end up with zillions of unresolved reference errors!

当Java编译器相反时,为什么Kotlin编译器告诉我必须拥有requires kotlin.stdlib;?如何使Kotlin和Java一起工作以生产Java 9模块?

Why is the Kotlin compiler telling me I have to have requires kotlin.stdlib; when the Java compiler says the opposite? How can I get Kotlin and Java to work together to produce a Java 9 module?

推荐答案

如果要在Kotlin中编写Java 9模块,则必须在module-info.java中声明requires kotlin.stdlib才能满足已编译Kotlin的运行时依赖性代码,以及对标准库API的显式依赖.

If you're authoring Java 9 module in Kotlin, you have to declare requires kotlin.stdlib in your module-info.java in order to satisfy runtime dependencies of the compiled Kotlin code in addition to the explicit dependencies on the standard library API.

javac警告您有关启用lint时要求使用自动模块的警告,因为自动模块具有某些潜在的

javac warns you about requiring an automatic module when lint is enabled, because automatic modules have some potential drawbacks compared to normal modules. Until the standard library is compiled as a normal module, you have to deal with this warning.

-Xallow-kotlin-package编译器标志允许您省略require kotlin.stdlib,因为它仅在编译标准库本身时使用.显然,如果您指定此标志并忽略该要求,则将无法使用标准库中的任何API,因此,这实际上不是您的选择.

-Xallow-kotlin-package compiler flag allows you to omit require kotlin.stdlib, because it is intended to be used only when the standard library itself is compiled. Obviously, if you specify this flag and omit that requirement, you won't be able to use any API from the standard library, so this is not really an option for you.

这篇关于Kotlin 1.2.10和Java 9在自动模块方面是否有相反的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆