How to pass a context to a custom defined method in groovy?


(Elad) #1
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"
}

what context should I pass to my setSystemProperties() to fix this error?

> No such property: jvmArgs for class: org.gradle.api.internal.project.DefaultProject_Decorated

or
> Could not find method classpath() for arguments [file collection] on root project 'RoutingRegression'.

the code worked fine when it was all in the the task body:

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

(Stefan Oehme) #2

If you want to stay as close as possible to the syntax before, use a closure, not a method:

def addSystemProperties = {
  //same logic as if you were inside the task block
}

task foo() {
  configure addSystemProperties
}