Paddy
Paddy

Reputation: 3640

Gradle exclude a specific subproject from full build


In our Gradle project, we want to add a new module for functional-tests that needs to be able to access dependencies from other subprojects but still not be run as part of the full project build. If I try this, it still gets built:

def javaProjects() {
   return subprojects.findAll { it.name != 'functional-tests' }
}

configure(javaProjects()) {
   ...
}

project(':functional-tests') {
    ....
}

The result is the same even if I move the functional-tests build to a separate build.gradle file of its own. Can someone point out how to achieve this?

Upvotes: 47

Views: 75296

Answers (5)

Aleksandar Radulović
Aleksandar Radulović

Reputation: 329

Just to mention that you don't need to create a new module for integration/functional tests. I prefer to make a new, dedicated source set.

The approach is nicely described here: https://tomgregory.com/gradle-integration-tests/

Upvotes: 0

Bruce
Bruce

Reputation: 1766

I do it like this:

//for all sub projects
subprojects {
    if (it.name != 'project name') {
        //do something
    }
}

by this way, I can exclude some special project in subprojects.

you can also use it in allprojects or project.

Upvotes: 24

Arto Pastinen
Arto Pastinen

Reputation: 71

You can't exclude the subproject, but you can disable subproject tasks:

gradle.taskGraph.whenReady {
  gradle.taskGraph.allTasks.each {
    if(it.project == project) {
      it.onlyIf { false }
    }
  }
}

Upvotes: 7

Scott
Scott

Reputation: 1550

I found a better solution to be to exclude the functional tests from running on the command line or via the build file.

For example, to run all tests except the functional tests, run:

$ gradle check -x :functional-tests:check

Then when building the project, you can let the subproject build but exclude their tests from running.

$ gradle clean assemble -x :functional-tests:check

A better option is do disable the functional tests in your build file unless a property is set. For example, in your build.gradle you'd add:

project('functional-tests') {
    test {
        onlyIf {
            project.hasProperty("functionalTests")
        }
    }
}

This way, functional tests are always skipped unless you specify a specific build property:

$ gradle check
$ gradle -PfunctionalTests check

Hope that helps!

Upvotes: 64

Opal
Opal

Reputation: 84884

As far as I know it's not possible to deactivate or exclude project after it as been included in settings.gradle. Therefore it maybe done in the following way in settings.gradle:

include 'p1', 'p2', 'p3'

if (any_condition_here) {
   include 'functional-tests'
}

It will require additional checking in build.gradle - to configure the project if it's included.

What also comes to my head is -a command line switch, see here. Maybe it might helpful somehow.

Upvotes: 12

Related Questions