如何将上下文传递给groovy中的自定义方法?

时间:2016-03-08 14:08:42

标签: intellij-idea gradle groovy

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"
}

1 个答案:

答案 0 :(得分:0)

查看您的任务,我知道为什么类路径设置失败。您错过了=。使用等号表示您设置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)方法

的测试类

可在此处找到jvmArgsJavaExec的引用: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