使用Kotlin配置Gradle [英] Configuring Gradle with Kotlin
问题描述
我正在尝试使用kotlin和gradle,但我无法使用Intellij Idea 15成功创建项目。
I am trying to use kotlin with gradle, but I am unable to succesfully create a project with Intellij Idea 15.
我用两个模块创建了简单的项目 hello-java
和 hello-kotlin
。
I've create simple project with two modules hello-java
and hello-kotlin
.
hello-java
是普通的java项目,它正在编译并且运行得很好。
hello-java
is plain java project and it is compiling and running perfectly fine.
hello-kotlin
是简单的kotin模块,只有一个* .kt文件和 build.gradle
file。
以下是来源:
hello-kotlin
is simple kotin module, with just one *.kt file and build.gradle
file.
Here are the sources:
build.gradle
build.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!")
}
主模块settings.gradle
main module settings.gradle
include 'hello-java'
include 'hello-kotlin'
运行 gradlew clean build
所有项目都已成功编译,但在运行 java -jar时hello-kotlin-1.0-SNAPSHOT.jar
我收到以下错误:
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:
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
更重要的是,Intellij似乎没有认识到ize src / main / kotlin
目录作为源目录(它没有标记为蓝色),编辑HelloKotlin.kt文件时无法使用自动完成功能。
What is more, Intellij seems not to recognize src/main/kotlin
directory 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项目窗口中的项目结构
Here's my project structure in Intellij project window
我尝试使用Intellij选项配置Kotlin模块(工具 - > Kotlin->使用Kotlin配置项目
),但是它给了我错误所有带有kotlin文件的模块都已配置
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"
帮助我stackoverflow,你是我唯一的帮助。
Help me stackoverflow, you are my only help.
推荐答案
运行gradlew clean build时,所有项目都编译成功,但运行java -jar时你好-kotlin-1.0-SNAPSHOT.jar我收到以下错误...
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...
jar {
manifest {
attributes 'Main-Class': 'HelloKotlinKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
什么更多的是,Intellij似乎没有将src / main / kotlin目录识别为源目录(它没有标记为蓝色)......
What is more, Intellij seems not to recognize src/main/kotlin directory as source directory (it's not marked in blue)...
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
可以在这里找到文档和有用的资源和那里。
这篇关于使用Kotlin配置Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!