Cody Hoag
Cody Hoag

Reputation: 97

Calling Gradle task for only one subproject

How do I configure subprojects for Gradle and run a Gradle task from the build.gradle only for the current directory I'm executing the task from?

For example, suppose I have two projects in a parent folder:

I have project 1 and 2 included in the settings.gradle file:

include(
    'project-1',
    'project-2'
)

I have a task configured in the build.gradle file that I would like to call. I've nested the tasks within a subprojects{} block:

subprojects {
    ...
    task check(type: JavaExec) {
        main = MAIN_CLASS
        classpath = sourceSets.main.runtimeClasspath
    }
    ...
}

However, the build.gradle is run for all subprojects that way.

How can I define all possible subprojects but only call a Gradle task for the current project I'm executing from (i.e., avoid calling the task for all subprojects)? I would like to do this with the current task name only (e.g., gradle check).

Upvotes: 1

Views: 5943

Answers (1)

Louis Jacomet
Louis Jacomet

Reputation: 14500

Invoking the task in a single project with gradle check is not possible.

You have two options:

  • Leverage the model Gradle proposes, where tasks can be prefixed by the project name in a multi project build which removes the need to navigate to subprojects.
    • Which means invoking gradle :project-1:check or gradle :project-2:check depending on which task you want to run.
  • Define the tasks to have different name in each subprojects. See task rules on how to define tasks with dynamic names.
    • Which means invoking gradle check1 or gradle check2

Upvotes: 1

Related Questions