Spring boot gradle插件强制Mockito版本

Ami*_*iri 6 java spring gradle spring-boot spring-boot-gradle-plugin

我大致有以下设置:

test-utils/build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

dependencies {
    compile ('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.mockito'
        exclude group: 'org.hamcrest'
    }
    compile  'org.mockito:mockito-core:2.0.41-beta'
    compile  'org.assertj:assertj-core:3.3.0'
}
Run Code Online (Sandbox Code Playgroud)

main/build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

dependencies {
    testCompile project(':test-utils')
}
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,似乎spring boot插件强制mockito版本回到1.x:

# ./gradlew :main:dependencies --configuration=testCompile

testCompile - Compile classpath for source set 'test'.
+--- project :test-utils
     +--- org.springframework.boot:spring-boot-starter-test: -> 1.3.1.RELEASE
     |    +--- junit:junit:4.12
     |    +--- org.springframework:spring-core:4.2.4.RELEASE
     |    \--- org.springframework:spring-test:4.2.4.RELEASE
     |         \--- org.springframework:spring-core:4.2.4.RELEASE
     +--- org.mockito:mockito-core:2.0.41-beta -> 1.10.19
     |    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     |    \--- org.objenesis:objenesis:2.1
     \--- org.assertj:assertj-core:3.3.0
Run Code Online (Sandbox Code Playgroud)

如果我将弹簧启动插件取出等式,事情就会按预期工作:

# ./gradlew :main:dependencies --configuration=testCompile

testCompile - Compile classpath for source set 'test'.
+--- project :test-utils
     +--- org.springframework:spring-core:4.2.4.RELEASE (*)
     +--- org.springframework:spring-test:4.2.4.RELEASE
     |    \--- org.springframework:spring-core:4.2.4.RELEASE (*)
     +--- junit:junit:4.12
     +--- org.mockito:mockito-core:2.0.41-beta
     |    +--- net.bytebuddy:byte-buddy:1.0.2
     |    \--- org.objenesis:objenesis:2.1
     \--- org.assertj:assertj-core:3.3.0
Run Code Online (Sandbox Code Playgroud)

什么是spring boot插件正在做什么,我怎么能告诉它不要?

And*_*son 6

您的main项目应用了 Spring Boot 的插件,因此它使用 Spring Boot 的依赖项管理。这意味着默认情况下,它将使用 Spring Boot 的首选 Mockito 版本,而不管test-utils.

文档中所述,您可以通过设置相关属性来覆盖 Spring Boot 管理的依赖项的版本。对于 Mockito,该属性是mockito.version。将以下内容添加到您的main项目中:

ext['mockito.version'] = '2.0.41-beta'
Run Code Online (Sandbox Code Playgroud)