Call gradle task from groovy w/o Runtime.exec()

gradle, groovy

Solution

You can use Gradle Api to execute the task programmatically:

    ProjectConnection connection = GradleConnector.newConnector()
            .forProjectDirectory(projectDir) // the gradle project directory
            .connect()

    connection.newBuild()
                .forTasks(taskName) // set your task name here
                .run()

You need to depend on Gradle Api (the jar as you mentioned):

     "org.gradle:gradle-tooling-api:${gradle.gradleVersion}"

Problem

Can I call to gradle task from groovy script w/o calling gradle (gradlew) script as external program? I.e. put gradle jar to classpath and calling to main?

Original source