gradle脚本Kotlin构建文件:build.gradle.kts [英] gradle script Kotlin build file: build.gradle.kts
问题描述
thufir @ dur:〜 / github / gradleScriptKotlin $
thufir @ dur:〜/ github / gradleScriptKotlin $ gradle clean shadowJar; java -jar build / libs / gradleScriptKotlin.jar
>任务:compileKotlin
使用Kotlin增量编译
>任务:shadowJar
在任务':shadowJar'的配置中发现问题。通过TaskInputs和TaskOutputs方法注册无效输入和输出已被弃用,并计划在Gradle 5.0中删除。
- 没有为属性'mainClassName'指定值。
SimpleWorkResult类型已被弃用,并计划在Gradle 5.0中删除。请改用WorkResults.didWork()。
在1s中创建成功
4个可执行任务:4个执行
您好gradle脚本Kotlin世界!
thufir @ dur:〜/ github / gradleScriptKotlin $
为简洁起见,请参阅到项目本身只包含构建文件和一个kotlin脚本。
如何使用Gradle脚本构建可运行的ShadowJar Kotlin构建文件?
你的意思是fatjar吗?
如果是这样,你可以使用影子gradle插件:
id'com.github.johnrengelman.shadow'version' 2.0.2'
如果你想让jar可执行,你还需要添加 Main-class
来清单(下面的文件 Applicaion.kt中的main方法的例子
包含 test $
$ $ $
$ $ b $
$
$''Main-Class''''' test.ApplicationKt'
}
}
然后你可以运行 jar
命令: java -jar< your jar>
<下面我列举一个简单的例子。文件
build.gradle
: 插件{
id' com.github.johnrengelman.shadow'version'2.0.2'
idorg.jetbrains.kotlin.jvmversion1.2.21
}
repositories {
jcenter()
}
jar {
manifest {
attributes'Main-Class':'test.ApplicationKt'
}
dependencies {
compile(org.jetbrains.kotlin:kotlin-stdlib-jdk8)
compile(org.simpleframework:simple-xml:2.5 )
}
文件 test.Application.kt
$ b
包测试
导入org.simpleframework.xml.Element
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
import org.simpleframework.xml.core.Persister
$ b $ private val testXml =
< feed>
<条目>
< id> someid< / id>
<发布> somedate< / publishe d>
< / entry>
< entry>
< id> someid2< / id>
<发布> somedate2< / published>
< / entry>
< / feed>
.trimIndent()
@Root(name =feed,strict = false)
数据类Feed(
@field:ElementList =entry,inline = true)
var entriesList:List< Entry> ;? = null
)
@Root(name =entry,strict = true)$ (
@field:Element(name =id)
var id:String?= null,
@field:Element(name =b发布)
var published:String?= null
)
fun main(args:Array< String>){
println(testXml)
val serializer = Persister()
示例= serializer.read(Feed :: class.java,testXml)
println(示例)
}
运行命令: gradle shadowJar
然后尝试运行jar: java -jar build / libs / shadow_test-all.jar
更新2018-02-17
build.gradle.kts
文件版本:
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
插件{
id(org.jetbrains.kotlin.jvm)版本1.2.21
id(com.github.johnrengelman.shadow)版本2.0.2
}
知识库{
jcenter()
}
tasks.withType< Jar> {
manifest {
attributes(mapOf(
Main-Classtotest.ApplicationKt
))
}
}
dependencies {
compile(org.jetbrains.kotlin:kotlin-stdlib-jdk8)
compile(org.simpleframework:simple-xml:2.5)
}
Simplest possible Kotlin hello world for gradle script Kotlin:
thufir@dur:~/github/gradleScriptKotlin$
thufir@dur:~/github/gradleScriptKotlin$ gradle clean shadowJar;java -jar build/libs/gradleScriptKotlin.jar
> Task :compileKotlin
Using Kotlin incremental compilation
> Task :shadowJar
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
- No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
Hello gradle script Kotlin world!
thufir@dur:~/github/gradleScriptKotlin$
For the sake of brevity please refer to the project itself which only really consists of the build file and a kotlin script.
How to build a runnable ShadowJar with a gradle script Kotlin build file?
Do you mean fatjar? If so, you could use shadow gradle plugin:
id 'com.github.johnrengelman.shadow' version '2.0.2'
And if you want to make jar executable, you need also add Main-class
to manifest (below example for main method in file Applicaion.kt
with package test
):
jar {
manifest {
attributes 'Main-Class': 'test.ApplicationKt'
}
}
And with this you may run jar
with command: java -jar <your jar>
Below I have include simple example. File build.gradle
:
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.2'
id "org.jetbrains.kotlin.jvm" version "1.2.21"
}
repositories {
jcenter()
}
jar {
manifest {
attributes 'Main-Class': 'test.ApplicationKt'
}
}
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.simpleframework:simple-xml:2.5")
}
File test.Application.kt
:
package test
import org.simpleframework.xml.Element
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
import org.simpleframework.xml.core.Persister
private val testXml = """
<feed>
<entry>
<id> someid </id>
<published> somedate </published>
</entry>
<entry>
<id> someid2 </id>
<published> somedate2 </published>
</entry>
</feed>
""".trimIndent()
@Root(name = "feed", strict = false)
data class Feed(
@field:ElementList(name = "entry", inline = true)
var entriesList: List<Entry>? = null
)
@Root(name = "entry", strict = true)
data class Entry(
@field:Element(name = "id")
var id: String? = null,
@field:Element(name = "published")
var published: String? = null
)
fun main(args: Array<String>) {
println(testXml)
val serializer = Persister()
val example = serializer.read(Feed::class.java, testXml)
println(example)
}
Run command: gradle shadowJar
And after try to run jar: java -jar build/libs/shadow_test-all.jar
Update 2018-02-17
build.gradle.kts
version of file:
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.jetbrains.kotlin.jvm") version "1.2.21"
id("com.github.johnrengelman.shadow") version "2.0.2"
}
repositories {
jcenter()
}
tasks.withType<Jar> {
manifest {
attributes(mapOf(
"Main-Class" to "test.ApplicationKt"
))
}
}
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.simpleframework:simple-xml:2.5")
}
这篇关于gradle脚本Kotlin构建文件:build.gradle.kts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!