smac89
smac89

Reputation: 43234

Run gradle task in subproject

I have a multi-project build like so

main
|_build.gradle
|_settings.gradle
|_examples
|_|_build.gradle
|_|_simple-project
|_|_|_build.gradle

settings.gradle

rootProject.name = 'main'
include 'examples'
include 'examples:simple-project'

Now in the examples project build.gradle, I have declared a task for all subprojects like so:

subprojects {
    task hello(group: 'example') {
        doLast {
            println "${it.project.name}: I am part of example"
        }
    }
}

When I run gradle tasks --all, I get output similar to the following (amoung others):

Example tasks
-------------
examples:simple-project:hello

Now I try to run that task for simple-project like this

gradle -p examples/simple-project hello

And the output I get is:

FAILURE: Build failed with an exception.

  • What went wrong: Task 'hello' not found in project ':examples:simple-project'. Some candidates are: 'help'.

  • Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s

What am I doing wrong? What is the correct way to run the task in the subproject?


Edit

If I run this instead

gradle -p examples hello

then this also runs the subproject task (hello)

> Task :examples:simple-aspect-project:hello

simple-aspect-project: I am an example

I don't want this. I want to be able to run the task for the subproject I want, not all of them at once.

Upvotes: 1

Views: 6229

Answers (1)

ToYonos
ToYonos

Reputation: 16833

What about a simple gradle :examples:simple-project:hello

I think setting -p examples/simple-project reduces the scope to the simple-project project and cancelled the hello task creation which is located in the examples project.

Upvotes: 2

Related Questions