gradle - 如何使用Gradle Kotlin DSL为FatJar指定入口点Main-Class?

标签 gradle kotlin uberjar gradle-kotlin-dsl shadowjar

一旦成为attribute,如何在Main-Class中指定ShadowJar 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"
}

另外,此构建文件可能已过期:
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$ 

项目:

https://github.com/THUFIR/HelloKotlinWorld

最佳答案

建立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"
}

运行:
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$ 

但我认为,鉴于警告,这可能是一种过时的方法。

关于gradle - 如何使用Gradle Kotlin DSL为FatJar指定入口点Main-Class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807387/

相关文章:

java - Gradle:无法应用 'compile'

gradle - 使用命令行告诉 gradle 下载所有源 jar

java - Liquibase:如何在列标签中设置外键约束?

java - 添加Groovy脚本以通过Gradle Android运行

android - 有没有一种方法可以设置和合并而无需仅为 1 个值创建映射

Android Kotlin - 意外标记(使用 ';' 分隔同一行的表达式)

maven - 在多模块项目中使用 Maven 阴影插件 - NullPointerException

arrays - 如何将Short/Int写入1字节缓冲区

hadoop - Cascalog:启动uberjar并在hadoop上进行main

java - 使用可运行 jar 时未找到 persistence.xml