Recently I’ve had to deal with a lot of gradle projects, specifically the multi-module ones. It’s dead easy to setup, however, every time I do it, I have to refer to one of my old projects to see the layout. So, hopefully, while this document will certainly help future me, I hope it is of help to you too.

First things first, create a project directory. We will refer this directory as ‘project root directory’ in future. For all the awesome things that I do, we’ll call this one, drum roll please … awesomeproject.

Shell

1

2

mkdirawesomeproject

cdawesomeproject

Once in that directory, lets create the
build.gradle file at the project root with the following content:

build.gradle

1

2

// build.gradle

apply plugin:'distribution'

Alongside this, create a
settings.gradle file but leave it blank for now.

Shell

1

touchsettings.gradle

Now create your sub-module project directories. In this example, we’ll create two directories, awesomeproject-scripts which will hold our deployment scripts and awesomeproject-service which will contain our actual java code.

Shell

1

mkdirawesomeproject-scripts awesomeproject-service

Time to create gradle files for your sub-modules. We won’t go into the detail of what the gradle files will actually contain, however, for semi-completeness, we’ll just apply some plugins as a marker to indicate the type of sub-module projects.

awesomeproject-scripts/build.gradle

1

2

// awesomeproject-scripts/build.gradle

apply plugin:'distribution'

awesomeproject-service/build.gradle

1

2

// awesomeproject-service/build.gradle

apply plugin:'java'

As you can see from above, our
awesomeproject-scripts/build.gradle file applies the distribution plugin to indicate that it will need to archive its contents while the
awesomeproject-service/build.gradle applies the java plugin to indicate that it will be compiling its java source.

Amazing! Now we just need to tell our root project to include these two sub-modules. This is simple. You know we left that
settings.gradle file in the project root blank? Well, lets change that.

settings.gradle

1

2

// settings.gradle

include'awesomeproject-scripts','awesomeproject-service'

Here, we are telling the root gradle project to include the sub-modules. Gradle will then expect
build.gradle files within these directories and will execute jobs. It’s pretty clever so if you’ve got a module that needs to execute one of the other sub-modules first, it will run it in the correct required order.

More detailed documentation on this is available on gradle’s website, however, this should get you up and running quickly.