簡體   English   中英

Spring Boot Gradle插件強制Mockito版本

[英]Spring boot gradle plugin forces Mockito version

我大致有以下設置:

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'
}

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')
}

但是由於某種原因,似乎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

如果我將spring boot插件從方程式中剔除,事情將按預期進行:

# ./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

spring boot插件到底在做什么,我該如何做呢?

您的main項目已應用了Spring Boot的插件,因此它正在使用Spring Boot的依賴項管理。 這意味着,默認情況下,它將使用Spring Boot的Mockito的首選版本,而不管test-utils中指定的版本如何。

文檔中所述 ,您可以通過設置相關屬性來覆蓋Spring Boot管理的依賴項版本。 對於Mockito來說,該屬性是mockito.version 將以下內容添加到您的main項目中:

ext['mockito.version'] = '2.0.41-beta'

我認為您可以使用配置設置來強制版本

    allprojects {

        configurations.all {
            resolutionStrategy { rs ->
                rs.force "org.mockito:mockito-core:2.0.41-beta"
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM