Search code examples
javaeclipsegradlegradle-eclipse

Gradle alternate name for build.gradle and conventions for nested modules


Here is a problem I run into.

In Eclipse/STS when I open multiple build.gradle I can't tell what files they belong without looking at the path. (This is same as pom.xml in Maven.)

Is there a way to say have something like below:

top-project
        build.gradle
        module-1
            module-1.gradle
        module-2
            module-2.gradle
        .
        .
        module-n
            module-n.gradle

In the above module-1 etc. are just names of sub-modules as in one real life example this could be acme-schema or acme-web

  1. How does gradle build work ?
  2. More importantly - how does eclipse cope with this during apply eclipse / Build Model ?

Solution

  • This is a pretty common requirement for multi-project builds. It should really be in the user guide. Anyway, you can see an example in one of my open source projects. All you need to do is add something like the following to the settings.gradle file in the root of the project:

    // Change names of build files in sub-projects for easier navigation in IDEs
    // and text editors.
    renameChildBuildFiles(rootProject)
    
    void renameChildBuildFiles(parentProject) {
        for (p in parentProject.children) {
            p.buildFileName = (p.name - "my-app-") + ".gradle"
        }
    }
    

    This specific bit of code uses the directory names of the subprojects as the base names of the build files, assuming that all subproject directories are prefixed with "my-app-".

    You can use any rule you like in setting the file names of the subproject build files. You don't have to use this one.