def setSystemProperties(project) {
    if (project.hasProperty('serverversion')) {
        args(serverversion)
    }
    if (project.hasProperty('input_flavor')) {
        systemProperties['input_flavor'] = input_flavor
        print "gradle input_flavor" + input_flavor
    }
    jvmArgs = ["-Xms1024m", "-Xmx1024m"]
    classpath sourceSets.main.runtimeClasspath
}



//warm up

task BL_generate_parallel_warmup(type: JavaExec) {
    setSystemProperties(project)
    dependsOn resources_cleaner_bl
    systemProperties['isDummyRun'] = 'true'
    main = "astar.BlParallelGenerator"
}

我应该将什么上下文传递给setSystemProperties()来解决此错误?
> No such property: jvmArgs for class: org.gradle.api.internal.project.DefaultProject_Decorated
要么> Could not find method classpath() for arguments [file collection] on root project 'RoutingRegression'.
当代码全部放在任务主体中时,代码可以正常工作:
//warm up

task BL_generate_parallel_warmup(type: JavaExec) {
      if (project.hasProperty('serverversion')) {
        args(serverversion)
    }
    if (project.hasProperty('input_flavor')) {
        systemProperties['input_flavor'] = input_flavor
        print "gradle input_flavor" + input_flavor
    }
    jvmArgs = ["-Xms1024m", "-Xmx1024m"]
    classpath sourceSets.main.runtimeClasspath
    dependsOn resources_cleaner_bl
    systemProperties['isDummyRun'] = 'true'
    main = "astar.BlParallelGenerator"
}

最佳答案

查看您的任务,我看到了为什么classpath设置失败的原因。您缺少=。使用等号表示您设置了JavaExec类实例的属性。如果没有等号,那么您正在告诉groovy调用方法。由于不存在setClasspath方法(类路径仅作为类的属性存在),因此该方法失败。

task runApp(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'example.so.35869599.Main'
    jvmArgs = ["-Xms1024m", "-Xmx1024m"]
    // arguments to pass to the application
    args 'blah!'
}

使用上面的代码,我可以运行包含public static void main(String... args)方法的测试类

在此处可以找到对jvmArgs中的JavaExec的引用:https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:jvmArgs

编辑:没注意到您正在尝试传递JavaExec对象。 JavaExec的实例作为arg传递给JavaExec配置闭包。我们可以通过这样的例子使这一点更加清楚。
// here if we specify the type and name of the object passed to the closure
// it becomes clearer where we can access the `jvmArgs` property.
task runApp(type: JavaExec) { JavaExec exec ->
    addArgsToJvm(exec)
    classpath = sourceSets.main.runtimeClasspath
    main = 'example.so.35869599.Main'
    // arguments to pass to the application
    args 'appArg1'
}

def addArgsToJvm(JavaExec exec) {
    println "############################## caller passed: ${exec.getClass().getCanonicalName()}"
    exec.jvmArgs = ["-Xms1024m", "-Xmx1024m"]
}

上面的示例将在命令行中显示此结果:
############################## caller passed: org.gradle.api.tasks.JavaExec_Decorated

10-05 19:03