简体   繁体   English

Gradle构建因Kotlin,Scala和Java而失败

[英]Gradle build failed with Kotlin, Scala and Java

I have a project written in Java, Scala and Kotlin. 我有一个用Java,Scala和Kotlin编写的项目。

But when I build this project in Gradle, it error with message: 但是,当我在Gradle中构建此项目时,出现错误消息:

11:19:51: Executing task 'build'...

:compileKotlin
Using Kotlin incremental compilation
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (3, 19):             
Unresolved reference: Flag
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (10, 18):     
Unresolved reference: GroupMessageResponder
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (13, 2): 
'doPost' overrides nothing
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (35, 23): 
Unresolved reference: Flag
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (38, 2): 
'getHelpMessage' overrides nothing
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (40, 2): 
'getKeyWordRegex' overrides nothing
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (42, 2): 
'instance' overrides nothing
:compileKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --
debug option to get more log output.

BUILD FAILED in 1s
1 actionable task: 1 executed
Compilation error. See log for more details
11:19:53: Task execution finished 'build'.

Those errors are all about 'Unresolved reference', and those 'reference' are all written in Scala or Java. 这些错误全都与“未解决的参考”有关,而这些“参考”全都用Scala或Java编写。

But run this project in IDEA works fine . 但是在IDEA中运行此项目效果很好

I thought this was caused by Gradle compile Kotlin first, which should compiled in last, so those 'reference' could not be found. 我以为这是由Gradle首先编译Kotlin引起的,应该最后编译一次,所以找不到那些“引用”。

And this is my build.gradle : build.gradle 这是我的build.gradlebuild.gradle

UPDATE: 更新:

A part of output of gradle clean testClasses --info : gradle clean testClasses --info的输出的一部分:

...SKIP...
> Task :compileKotlin
Using Kotlin incremental compilation

e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (3, 19): 
Unresolved reference: Flag
e: F:\Code\Project\Avalon\src\main\kotlin\avalon\group\Execute.kt: (11, 18): 
Unresolved reference: GroupMessageResponder
...SKIP....

> Task :compileKotlin
[KOTLIN] deleting F:\Code\Project\Avalon\build\classes\kotlin\main on error
[KOTLIN] deleting F:\Code\Project\Avalon\build\classes\kotlin\main on error

:compileKotlin (Thread[Task worker Thread 3,5,main]) completed. Took 15.981 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option 
to get more log output.

BUILD FAILED in 24s
2 actionable tasks: 2 executed
Stopped 0 worker daemon(s).

Full output posted here . 完整的输出发布 在这里

The problem you have are the different languages. 您遇到的问题是不同的语言。

Some Kotlin and Java 一些Kotlin和Java

Let's assume you just have kotlin and java. 假设您只有kotlin和Java。 Than you can access from ( -> means A accesses classes of B) 比您可以访问的( ->表示A访问B的类)

kotlin -> java 
java -> kotlin

The reason you can do that, is because during the compilation ( compileKotlin ) Kotlin take care of this crossCompilation / jointCompilation / whatever-other-name between those two languages. 可以这样做的原因是,在编译( compileKotlin )时,Kotlin会照顾这两种语言之间的crossCompilation / jointCompilation / other -other-name


Kotlin and Scala (one direction) Kotlin和Scala(一个方向)

But if you have the case: 但是,如果您有这种情况:

kotlin -> scala

than you must consider the the following: when compileKotlin runs, it compiles all .java and .kt files to class files, but non of the .scala files (because it doesn't know anything about scala ). 则必须考虑以下内容:当compileKotlin运行时,它将所有.java.kt文件编译为class文件,但不编译为.scala文件(因为它对scala一无所知)。 When you now access scala classes from kotlin , than you must first compile those .scala classes, to ensure that the compileKotlin compilation process find those .class files for a successful compilation. 现在,当您从kotlin访问scala类时,必须首先编译那些.scala类,以确保compileKotlin编译过程找到那些.class文件以成功进行编译。

You can define that scala must run before kotlin with eg 您可以使用以下命令定义scala必须在kotlin之前运行

compileKotlin.dependsOn compileScala

Kotlin and Scala (both directions) Kotlin和Scala(双向)

So good so far, but the problem is, when you have 到目前为止还不错,但是问题是,当您有

kotlin -> scala
scala -> kotlin

Because this scenario is not working. 因为这种情况下不起作用。

You need to compile the scala classes first, to successfully compile the kotlin classes (see description above). 您需要先编译scala类,才能成功编译kotlin类(请参见上面的描述)。 But on the other hand, you need the kotlin classes to be compiled, to successfully compile the scala classes. 但是,另一方面,您需要编译kotlin类,才能成功编译scala类。 That's a problem. 那是个问题。

The solution would be, that one of the languages allows jointCompilation for all of the languages (but this will never happen). 解决的办法是,其中一种语言允许所有语言都使用jointCompilation (但这绝不会发生)。

So for two JVM languages, where both are not Java , you must decide, which direction of access is allowed. 因此,对于两种都不是Java JVM语言,您必须确定允许访问的方向。 But you can't allow both directions of access. 但是您不能同时允许两个方向的访问。

In such cases, it often helps, to put the logic into a java class, and access this java class instead of the eg scala class. 在这种情况下,将逻辑放入java类并访问该java类而不是例如scala类通常会有所帮助。

The problem is also the same for eg groovy , java and some other jvm language, like in the post https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/10 对于groovyjava和其他一些jvm语言,问题也相同,例如在https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/10中

The missing link to let Scala call Kotlin code would be to add the following in build.gradle 让Scala调用Kotlin代码的缺少链接是在build.gradle中添加以下内容

compileScala.classpath += files(compileKotlin.destinationDir)

Based on https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/3 基于https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM