Accepting arguments to a JavaExec task

I’m using Caliper to run performance benchmarks. I’m creating a JavaExec-based task to run the “com.google.caliper.Runner” class, but I need to pass in a benchmark class name (for example “org.example.parsers.ParserBenchmark”). I’m not sure of the best way to do this. My first attempt was:

task bench(type: JavaExec, dependsOn: compileTestJava) {

benchmarkClass = rootProject.ext.properties.get(“bench_class”)

if (benchmarkClass == null) throw new AssertionError(“Set the ‘bench_class’ property.”)

main = ‘com.google.caliper.Runner’

args = [benchmarkClass, …]

}

The problem is that the “if (benchmarkClass = null)” check happens at configure time. So if I’m just compiling the project, it will fail saying that “bench_class” isn’t set. I want to make it so that “bench_class” is only checked if the ‘bench’ task is running.

(Though even if I get that working, my overall approach still seems messy. Does anybody know of a cleaner way to do this?)

Hi, where do you define the bench_class in you build script. do move the check into the execution phase, you can move some of your logic into a doFirst block:

task bench(type: JavaExec, dependsOn: compileTestJava) {
     ...
   main = 'com.google.caliper.Runner'
            doFirst{
      benchmarkClass = rootProject.ext.properties.get("bench_class")
        if (benchmarkClass == null) throw new AssertionError("Set the 'bench_class' property.")
        args = [benchmarkClass, ...]
        }
}

To have a more declarative syntax here, you might write your own Caliper task, that extends the JavaExec task.

regards, René