bearhunterUA
bearhunterUA

Reputation: 259

Parallel execution in Gradle

I am migrating my build from Ant to Gradle and trying to rewrite this code block:

 <parallel>
            <exec executable="npm">
                <arg value="run"/>
                <arg value="dev"/>
            </exec>
            <java className="foo.bar.Launcher" fork="true">
                <classpath>
                    <path refid="project.classpath"/>
                </classpath>
            </java>
 </parallel>

But I hadn't found something similar to parallel tag, only solutions with Java concurrency or some other workarounds. Does Gradle have some proper way to execute something in parallel without workarounds?

Upvotes: 4

Views: 5362

Answers (1)

Michael Easter
Michael Easter

Reputation: 24498

Executing tasks (in the same project) in parallel has been an incubating feature (or not yet available) until Gradle 5.0, when it was promoted.

However, in your case, note that Gradle contains an Ant Builder. Because you are not trying to run tasks in parallel, it might be a straight-ahead port. (Here is a discussion of using ant.parallel.)

To illustrate, consider this shell-script, wait.sh:

sleep $1 
echo $2 >> wait.log

And the following tasks:

task doItSerial << {
    ant.exec(executable: "${projectDir}/wait.sh") {
        arg(value: 10)
        arg(value: "TRACER A")
    }
    ant.exec(executable: "${projectDir}/wait.sh") {
        arg(value: 10)
        arg(value: "TRACER B")
    }
}

task doItParallel << {
    ant.parallel {
        ant.exec(executable: "${projectDir}/wait.sh") {
            arg(value: 10)
            arg(value: "TRACER A")
        }
        ant.exec(executable: "${projectDir}/wait.sh") {
            arg(value: 10)
            arg(value: "TRACER B")
        }
    }
}

In my experimentation, doItSerial takes 20 seconds; doItParallel takes 10 seconds, as we would anticipate.

Upvotes: 2

Related Questions