Gradle snippets: Rename your subprojects’ build files

It’s good to have project separated into modules. It encourages separation of functionalities and simplifies code. Fortunately gradle supports multiple projects very well.
Having a project with 2 modules like this:

- awesome-project-root
-- build.gradle
-- settings.gradle
-+ persistence
---- build.gradle
-+ processing
---- build.gradle

Next thing is to add to settings.gradle several lines with subprojects:

include "persistence"
include "processing"

Since now you have 3 build.gradle files in your repository. They are configured as subprojects. You may read more about subprojects in gradle’s documentation. It may be confusing when your projects grows up and you’ll have more of them.

Really nice thing I’ve found in hibernate’s configuration (see https://github.com/hibernate/hibernate-core), is renaming of all these build.gradle files.

rootProject.children.each { project ->
    project.buildFileName = "${project.name}.gradle"
    assert project.projectDir.isDirectory()
    assert project.buildFile.isFile()
}

This little code snippet lets gradle know that your project’s structure will be:

awesome-project-root
-- build.gradle
-- settings.gradle
-+ persistence
---- persistence.gradle
-+ processing
---- processing.gradle

And this really simplifies navigation. I think it’d be nice if gradle supported both naming conventions.

Tagged with: , , ,
Posted in gradle snippets

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>