DynamicTasks (type: JavaExec) to compile .JS --> .class leads to ConcurrentModificationException


(tinkerer.tech) #1

Gradle M6: Is there anything inherently wrong about this script-

configurations.add('rhinoJS')
dependencies {
 //rhinoJS 'rhino:js:1.7R2'
                              rhinoJS 'rhino:js:1.6R1'
           }
repositories {
    mavenCentral()
}
  def path = ant.path {
        fileset(dir: "src/scripts", includes: '*.js')
}
path.list().each { jsPath ->
 def jsFile = new File(jsPath)
 def jsFileWoExt = jsFile.name.lastIndexOf('.').with {it != -1 ? jsFile.name[0..<it] : jsFile.name}
        task "JSCompile$jsFileWoExt(type:JavaExec)" << {
  main = "org.mozilla.javascript.tools.jsc.Main"
  classpath = configurations.rhinoJS
  doLast {
   args = ["-o", "${jsFileWoExt}.class", "-package", "scripts", "${jsPath}" ]
    }
 }
}
task compileScripts(dependsOn: tasks.matching { Task task -> task.name.startsWith("JSCompile")})

(tinkerer.tech) #2

I’m also open to exploring alternate ways of approaching this problem… basically what I want is to compile a bunch of .js files in a certain folder to .class. Thanks!


(René Groeschke) #3

Hello Tinkerer, some notes about your snippet: * The type definition of your task is not working as it is part of the task name you’re dynamically declaring. * Using the << in the task declaration is a shortcut for a doLast{} closure. When declaring a typed task you normally never use this shortcut as you don’t want to add a doLast closure but add a configuration closure (just remove the << ) * you can move the args declaration in your task to the configuration phase.

Here’s a modified snippet that should do the trick:

configurations.add('rhinoJS')
dependencies {
    rhinoJS 'rhino:js:1.6R1'
           }
  repositories {
    mavenCentral()
}
  def path = ant.path {
        fileset(dir: "src/scripts", includes: '*.js')
}
  path.list().each { jsPath ->
    def jsFile = new File(jsPath)
    def jsFileWoExt = jsFile.name.lastIndexOf('.').with {it != -1 ? jsFile.name[0..<it] : jsFile.name}
    task "JSCompile$jsFileWoExt"(type:JavaExec) {
        main = "org.mozilla.javascript.tools.jsc.Main"
        classpath = configurations.rhinoJS
        args = ["-o", "${jsFileWoExt}.class", "-package", "scripts", "${jsPath}" ]
            }
}
task compileScripts(dependsOn: tasks.matching { Task task -> task.name.startsWith("JSCompile")})

regards, René


(tinkerer.tech) #4

Excellent! Thanks for the advice.