Reducing duplication between two similar JavaExec tasks

I have the following two tasks for running FitNesse in different modes:

task fitnesseWiki(type: JavaExec, dependsOn: build) {
    classpath configurations.runtime
    main "fitnesse.FitNesse"
    args '-p', fitnessePort, '-d', fitnesseDir, '-r', fitnesseRoot, '-o'
    doFirst {
        systemProperties(["maven.classpath": mavenPathAsWikiPaths()])
    }
}
  task fitnesseTest(type: JavaExec, dependsOn: build) {
    classpath configurations.runtime
    main "fitnesse.FitNesse"
    args '-p', fitnessePort, '-d', fitnesseDir, '-r', fitnesseRoot, '-o', '-c', 'StatisticsTests?suite&format=text'
    doFirst {
        systemProperties(["maven.classpath": mavenPathAsWikiPaths()])
    }
}
  def mavenPathAsWikiPaths() {
    (configurations.archives.artifacts.files + configurations.runtime.asFileTree).collect {
        "!path ${it.path}"
    }.join("\n")
}

This works fine, but I’m a bit concerned about the duplication. As you can see, they only differ in the different “args”. Is there a simple way of reducing this duplication? I made some experiments with a custom task class inheriting from JavaExec, but had trouble getting the details right. And I don’t know if that’s the right way.

You could write a custom task that has a @TaskAction that uses project.javaexec { }.

You could use a common closure to configure the two similar FitNesse tasks.

e.g.,

[ fitnesseWiki, fitnesseTest ].each { task ->
   task.configure {
      // common configuration
   }
}

I’ve setup this exact same thing for someone else and they found the output of running FitNesse with ‘-c’ really unhelpful when failures occurred because you couldn’t view the output from Jenkins. This was with a ~2-3 year old copy of FitNesse, so that may have improved. We ended up switching to using the FitNesse JUnit runner and results were much better. You could navigate around and see stacktraces/failed pages/etc.

We also had problems with port collisions, so that’s another thing to be mindful of.

Thank you for the great suggestion, Sterling. I’ll have a look at the ant runner.

Or even ‘configure([fitnesseWiki, fitnesseTest]) { … }’.