2

我正在使用 Micronaut 并使用它的 cli 创建一个 java-cli-app。它没有额外的代码,通过运行 main 方法运行良好。执行 ./gradlew build 时,它还会创建一个包含所有依赖项的 jar。

我还可以使用 GraalVM 19.1.1 和 19.2.0 构建本机映像,并且运行良好。

当我通过手动运行任务来构建本机映像时

./gradlew buildNativeExecutable

它也运行良好,

想做什么让它在创建 jar 时自动运行。

我已经尝试过的,

task taskX << {
    println 'taskX'
}
task taskY << {
    println 'taskY'
}
task taskZ << {
    println 'taskZ'
}
taskX.dependsOn taskY
taskZ.shouldRunAfter taskY

这不起作用(我知道这是旧的,我只是使用/尝试了 shouldRunAfter jar / assemble 的不同变体)

buildNativeExecutable.shouldRunAfter jar

或者

shadowJar.doLast {
    task buildNativeExecutable(type:Exec) {
        workingDir "${buildDir}/libs"

    commandLine "native-image", "-J-Drx.unsafe-disable=true", "-jar", "test-0.1-all.jar", "-H:FallbackThreshold=0", "-H:+ReportExceptionStackTraces", "-H:+PrintAnalysisCallTree", "-H:-AddAllCharsets", "-H:EnableURLProtocols=http,https", "--enable-all-security-services", "-H:-SpawnIsolates", "-H:+JNI", "--no-server", "-H:-UseServiceLoaderFeature", "-H:+StackTrace"

    }

}

或者

shadowJar {
    mergeServiceFiles()
    doLast { task ->
        buildNativeExecutable(task)
    }
}

或者

build.doLast {
    task buildNativeExecutable(type:Exec) {
        workingDir "${buildDir}/libs"

        commandLine "native-image", "-J-Drx.unsafe-disable=true", "-jar", "test-0.1-all.jar", "-H:FallbackThreshold=0", "-H:+ReportExceptionStackTraces", "-H:+PrintAnalysisCallTree", "-H:-AddAllCharsets", "-H:EnableURLProtocols=http,https", "--enable-all-security-services", "-H:-SpawnIsolates", "-H:+JNI", "--no-server", "-H:-UseServiceLoaderFeature", "-H:+StackTrace"
    }
}

我的 build.gradle 文件

plugins {
    id "net.ltgt.apt-eclipse" version "0.21"
    id "com.github.johnrengelman.shadow" version "5.0.0"
    id "application"
}



version "0.1"
group "test"

repositories {
    mavenCentral()
    maven { url "https://jcenter.bintray.com" }
}

configurations {
    // for dependencies that are needed for development only
    developmentOnly
    generateConfig
}

dependencies {
    annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    annotationProcessor "io.micronaut.configuration:micronaut-picocli"
    annotationProcessor "io.micronaut:micronaut-inject-java"
    annotationProcessor "io.micronaut:micronaut-validation"
    generateConfig 'info.picocli:picocli-codegen:4.0.2'
    implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
    implementation "io.micronaut:micronaut-runtime"
    implementation "info.picocli:picocli"
    implementation "io.micronaut.configuration:micronaut-picocli"
    implementation "io.micronaut:micronaut-inject"
    implementation "io.micronaut:micronaut-validation"
    runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
    testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    testAnnotationProcessor "io.micronaut:micronaut-inject-java"
    testImplementation "org.junit.jupiter:junit-jupiter-api"
    testImplementation "io.micronaut.test:micronaut-test-junit5"
    testImplementation "io.micronaut:micronaut-inject-java"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}

test.classpath += configurations.developmentOnly
mainClassName = "test.testCommand"

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
}
// use JUnit 5 platform
test {
    useJUnitPlatform()
}
tasks.withType(JavaCompile){
    options.encoding = "UTF-8"
    options.compilerArgs.add('-parameters')
}

task(generateGraalReflectionConfig, dependsOn: 'classes', type: JavaExec) {
    main = 'picocli.codegen.aot.graalvm.ReflectionConfigGenerator'
    classpath = configurations.generateConfig + sourceSets.main.runtimeClasspath
    def outputFile = "${buildDir}/resources/main/META-INF/native-image/${project.group}/${project.name}/reflect-config.json"
    args = ["--output=$outputFile", mainClassName]
}

assemble.dependsOn generateGraalReflectionConfig

task buildNativeExecutable(type:Exec) {
    workingDir "${buildDir}/libs"

    commandLine "${System.env.GRAALVM_HOME}/bin/native-image", "-J-Drx.unsafe-disable=true", "-jar", "test-0.1-all.jar", "-H:FallbackThreshold=0", "-H:+ReportExceptionStackTraces", "-H:+PrintAnalysisCallTree", "-H:-AddAllCharsets", "-H:EnableURLProtocols=http,https", "--enable-all-security-services", "-H:-SpawnIsolates", "-H:+JNI", "--no-server", "-H:-UseServiceLoaderFeature", "-H:+StackTrace"
//    commandLine "ls", "-l"
}
shadowJar {
    mergeServiceFiles()
}

run.classpath += configurations.developmentOnly
run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')

当我运行特定任务时,它工作正常!

$ ./gradlew buildNativeExecutable

> Task :buildNativeExecutable
[test-0.1-all:19292]    classlist:   4,886.05 ms
[test-0.1-all:19292]        (cap):   1,800.38 ms
[test-0.1-all:19292]        setup:   3,589.77 ms
[test-0.1-all:19292]   (typeflow):  14,079.22 ms
[test-0.1-all:19292]    (objects):  12,647.95 ms
[test-0.1-all:19292]   (features):     780.98 ms
[test-0.1-all:19292]     analysis:  28,336.62 ms
Printing call tree to /home/someuser/test/build/libs/reports/call_tree_test-0.1-all_20190821_183418.txt
Printing list of used classes to /home/someuser/test/build/libs/reports/used_classes_test-0.1-all_20190821_183421.txt
Printing list of used packages to /home/someuser/test/build/libs/reports/used_packages_test-0.1-all_20190821_183421.txt
[test-0.1-all:19292]     (clinit):     572.67 ms
[test-0.1-all:19292]     universe:   1,361.53 ms
[test-0.1-all:19292]      (parse):   2,219.23 ms
[test-0.1-all:19292]     (inline):   3,373.54 ms
[test-0.1-all:19292]    (compile):  43,182.08 ms
[test-0.1-all:19292]      compile:  50,397.77 ms
[test-0.1-all:19292]        image:   4,215.91 ms
[test-0.1-all:19292]        write:   2,583.69 ms
[test-0.1-all:19292]      [total]: 107,083.26 ms

BUILD SUCCESSFUL in 1m 50s
1 actionable task: 1 executed

$ cd /home/someuser/test/build/libs
$ ls -ltrh
-rw-rw-r-- 1 abcd abcd 1,8K Aug 21 15:42 test-0.1.jar
-rw-rw-r-- 1 abcd abcd 8,9M Aug 21 15:42 test-0.1-all.jar
-rwxrwxr-x 1 abcd abcd  36M Aug 21 15:44 test-0.1-all

但是我无法在创建 jar 后运行它:(

./gradlew build

BUILD SUCCESSFUL in 11s
13 actionable tasks: 11 executed, 2 up-to-date

我应该在上面的 build.gradle 中更改/添加什么以便它在 jar 创建后运行?

4

1 回答 1

0

你基本上buildNativeExecutable想追shadowJar。如果是这种情况,那么只需添加以下内容即可获得所需的结果:

buildNativeExecutable.dependsOn(shadowJar)

于 2019-09-18T14:19:04.617 回答