简体   繁体   中英

Gradle multiple subproject with different spring boot plugin

I have multi project build structure and spring boot is applied from the parent to all subprojects. Here's the parent build.gradle

buildscript {
  repositories {
    maven {
            url 'http://someurl.com/repository/MavenRepositoryGroup/'
        }
  }

  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE"
  }
}


subprojects  {

    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'eclipse'
    apply plugin: 'war'
    apply plugin: 'org.springframework.boot'
..
..
..
}
}

Among multiple subprojects, I have a requirement to upgrade just one subproject to spring boot 2 version. How to do it?

It's pretty easy, just declare plugins in subprojects directly, where they are used. BTW, consider using new plugins DSL instead of apply . Take a look at a demo project I created for you. Here are the most interesting parts:

settings.gradle :

rootProject.name = "so53381565"

enableFeaturePreview("IMPROVED_POM_SUPPORT")

include(":a")
include(":b")
include(":c")

build.gradle :

subprojects {
    apply plugin: "java"

    repositories {
        jcenter()
    }
}

wrapper {
    gradleVersion = "4.10.2"
    distributionType = Wrapper.DistributionType.ALL
}

a/build.gradle :

plugins {
    id "org.springframework.boot" version "1.5.8.RELEASE"
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter"
}

b/build.gradle :

plugins {
    id "org.springframework.boot" version "1.5.17.RELEASE"
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter"
}

c/build.gradle :

plugins {
    id "org.springframework.boot" version "2.0.6.RELEASE"
}

dependencies {
    implementation "org.springframework.boot:spring-boot-dependencies:2.0.6.RELEASE"
    implementation "org.springframework.boot:spring-boot-starter"
}

App.java :

@SpringBootApplication
public class App implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(SpringVersion.getVersion());
    }
}

App.java is the same in all three subprojects. Executing ./gradlew clean :a:bootRun :b:bootRun :c:bootRun will output something like:

…
4.3.12.RELEASE
…
4.3.20.RELEASE
…
5.0.10.RELEASE
…

As you see, you're using three different Springs in three different subprojects.


EDIT

First of all, you can still use apply , just put them in subprojects.

Second, you can use plugins with your own repositories and dependencies, even with those not published in Gradle's Plugin Repository by adding a pluginManagement block in settings.gradle (this is Kotlin DSL from my project):

pluginManagement {
    repositories {
        gradlePluginPortal()
        add(jcenter())
        add(maven("http://my.jfrog.io"))

    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "by.dev.madhead.some-plugin") {
                useModule("by.dev.madhead:some-gradle-plugin:${requested.version}")
            }
        }
    }
}

Now I am able to use by.dev.madhead.some-plugin in plugins DSL across the project:

plugins {
    id("by.dev.madhead.some-plugin").version("42")
}

And it will be substituted by by.dev.madhead:some-gradle-plugin:42

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