How to setup dependecy between modules to produce a fat-jar?

I have a multi-module build with gradle for a java library.

This is the folder structure:

- moduleA
  - src
  - build.gradle
- moduleB
  - src
  - build.gradle
- moduleC
  - src
  - build.gradle
- mainModule
  - src
  - build.gradle
build.gradle
settings.gradle

I want to create a fat-jar form the mainModule, where all the source code from the 4 modules are included in the same jar.

In mainModule's build.gradle I have this:

apply plugin: 'java-library'

version = '0.1.0'

jar {
    manifest {
        attributes('Implementation-Title': project.name,'Implementation-Version': project.version)
    }

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

dependencies {
    api project(':moduleA')
    api project(':moduleB')
    api project(':moduleC')
    api 'random.lib:vendor:1.0.0'
}

And this is the settings.gradle:

rootProject.name = 'ProjectName'
include('moduleA')
include('moduleB')
include('moduleC')
include('mainModule')

But when I tried to build the project with ./gradlew clean build, I got the error:

Execution failed for task ':mainModule:jar'.
> Cannot expand ZIP 'moduleA/build/libs/moduleA-0.1.0.jar' as it does not exist.

Gradle it is trying to create the fat-jar without building the other modules first, probably because I did not set up something properly.

So, how can I organize these dependecies where the other modules are build first then the mainModule?