gradle - 如何使用 Gradle Kotlin DSL 为 FatJar 指定入口点 Main-Class?
问题描述
如何在导入后指定Main-Class
属性?ShadowJar
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$
项目:
解决方案
构建文件:
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$
但我认为,鉴于警告,这可能是一种过时的方法。
推荐阅读
- javascript - 将 Web 链接添加到 FCM 推送通知 Web
- python - 使用 Pandas.Melt 重新排列 Pandas 数据框以一次获取多列?
- powershell - 在使用 NoProfile 参数运行 Powershell 时尝试删除别名不起作用
- git - 在 Windows 上找不到 git-upload-pack 命令
- python - 如何从 Heroku 获取 Postgresql 配置参数?
- sql - 我写了程序,只填了一栏,告诉我错误在哪里?
- windows - 如何使用 wGet 获取文件而不是 url
- laravel - 如何在 laravel eloquent 查询中使用数学函数?
- reactjs - 无法读取未定义的属性“问题”
- architecture - 在分布式架构中使用 DDD 聚合服务?