Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JavaExec multiple times in a single task using Gradle?

Tags:

I have a task that runs a simple JavaExec.

What I cant seem to get working is the ability to run the JavaExec multiple times while iterating a Filetree object (containing the files) each of while I want to pass into the main JavaExec class one by one. Unfortunately the compiler or code generation tool as it is doesnot accept a directory as an arg so I need to pass the file as an arg per loop. Here's what I have:

task generateClasses(type: JavaExec) {
   description = 'Generates Json Classes...'
   classpath configurations.all
   main = "org.apache.gora.compiler.Compiler"
   FileTree tree = fileTree(dir: 'src/main')
   tree.include '**/*.json'
       tree.each {File file ->
       println file
       args = [ "src/main/json/$file.name", "$buildDir/generated-src/src/main/java" ]
   }    

}

compileJava.source generateClasses.outputs.files, sourceSets.main.java

From the above it works and I get all files listed but the JavaExec is called just the once on the very last file read.

How do I address the above? Please help.

like image 294
user983022 Avatar asked Nov 20 '12 14:11

user983022


Video Answer


1 Answers

How about using the project.javaexec method? See the API Documentation or the DSL ref.

task generateClasses {
  description = 'Generate Json Classes'
  fileTree(dir: 'src/main', include:'**/*.json').each { file ->
    doLast {
      javaexec {
        classpath configurations.all
        main = 'org.apache.gora.compiler.Compiler'
        args = ["src/main/json/$file.name", "$buildDir/generated-src/src/main/java"]
      }
    }
  }
}
like image 157
ajoberstar Avatar answered Sep 17 '22 19:09

ajoberstar