Gradle multi-project custom build.gradle file name
Asked Answered
L

3

6

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?

Lehman answered 16/6, 2015 at 15:36 Comment(0)
E
7

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.

Eugenle answered 17/6, 2015 at 9:51 Comment(2)
Thank you, sir! Of all the google searches I tried, "rename" wasn't one of them. Thanks!Lehman
Note: In 6.2.1, I had to include the children before iterating over them. Seems obvious, but I didn't catch it right away.Asylum
C
7

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'
}
Cletacleti answered 16/6, 2015 at 21:43 Comment(1)
Detailed on slide 52 of 78 from Ben Muschko found in above link "multi-project"Clarabelle
E
7

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.

Eugenle answered 17/6, 2015 at 9:51 Comment(2)
Thank you, sir! Of all the google searches I tried, "rename" wasn't one of them. Thanks!Lehman
Note: In 6.2.1, I had to include the children before iterating over them. Seems obvious, but I didn't catch it right away.Asylum
D
1

You can find this content in Gradle in action, manning

enter image description here

Desma answered 27/9, 2018 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.