others-how to share repositories and build scripts in gradle projects with submodules

Problem

This article would demo how to to share repositories and build scripts in gradle project with submodules, I would use gradle to build a project with two spring boot submodules which are both built by gradle too.

Environment

  • Mac OS
  • Gradle 6.7.1
  • IntelliJ IDEA 2019.3

Solution

The whole directory tree of the project is as follows, the root project contains two submodules app1 and app2, they are all managed by gradle.

root project
   |-- gradle
          |--wrapper
               |--gradle-wrapper.properties
   |-- settings.gradle
   |-- build.gradle
   |-- gradlew
   |-- app1
         |
         |--build.gradle
   
   |-- app2
         |--build.gradle 

File 1: The root project’s settings.gradle

rootProject.name = 'bswen-springboot23'
include 'app1'
include 'app2'

File 2: The root project’s build.gradle

This is the key point of sharing scripts in gradle projects with submodules.

buildscript {
    repositories {
        mavenLocal()
        maven { url 'https://maven.aliyun.com/repository/google/' }
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/spring/' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
        maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
    }
}


plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE' apply false
    id 'io.spring.dependency-management' version '1.0.8.RELEASE' apply false
    id 'java'
}

repositories {
    mavenLocal()
    maven { url 'https://maven.aliyun.com/repository/google/' }
    maven { url 'https://maven.aliyun.com/repository/public/' }
    maven { url 'https://maven.aliyun.com/repository/spring/' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
    maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }

    mavenCentral()
}

Pay attention to the plugins block as follows, the apply false means that the plugin is not required by the root project. Then submodules can apply these plugins by their needs, and the version of the plugins can be omited in the submodules’ bulid.gradle.

plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE' apply false
    id 'io.spring.dependency-management' version '1.0.8.RELEASE' apply false
    id 'java'
}

File 3: app1’s build.gradle

The submodule’s build.gradle is as follows:


plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'java'
}

group 'com.bswen.app1'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

You can see that there is no plugin version info in the plugins block, and you also do not need the repositories and buildscript block in submodules.