1

I have around 25 tasks for various devops things as part of my large multiproject build.

The structure of the project looks like this

RootProject
  -buildSrc
  -DirectoryA
     -SubProjectA1
     -SubProjectA2
     -SubProjectA3
  -DirectoryB
     -SubProjectB1
     -SubProjectB2
  -DirectoryC
     -SubProjectC1
     -SubProjectC2

etc..

Directories are there just to conveniently separate projects. RootProject and all SubProject have build.gradle files.

In the RootProject build.gradle file there are around 25 tasks. These tasks are configured on both root project and subprojects.

They make build.gradle rather long and complicated. So I thought of moving them into separate Tasks.gradle file located under buildSrc directory. And then pulling them in with "apply from"

Is this a good practice or is there a better way?

If I pull them in, there is another issue. Some tasks are configured for SubProjects, and other are configured for RootProject. So I can't really just put them into single Tasks.gradle file.

Should I then put them into something like RootProjectTasks.gradle and SubProjectsTasks.gradle?

It all feels a little wrong...

JohnyC
  • 13
  • 2
  • Possible duplicate of [Is there a way to split/factor out common parts of Gradle build](https://stackoverflow.com/questions/2566685/is-there-a-way-to-split-factor-out-common-parts-of-gradle-build) – tkruse Jan 09 '18 at 08:02

2 Answers2

0

If you use Jenkins for build, then you should make build.gradle for every sub-project and also for Root Project.

In Jenkins, you can Invoke Gradle script multiple times. So, you can invoke every build.gradle file one by one.

There is another way to invoke every build.gradle of sub-projects by invoking only Root projects build.gradle. For that you must add following line in settings.gradle

include ":sub-project1-name", ":sub-project2-name", ":sub-project3-name",

Feel free for more information

0

Do not put *.gradle file into the buildSrc folder. The buildSrc folder is a nested project folder that should be organized like a project and contain only *.java or *.groovy files in buildSrc/src/main/groovy.

You can extract tasks into separate gradle files and include them via apply from.

Tasks that apply to all projects can go here:

allprojects {
    apply plugin: 'idea'
}

Tasks that apply only to all subprojects can go here:

subprojects {
    apply plugin: 'java'
}

You can either also include them directly from the submodules that need them. Or apply to only some subprojects like so:

def clientProjects() {
    return subprojects.findAll {
            // some predicate that is true only for client projects
    }
}

configure(clientProjects()) {
    apply plugin: ...
}
tkruse
  • 10,222
  • 7
  • 53
  • 80