47


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?

5 Answers 5

64

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!

3
  • Should this "onlyIf" statement be placed in the main build.gradle of the project, or in that of the functional-tests subproject?
    – retrovius
    Sep 19, 2019 at 15:02
  • In the functional-tests subproject.
    – Scott
    Apr 13, 2020 at 20:58
  • @Scott I am having similar requirement .i want to have flexibility to include any sub-projects in a jar created by bootJar.Say my project has 6 sub-modules but i want to say now create an executable jar using bootJar of only 4 sub-modules ,Is it possible doing this from commands such as for example something on the lines of gradle bootJar --exclude subproject3,subproject4? Is there any solutions for such requirements? Apr 8, 2021 at 14:28
23

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.

12

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.

4
  • Hi @Opal, I guess the first option should be enough for my needs. However, our Gradle builds in CI always run full build without -a option. So I guess -a option will not work for that case.
    – Paddy
    Feb 2, 2015 at 7:11
  • 1
    @Opal: can you provide a full example including build.gradle?
    – Nayan
    Apr 16, 2015 at 12:11
  • @Rafiqunnabi, please describe your problem.
    – Opal
    Apr 16, 2015 at 12:13
  • I've a sub-project named functional-test. I want to exclude this sub-project from being built when I run the build command of the root project.
    – Nayan
    Apr 16, 2015 at 12:19
7

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

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.