Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle multi-project custom build.gradle file name

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?

like image 425
Yevgeny Krasik Avatar asked Jun 16 '15 15:06

Yevgeny Krasik


1 Answers

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.

like image 162
Jolta Avatar answered Nov 15 '22 10:11

Jolta