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?
As others have noted, in order for a jar file to be executable, the application's entry point must be set in the Main-Class attribute of the manifest file. If the dependency class files are not collocated, then they need to be set in the Class-Path entry of the manifest file.
Shadow is a plugin that packages an application into an executable fat jar file if the current file is outdated. Tutorials → Working with JAR files → Fat JAR files. The task shadowJar (e.g., running the command gradlew shadowJar or gradlew clean shadowJar ) creates the JAR file in the build/libs folder.
To add the buildSrc module to your project, all you need to do is to create a buildSrc folder in the root of your Android project. After you sync your Gradle files (using Sync Project with Gradle Files ), Android Studio will detect the folder and populate it with the standard build folders ( build/ and . gradle/ ).
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With