I\'ve written a simple kotlin source file in order to get started, and a gradle script file. But I can\'t figure out how to add the main func to the manifest, so that the ja
Add the plugin application
, then set the mainClassName
as
mainClassName = '[your_namespace].[your_arctifact]Kt'
For instance, suppose you have placed the following code in a file named main.kt
:
package net.mydomain.kotlinlearn
import kotlin
import java.util.ArrayList
fun main(args: Array<String>) {
println("Hello!")
}
your build.gradle
should be:
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = "net.mydomain.kotlinlearn.MainKt"
In fact Kotlin is building a class to encapsulate your main function named with the same name of your file - with Title Case.
I've found the workaround (thanks to MkYong website)
So i get with the following gradle script :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.1-2'
}
}
apply plugin: "kotlin"
repositories {
mavenCentral()
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.1-2'
}
jar {
manifest {
attributes 'Main-Class': 'com.loloof64.kotlin.exps.MultideclarationsKT'
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
For anyone using the Kotlin MP plugin here is your code
jvm{
jvmJar {
manifest{
attributes 'Main-Class':'Class path here'
}
}
}
None of the above solution worked for me while building Artifact.
IDE version IntelliJ IDEA 2019.1.1
.
To fix the issue, do the following
Steps
Step 1 - Create Artifact
Go to File -> Project Structure -> Artifacts
Click the +
-> JAR
-> From modules with dependencies
Main Class
Step 2 - Change MANIFEST Path
Change value of Directory for META-INF/MANIFEST.MF
to your project root.
For example , from /your/project/directory/src/main/kotlin
to /your/project/directory
OK
,then Press Apply
and OK
.Step 3 - Build Artifact
Build
-> Build Artifacts
-> [your-artifact-name]
-> Build
.The generated JAR
file can be found in the out/artifact/[your-artifact-name]
directory. (y)
If using Gradle
with the Kotlin
DSL
, then my duplicate question has an answer of:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
kotlin("jvm") version "1.2.51"
id("com.github.johnrengelman.shadow") version "2.0.4"
}
group = "xxx.yyy"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<ShadowJar> {
manifest.attributes.apply {
put("Implementation-Title", "Gradle Jar File Example")
//put("Implementation-Version" version)
put("Main-Class", "HelloKotlinWorld.App")
}
Which is, I think, the simplest solution. Oh, perhaps you're using just Kotlin
itself and not the DSL
.
In case you happen to be dumb, like me:
Don't create your IntelliJ run configuration as an Application. IntelliJ will assume it's Java & it will never work. Instead, use the "Kotlin" entry.