specify Kotlin class as the Main-Class entry point

2019-06-05 09:13发布

How do I explicitly tell shadowJar to use a Kotlin file as the entry point for the resulting uberJar? (Or, fat JAR as you prefer.)

clumsily editing META-INF/MANIFEST.MF and the replacing it in the zip file (used the GUI to put it back):

thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ ll
total 900
drwxrwxr-x 2 thufir thufir   4096 Nov  5 03:42 ./
drwxrwxr-x 8 thufir thufir   4096 Nov  5 03:42 ../
-rw-rw-r-- 1 thufir thufir 903184 Nov  5 03:42 kotlinAdder.jar
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ jar xf kotlinAdder.jar 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ ll
total 928
drwxrwxr-x  7 thufir thufir   4096 Nov  5 03:42 ./
drwxrwxr-x  8 thufir thufir   4096 Nov  5 03:42 ../
drwxrwxr-x  2 thufir thufir   4096 Nov  5 03:42 demo/
drwxrwxr-x 17 thufir thufir  12288 Nov  5 03:42 kotlin/
-rw-rw-r--  1 thufir thufir 903184 Nov  5 03:42 kotlinAdder.jar
drwxrwxr-x  3 thufir thufir   4096 Nov  5 03:42 META-INF/
drwxrwxr-x  3 thufir thufir   4096 Nov  5 03:42 net/
drwxrwxr-x  4 thufir thufir   4096 Nov  5 03:42 org/
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ nano META-INF/MANIFEST.MF 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Main-Class: net.bounceme.dur.kotlin.App

thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ nano META-INF/MANIFEST.MF 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Main-Class: demo.MainKt
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ java -jar kotlinAdder.jar 
Hello, world!
thufir@dur:~/NetBeansProjects/kotlin/build/libs$ 

Success of sorts. It ran the Kotlin class as intended.

project:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── net
    │   │       └── bounceme
    │   │           └── dur
    │   │               └── kotlin
    │   │                   └── App.java
    │   └── kotlin
    │       └── demo
    │           ├── example.kt
    │           └── main.kt
    └── test
        └── java

13 directories, 9 files

build file:

plugins {
    id 'com.gradle.build-scan' version '1.8' 
    //   id 'java'
    id 'application'
    id "org.jetbrains.kotlin.jvm" version "1.1.51"
    id 'com.github.johnrengelman.shadow' version '2.0.1'
}

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
    //publishAlways()
}

configurations {
    provided
}

shadowJar {
    baseName = 'kotlinAdder'
    classifier = null
    version = null
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

//mainClassName = 'net.bounceme.dur.kotlin.App'
mainClassName = 'demo.MainKt'

repositories {
    jcenter()
}

configurations {
    provided
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
    compile 'com.google.firebase:firebase-auth:11.4.2'

} 

How do I tell gradle to keep using shadowJar but to build the MANIFEST.MF as manually edited, to set Main-Class: demo.MainKt explicitly as the entry point.

Else, the Java code runs by default even when I've explicitly set the entry point as above in the build.gradle file.

0条回答
登录 后发表回答