How to specify the entry point, Main-Class, for a FatJar using Gradle Kotlin DSL?

百般思念 提交于 2020-01-11 13:24:33

问题


How do I specify the Main-Class attribute in the ShadowJar once it's imported?

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> {
    baseName = "app"
    classifier = "inajar"
    version = "9"
    //main-class = "foobar"
}

Also, this build file might be out of date:

thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ gradle clean ShadowJar

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
4 actionable tasks: 4 executed
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ java -jar build/libs/app-9-inajar.jar 
no main manifest attribute, in build/libs/app-9-inajar.jar
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 

project:

https://github.com/THUFIR/HelloKotlinWorld


回答1:


build file:

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")
    }


    baseName = "app"
    classifier = "inajar"
    version = "9"
}

runs:

thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ gradle clean ShadowJar

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ java -jar build/libs/app-9-inajar.jar 
Hello world.
thufir@dur:~/NetBeansProjects/HelloKotlinWorld$ 

But I think it might be an out-of-date approach given the warning.



来源:https://stackoverflow.com/questions/53807387/how-to-specify-the-entry-point-main-class-for-a-fatjar-using-gradle-kotlin-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!