简体   繁体   中英

Use Spring Boot as a transitive dependency from common JAR with Gradle

I have a common library that is published as a jar to maven local, which is then used by a set of very thin client applications. These applications basically feed their own configuration to the common library and initializes themselves as Spring applications.

This common library uses spring boot and a bunch of other dependencies. My question is, is it possible for me to not define spring boot as a dependency in the applications, and simply use the common library's spring version as a transitive dependency in all the applications?

I feel like this should be possible, but when I tried doing it, the springframework.org.boot classes are not being resolved (eg: @SpringBootApplication annotation).

These are my build.gradle files:

build.gradle from the common library

plugins {
    id 'java-library'
    id 'maven-publish'
}

group 'com.my.group'
version '1.0-SNAPSHOT'

sourceCompatibility = '11'

repositories {
    mavenLocal()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'com.my.group'
            artifactId = 'common-lib'
            version = '0-alpha.1'

            from components.java
        }
    }
}

configurations {
    springBom
    compileOnly.extendsFrom(springBom)
    annotationProcessor.extendsFrom(springBom)
    implementation.extendsFrom(springBom)
}

dependencies {
    springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')

    api 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.cloud:spring-cloud-aws-messaging'

    annotationProcessor 'org.springframework.boot:spring-boot-autoconfigure-processor'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    
    // a few other non-spring dependencies
}

build.gradle from a sample application

plugins {
    id 'java'
}

group = 'com.my.group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenLocal()
}

dependencies {
    implementation group: 'com.my.group', name: 'common-lib', version: '0-alpha.1'
}

Example application initialization

package com.my.group.Core;
// none of the below are resolved
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;

@SpringBootApplication(scanBasePackages = {"com.my.group"})
public class ConsumerApplication implements ApplicationRunner {

    @Autowired
    Core core;

    public ConsumerApplication() {
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            core.processEvents();
        } catch (ConfigurationException | InitializationException e) {
            e.printStackTrace();
        }
    }
}

If I add in the following dependencies to my applications, everything works as expected.

springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter'

I found the issue. I have not included api in the configurations and as a result, the application could not resolve the version. Fixed by updating the build.gradle in my common library like so:

configurations {
    springBom
    compileOnly.extendsFrom(springBom)
    annotationProcessor.extendsFrom(springBom)
    implementation.extendsFrom(springBom)
    api.extendsFrom(springBom)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM