2
votes

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?

1
Yes, you can surely do it, the only question is how. Maybe it would be good to change your question. - Grzegorz Żur

1 Answers

6
votes

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}"