Gradle exclude a specific subproject from full build
Asked Answered
H

5

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?

Halting answered 2/2, 2015 at 6:41 Comment(0)
A
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!

Ably answered 4/12, 2015 at 15:50 Comment(3)
Should this "onlyIf" statement be placed in the main build.gradle of the project, or in that of the functional-tests subproject?Balcom
In the functional-tests subproject.Ably
@Ably 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?Calcaneus
M
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.

Misinform answered 20/2, 2017 at 7:34 Comment(0)
P
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.

Pointless answered 2/2, 2015 at 6:57 Comment(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.Halting
@Opal: can you provide a full example including build.gradle?Unconformity
@Rafiqunnabi, please describe your problem.Pointless
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.Unconformity
B
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 }
    }
  }
}
Bandler answered 8/12, 2016 at 7:50 Comment(0)
P
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/

Plotter answered 15/3, 2022 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.