springgradlecircular-dependencymulti-modulesubproject

Gradle Multi Module Project: Apply module dependency to all subprojects except for itself


My application is a Gradle Multi Module Project, consisting of multiple services with one service-common module.

I´ve pulled all dependencies that all modules have in common out into the root build.gradle, and I also want to include the service-common module in all subprojects, which in theory works, but I´m getting a circular dependency issue because it is included in itself.

apply plugin: 'java'

group = 'com.myapplication'

ext {
    set('springCloudVersion', "2.2.0.RELEASE")
    set('springBootVersion', "2.2.2.RELEASE")
}

allprojects {

    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
    }

}

buildscript {
    ext {
        springBootVersion = "2.2.2.RELEASE"
    }

    repositories {
        maven { url 'https://repo.spring.io/plugins-snapshot' }
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.7.BUILD-SNAPSHOT'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

subprojects {
    version = '1.0'

    apply plugin: 'org.springframework.boot'
    apply plugin: "io.spring.dependency-management"
    apply plugin: 'java'

    ext {
        springCloudVersion = "2.2.0.RELEASE"
        springBootVersion = "2.2.2.RELEASE"
    }

    test {
        useJUnitPlatform()
    }

    repositories {
        mavenCentral()
        jcenter()
        maven { url 'https://repo.spring.io/milestone' }
    }

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

    dependencies {
        compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: "${springBootVersion}"
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: "${springBootVersion}"
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: "${springCloudVersion}"
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-kubernetes', version: '1.1.1.RELEASE'

        compile group: 'io.micrometer', name: 'micrometer-registry-prometheus', version: '1.3.2'

        compile group: 'com.github.piomin', name: 'logstash-logging-spring-boot-starter', version: '1.2.2.RELEASE'

        compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
        compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

        compileOnly 'org.projectlombok:lombok:1.18.10'
        annotationProcessor 'org.projectlombok:lombok:1.18.10'

        testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
        compile group: 'org.postgresql', name: 'postgresql', version: '42.2.9'

        testCompile group: 'org.springframework.cloud', name: 'spring-cloud-stream-test-support', version: "${springCloudVersion}"

        implementation project(":service-common")
    }

}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

How can I exclude implementation project(":service-common") for the service-common module?


Solution

  • You can do something like:

    subprojects {
        dependencies {
            if (!project.name == "service-common") {
                implementation(project(":service-common"))
            }
        }
    }