[英]Forcing Gradle to use an earlier dependency version for testing
有沒有辦法構建一個Gradle構建文件來強制測試階段使用早期版本的依賴項而不是用於編譯和打包的依賴項?
我正在嘗試通過HBaseTestingUtility建立一個HBase迷你集群來測試我的項目; 不幸的是,HBaseTestingUtility依賴於舊版本的Guava(14.0.1似乎可以工作),而我的項目的其余部分使用18.0。 以下是我的構建腳本的摘錄,原樣(我也使用了unbroken-dome的Gradle測試集插件來創建兩組獨立的測試):
plugins {
id 'org.unbroken-dome.test-sets' version '1.2.0'
}
apply plugin: 'java'
ext.verGuava = '18.0'
ext.verGuavaTEST = '14.0.1'
testSets {
testUnit { dirName = 'test/unit' }
testIntegration { dirName = 'test/integration' }
}
configurations {
provided
provided {
extendsFrom(compile)
}
testIntConf
testIntConf {
extendsFrom(provided)
resolutionStrategy {
force "com.google.guava:guava:${verGuavaTEST}"
forcedModules = ["com.google.guava:guava:${verGuavaTEST}"]
}
}
}
sourceSets {
main.compileClasspath += configurations.provided
testUnit.compileClasspath += configurations.provided
testUnit.runtimeClasspath += configurations.provided
testIntegration.compileClasspath += configurations.testIntConf
testIntegration.runtimeClasspath += configurations.testIntConf
}
dependencies {
provided "org.apache.hbase:hbase-client:${verHBase}"
provided "org.apache.hbase:hbase-common:${verHBase}"
compile "org.testng:testng:${verTestNG}"
testIntegrationCompile group:'org.apache.hadoop', name:'hadoop-common', version:"${verHadoop}", classifier: 'tests'
testIntegrationCompile group:'org.apache.hbase', name:'hbase-server', version:"${verHBase}"
testIntegrationCompile group:'org.apache.hbase', name:'hbase-server', version:"${verHBase}", classifier: 'tests'
testIntegrationCompile group:'org.apache.hbase', name:'hbase-hadoop-compat', version:"${verHBase}"
testIntegrationCompile group:'org.apache.hbase', name:'hbase-hadoop-compat', version:"${verHBase}", classifier: 'tests'
testIntegrationCompile group:'org.apache.hbase', name:'hbase-hadoop2-compat', version:"${verHBase}"
testIntegrationCompile group:'org.apache.hbase', name:'hbase-hadoop2-compat', version:"${verHBase}", classifier: 'tests'
testIntegrationCompile group:'org.apache.hadoop', name:'hadoop-hdfs', version:"${verHadoop}"
testIntegrationCompile group:'org.apache.hadoop', name:'hadoop-hdfs', version:"${verHadoop}", classifier: 'tests'
}
testUnit {
useTestNG()
logger.info "@@@ Classpath (UNIT testing): ${classpath.getFiles().collect({it.toString()}).inject('\n') {acc, next -> acc + next + '\n'}}"
logger.info "@@@ SystemProps (UNIT testing): ${System.getProperties().collect({it.toString()}).inject('\n') {acc, next -> acc + next + '\n'}}"
}
testIntegration {
useTestNG()
systemProperty "java.net.preferIPv4Stack", "true"
logger.info "@@@ Classpath (INTEGRATION testing): ${classpath.getFiles().collect({it.toString()}).inject('\n') {acc, next -> acc + next + '\n'}}"
logger.info "@@@ SysProps (INTEGRATION testing): ${System.getProperties().collect({it.toString()}).inject('\n') {acc, next -> acc + next + '\n'}}"
}
當我通過這個腳本運行構建時,我得到以下輸出,這似乎表明Guava 14.0.1被添加到testIntegration目標的類路徑中,而不是替換 Guava 18.0:
@@@ Classpath (UNIT testing):
/home/user/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar
@@@ Classpath (INTEGRATION testing):
/home/user/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar
/home/user/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/14.0.1/69e12f4c6aeac392555f1ea86fab82b5e5e31ad4/guava-14.0.1.jar
這種行為可能是預期的; API指示ResolutionStrategy.force(...)
將工件附加到要考慮的列表中。
如何指導Gradle完全從testIntegration目標的類路徑中消除Guava 18.0?
我嘗試更改sourceSets
部分以使用=而不是+ =分配類路徑:
sourceSets {
...
testIntegration.compileClasspath = configurations.testIntConf
testIntegration.runtimeClasspath = configurations.testIntConf
}
就消除Guava 18.0(並保留14.0.1)而言,這具有預期的效果,但由於某種原因,它似乎阻止Gradle檢測testIntegration
源文件的位置,因此永遠不會編譯或執行testIntegration
測試。
我還嘗試了一些從繼承配置中清除Guava的變體,如下所示:
configurations {
provided
provided {
extendsFrom(compile)
}
testIntConf
testIntConf {
extendsFrom(provided.copy {
exclude group:"com.google.guava", module:"guava"
})
resolutionStrategy {
force "com.google.guava:guava:${verGuavaTEST}"
forcedModules = ["com.google.guava:guava:${verGuavaTEST}"]
}
}
}
以上(以及我嘗試的所有其他變體)確實成功地消除了類路徑中Guava工件的重復,但它們似乎使resolutionStrategy
無效,因為即使在testIntegration
,已解析的工件也始終是較新的版本(18.0)。
雖然也許不是最優雅的解決方案(和我仍然有興趣在一個更清潔的一個,如果存在的話),這似乎工作的方法是創建一個完全獨立的配置僅包含新的番石榴依賴,然后用FileCollection#minus
從testIntegration目標本身的類路徑中減去該配置。
首先,創建配置; 我稱之為guava
,因為它的唯一目的是包含我想要從testIntegration
類路徑中排除的Guava 18.0工件:
configurations {
guava
...
}
(配置應包含在隔離的番石榴偽影,因此它不應該extendsFrom
任何其他配置)。
其次,將要從類路徑中排除的工件添加到新創建的配置的依賴關系列表中。 在這種情況下,我知道主要配置將com.google.guava:guava解析為版本18.0,因此我將該版本的Guava添加到guava
配置:
dependencies {
guava group:'com.google.guava', name:'guava', version:"${verGuava}"
...
}
第三,在Gradle目標中的classpath
上調用FileCollection#minus
,其中要強制使用早期版本的依賴項,以便排除較新的依賴項。 我上面的testIntegration
塊因此被轉換:
testIntegration {
classpath = classpath.minus(configurations.guava);
...
}
因此,在Gradle構建中如何工作的快速摘要是:
compile
) extendsFrom
的配置,然后使用ResolutionStrategy#force
ResolutionStrategy#forcedModules
和ResolutionStrategy#forcedModules
將早期版本的依賴項添加到該配置的依賴項列表中。 sourceSets
,將步驟1中創建的配置附加到您希望強制使用早期依賴項版本的目標的compileClasspath
和runtimeClasspath
。 dependencies
,添加較新版本的工件作為步驟2中創建的配置的依賴項。 classpath
現在包含默認依賴項和早期版本的並集。要刪除較新版本,請將classpath
設置為classpath.minus(newerDependencyConfiguration)
,其中newerDependencyConfiguration
是在步驟2中創建的那個。 我面臨同樣的問題,但更簡單的版本。 因為我沒有區分集成測試和單元測試。 使用它似乎給測試正確的番石榴依賴性
configurations {
all{
resolutionStrategy {
force 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
}
}
testCompile{
resolutionStrategy {
force 'com.google.guava:guava:14.0.1'
}
}
}
dependencies {
testCompile group: 'org.apache.hbase', name: 'hbase-testing-util', version: '1.2.4'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.