Run two subproject tasks simultaneously

I have a monorepo setup such as:

monorepo
 build.gradle
 /api
   build.gradle
 /webapp
   build.gradle

I’ve two separate tasks which build and run a development server: :api:bootRun and :webapp:runDev.

Is it possible to get these two tasks to run in parallel? They are not dependent on each other until the build task bootJar is run - which is outside the scope.

I’m currently trying to work around this using GPars, but to no avail:

tasks.register("dev") {
    def tasksToRun = ['api:bootRun', 'webapp:runDev']

    GParsPool.withPool { ExecutorService svc -> 
        tasksToRun.each { taskToRun ->
            svc.submit({runDevTaskFromProject(tasksToRun)})
        }
    }
}

def runDevTaskFromProject(String projectTask) {
    try {
        String[] components = projectTask.split(':')
        GradleRunner.create()
            .withProjectDir("$projectDir/" + components[0])
            .withArguments(components[1])
            .forwardOutput()
            .build()
    } catch (Exception e) {
        println "Run Failed " + e
        return
    }
}

Is there a better way of doing this - or do I need to write something based on the Workers API to facilitate this?

Things using the worker api and do not depend on each other can run in parallel, even within one project, yes.
Another point is, that --parallel or org.gradle.parallel enables tasks of different projects to run in parallel, so would probably also help you.

Thanks @Vampire

I believe org.gradle.parallel=true is set in the project level gradle.properties;

How would I structure the parent task to use with --parallel?

The property and parameter do the same.
But no idea what you mean with your question.

@Vampire for the question, how do I write that task so that the two subtasks (which have different names) are called in parallel?

Just depend on them I guess?

Wow - that worked, I didn’t know parallel worked on dependsOn thanks :slight_smile: