gradle 多工程apply plugin问题
最近用gradle编译spring cloud的项目,在父工程加入
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
....
的时候,遇到了一个比较奇怪的问题
gradle subprojects plugin with id not found
后来找到一个临时的方案,现在记录在这里
假如我有两个项目,一个是eureka-service, 一个是eureka-client,前者代表用eureka构建的eureka的服务器,后者代表使用eureka构建的客户端,两者其实有很多共同的build.gralde 元素,最直白的就是对于spring boot等的plugin的引用
原始的erueka-client的build.gradle 文件
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'eureka-client'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
// mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR4'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
原始的eureka-service的gradle文件
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'eureka-service'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
//mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR4'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
可以看到,除了dependencies这部分,我们可以单独存放,其他的,都是一样的,我们完全可以放到父工程的gradle文件里,于是就要这么办,在父工程的build.gradle中
subprojects {
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
// mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR4'
}
}
}
这时候,问题就来了,会报错,提示说找不到plugin java和org.springframework.boot等,查了网页才知道要这么搞
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
// mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR4'
}
}
}
感觉是在subprojects 里不能放置buildscript的东西,估计还是gradle生命周期的问题。