Gradle multi-project custom build.gradle file name

6.6k views Asked by At

I have a multi-project Gradle build, which is currently configured through a single build.gradle file.

There are over 70 modules in this project, and the single (gigantic) build.gradle file has become cumbersome to use, so I'd like to split it into small per-module buildscript files.

Now, I don't want to have 70 small build.gradle files (one in each module), as that would make navigating to a specific build.gradle a pain in the IDE (the only difference between the files is their path).

What I want is my per-module buildscript files to be named after the module name.

Instead of this:

root  
|--foo\  
|--| build.gradle  
|--bar\  
|--| build.gradle 

I want this:

root  
|--foo\  
|--| foo.gradle  
|--bar\  
|--| bar.gradle 

Since this doesn't seem to be officially supported, I tried hacking around the root build.gradle a bit, but it seems that applying a .gradle file happens before the projects are configured, so this gives an error for projects that depend on other projects:

in root build.gradle:

subprojects { subProject ->
    rootProject.apply from: "${subProject.name}/${subProject.name}.gradle"
}

foo.gradle, which is not a standard build.gradle file:

project('foo') {
    dependencies {
        compile project(':bar')
    }
} 

Is there any way of making it work like this?

3

There are 3 answers

2
Jolta On BEST ANSWER

A web search for "gradle rename build.gradle" rendered the below example settings.gradle file:

rootProject.buildFileName = 'epub-organizer.gradle'

rootProject.children.each { project ->
    String fileBaseName = project.name.replaceAll("\p{Upper}") { "-${it.toLowerCase()}" }
    project.buildFileName = "${fileBaseName}.gradle"
}

Note that the author is here also renaming the root project's build script, which you may or may not want.

One of the authors of Gradle, Hans Dockter, has said somewhere (I believe it was in his "Rocking the Gradle" demo from 2012), that he felt one of their biggest mistakes was using build.gradle as the default file name.

1
Radim On

You can customize name of your build scripts in settings.gradle file. Check recent presentation from Ben Muschko about multi-project builds or look at Gradle sources where similar customization is done.

rootProject.children.each {
  it.buildFileName = it.name + '.gradle'
}
0
FBH On

You can find this content in Gradle in action, manning

enter image description here