在 Gradle 中将 Manifest jar 与 JavaExec 一起使用时未找到 Main class

Main class not found when using Manifest jar with JavaExec in Gradle

我有以下 gradle 来自 Jhipster framework. It is used to run Gatling tests and due to the command line limit on windows I have tried using a manifest only jar approach. A detailed discussion regarding this can be found here Jhipster issue 的脚本 但是下面的脚本似乎正确地生成了 Manifest jar,但是 JavaExec 无法找到主要的 class,关于这个的一些指针真的很有帮助。下面还有控制台错误日志

我已经在 gradle 论坛上发布了这个 here,但到目前为止还没有成功

应用插件:'scala'

sourceSets {
    test {
        scala {
            srcDirs = ['src/test/gatling/simulations']
            output.classesDir = 'target/test-classes'
        }
    }
}
task manifestJar(dependsOn:'compileTestScala',type: Jar) {
    dependsOn configurations.runtime
    classifier 'pathing'
    doFirst {
        manifest {
            // uri is just needed for Windows-compatibility
            attributes 'Class-Path': configurations.runtime.files.collect{ project.uri(it) }.join(' ')
        }
    }
}
task gatlingRun(dependsOn:'manifestJar', type: JavaExec) {
    //dependsOn configurations.runtime
    group = "gatling"

    standardInput = System.in

    final def sourceSet = sourceSets.test
    File configFile = file('src/test/gatling/conf/gatling.conf')

    def String gatlingDataFolder = "$project.rootDir.absolutePath/src/test/gatling/data"
    def String gatlingReportsFolder = "$project.buildDir.absolutePath/reports/gatling"
    def String gatlingBodiesFolder = "$project.rootDir.absolutePath/src/test/gatling/bodies"
    def String gatlingSimulationsFolder = "$project.rootDir.absolutePath/src/test/gatling/simulations"

    //classpath sourceSet.output + sourceSet.runtimeClasspath + files("src/test/gatling/conf")
    classpath sourceSet.output + files(manifestJar.archivePath) + files("src/test/gatling/conf")
    //classpath = files(pathingJar.archivePath)
    main = "io.gatling.app.Gatling"

    environment GATLING_HOME:''

    args '-df', gatlingDataFolder
    args '-rf', gatlingReportsFolder
    args '-bdf', gatlingBodiesFolder
    args "-sf", gatlingSimulationsFolder
    args "-rd", ""

}

控制台日志:

:compileTestJava UP-TO-DATE
:compileTestJava (Thread[main,5,main]) completed. Took 0.973 secs.
:compileTestScala (Thread[main,5,main]) started.
:compileTestScala
Skipping task ':compileTestScala' as it is up-to-date (took 0.325 secs).
:compileTestScala UP-TO-DATE
:compileTestScala (Thread[main,5,main]) completed. Took 0.344 secs.
:manifestJar (Thread[main,5,main]) started.
:manifestJar
Skipping task ':manifestJar' as it is up-to-date (took 0.012 secs).
:manifestJar UP-TO-DATE
:manifestJar (Thread[main,5,main]) completed. Took 0.038 secs.
:processTestResources (Thread[main,5,main]) started.
:processTestResources
Skipping task ':processTestResources' as it is up-to-date (took 0.013 secs).
:processTestResources UP-TO-DATE
:processTestResources (Thread[main,5,main]) completed. Took 0.041 secs.
:testClasses (Thread[main,5,main]) started.
:testClasses
Skipping task ':testClasses' as it has no actions.
:testClasses UP-TO-DATE
:testClasses (Thread[main,5,main]) completed. Took 0.019 secs.
:gatlingRun (Thread[main,5,main]) started.
:gatlingRun
Executing task ':gatlingRun' (up-to-date check took 0.001 secs) due to:
  Task has not declared any outputs.
Starting process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''. Working directory: D:\Projects\jh5 Command: C:\Program Files\Java\jdk1.8.
0_45\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=SG -Duser.language=en -Duser.variant -cp D:\Projects\jh5\target\test-classes;D:\Projects
\jh5\build\resources\test;D:\Projects\jh5\build\libs\jhipster-pathing.jar;D:\Projects\jh5\src\test\gatling\conf io.gatling.app.Gatling -df D:\Projects
\jh5/src/test/gatling/data -rf D:\Projects\jh5\build/reports/gatling -bdf D:\Projects\jh5/src/test/gatling/bodies -sf D:\Projects\jh5/src/test/gatling
/simulations -rd
Successfully started process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''
Error: Could not find or load main class io.gatling.app.Gatling
:gatlingRun FAILED
:gatlingRun (Thread[main,5,main]) completed. Took 0.166 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':gatlingRun'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 28.134 secs
Stopped 0 compiler daemon(s).

编辑: 您的清单 jar 包含错误的类路径。尝试使用 testCompile 个文件:

task manifestJar(dependsOn:'compileTestScala',type: Jar) {
dependsOn configurations.testCompile
archiveName 'gatlingBooter.jar'
doFirst {
    manifest {
        // uri is just needed for Windows-compatibility
        attributes 'Class-Path': configurations.testCompile.files.collect{ project.uri(it) }.join(' ')
       }
   }
}

旧:

如果清单 jar 只能工作 java -jar @Deepu 也许我们应该尝试以下操作:

task runJar(dependsOn:jar) << {
  javaexec { main="-jar"; args jar.archivePath } 
}

至少看起来不错。周末会尝试测试一下。

参考文献: