java 使用 Kotlin 配置 Gradle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33738304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:03:41  来源:igfitidea点击:

Configuring Gradle with Kotlin

javaintellij-ideakotlin

提问by Filip Zymek

I am trying to use kotlin with gradle, but I am unable to succesfully create a project with Intellij Idea 15.

我正在尝试将 kotlin 与 gradle 一起使用,但我无法使用 Intellij Idea 15 成功创建一个项目。

I've create simple project with two modules hello-javaand hello-kotlin.

我创建了带有两个模块的简单项目hello-javahello-kotlin.

hello-javais plain java project and it is compiling and running perfectly fine.

hello-java是普通的 java 项目,它编译和运行得非常好。

hello-kotlinis simple kotin module, with just one *.kt file and build.gradlefile.
Here are the sources:

hello-kotlin是一个简单的 kotin 模块,只有一个 *.kt 文件和build.gradle文件。
以下是来源:

build.gradle

构建.gradle

group 'pl.fzymek.kotlin'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.10.4"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'HelloKotlinKt'

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.10.4"
}

HelloKotlin.kt

HelloKotlin.kt

fun main(args: Array<String>) {
    println("Hello, Kotlin!")
}

main module settings.gradle

主模块 settings.gradle

include 'hello-java'
include 'hello-kotlin'

When running gradlew clean buildall projects are compiled successfully, but when running java -jar hello-kotlin-1.0-SNAPSHOT.jarI get following error:

运行时gradlew clean build所有项目都编译成功,但运行时出现java -jar hello-kotlin-1.0-SNAPSHOT.jar以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at HelloKotlinKt.main(HelloKotlin.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

What is more, Intellij seems not to recognize src/main/kotlindirectory as source directory (it's not marked in blue) and I am not able to use auto-complete feature when editing HelloKotlin.kt file.

更重要的是,Intellij 似乎无法将src/main/kotlin目录识别为源目录(未标记为蓝色),并且在编辑 HelloKotlin.kt 文件时我无法使用自动完成功能。

Here's my project structure in Intellij project window

这是我在 Intellij 项目窗口中的项目结构

enter image description here

在此处输入图片说明

I've tried using Intellij option to configure modules with Kotlin(Tools->Kotlin->Configure project with Kotlin), but it gives me error that "All modules with kotlin files are configured"

我已经尝试使用 Intellij 选项来配置带有 Kotlin( Tools->Kotlin->Configure project with Kotlin) 的模块,但它给我的错误是“所有带有 kotlin 文件的模块都已配置”

Help me stackoverflow, you are my only help.

帮助我stackoverflow,你是我唯一的帮助。

采纳答案by aga

When running gradlew clean build all projects are compiled successfully, but when running java -jar hello-kotlin-1.0-SNAPSHOT.jar I get following error...

运行 gradlew clean build 时,所有项目都编译成功,但是在运行 java -jar hello-kotlin-1.0-SNAPSHOT.jar 时出现以下错误...

jar {
    manifest {
        attributes 'Main-Class': 'HelloKotlinKt'
    }

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

What is more, Intellij seems not to recognize src/main/kotlin directory as source directory (it's not marked in blue)...

更重要的是,Intellij 似乎没有将 src/main/kotlin 目录识别为源目录(它没有标记为蓝色)......

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

Documentation and useful resources can be found hereand there.

文档和有用的资源可以在这里那里找到。

回答by tddmonkey

Why doesn't my application run?

为什么我的应用程序不运行?

The Jar you're creating doesn't include the kotlin runtime as Gradle will only build a Jar with yourclass files in it. I see you're using the applicationplugin so either doing $ gradle runor creating a distribution and executing through the provided shell script should work ok. If you want to ship kotlin with your Jar you'll need to create a fat jar.

您正在创建的 Jar 不包含 kotlin 运行时,因为 Gradle 只会构建一个包含您的类文件的 Jar 。我看到您正在使用该application插件,因此无论是做$ gradle run还是创建分发并通过提供的 shell 脚本执行都应该可以正常工作。如果你想用你的 Jar 运送 kotlin,你需要创建一个胖 jar。

Why doesn't IDEA recognise the source directory?

为什么IDEA不识别源目录?

I suspect this is down to the fact you haven't applied the ideaplugin in your build file. I haven't done any work with Kotlin, but with other languages this is required to set up the workspace correctly.

我怀疑这是因为您没有idea在构建文件中应用该插件。我没有使用 Kotlin 做过任何工作,但是对于其他语言,这是正确设置工作区所必需的。

回答by Kirill Bitkov

I just do that documentation says, and all work fine. "Kotlin sources can be mixed with Java sources in the same folder, or in different folders. The default convention is using different folders:

我只是按照文档说的那样做,一切正常。“Kotlin 源代码可以与 Java 源代码混合在同一文件夹中,也可以在不同文件夹中。默认约定是使用不同的文件夹:

project
    - src
        - main (root)
            - kotlin
            - java

The corresponding sourceSets property should be updated if not using the default convention:

如果不使用默认约定,则应更新相应的 sourceSets 属性:

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
    main.java.srcDirs += 'src/main/myJava'
}

Hope it helps for you.

希望对你有帮助。