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

**URL:** https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384
**Category:** Old Forum Archive
**Created:** [May 15, 2012, 8:29pm UTC](https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384 "2012-05-15T20:29:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tinkerer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/tinkerer/32/683_2.png) [@Tinkerer](https://discuss.gradle.org/u/Tinkerer)
#### Post date: [May 15, 2012, 8:29pm UTC](https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384/1 "2012-05-15T20:29:00Z")

</div>

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

```gradle
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")})

```

---

<div class="post-metadata">

### Author: ![Tinkerer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/tinkerer/32/683_2.png) [@Tinkerer](https://discuss.gradle.org/u/Tinkerer)
#### Post date: [May 16, 2012, 1:10pm UTC](https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384/2 "2012-05-16T13:10:00Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)
#### Post date: [May 16, 2012, 2:58pm UTC](https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384/3 "2012-05-16T14:58:00Z")

</div>

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:

```gradle
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é

---

<div class="post-metadata">

### Author: ![Tinkerer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/tinkerer/32/683_2.png) [@Tinkerer](https://discuss.gradle.org/u/Tinkerer)
#### Post date: [May 16, 2012, 5:53pm UTC](https://discuss.gradle.org/t/dynamictasks-type-javaexec-to-compile-js-class-leads-to-concurrentmodificationexception/5384/4 "2012-05-16T17:53:00Z")

</div>

Excellent! Thanks for the advice.
