Gradle multi module projects

gradle logo

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.

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

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

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.

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.

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.

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.

Manthan Dave
I like awesome things. Currently transforming the world bit by bit. I'm a Software Engineer. (All views my own)

Leave a Comment

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

%d bloggers like this: