这是一种快速而肮脏的方法......
让我们从一个类开始定义一个库:
class LibVersion implements Comparable {
String filename
String library
String version
// Just compare the String versions.
// This is naïve, and should probably be improved
int compareTo( Object other ) {
version <=> other.version
}
String toString() {
"$library: $version"
}
static fromFilename( String s ) {
def matcher = ( s =~ $/.+/(.+?)-([0-9\.]+(?:-.+?){0,1})\.jar/$ )[0]
new LibVersion( filename: s, library: matcher[ 1 ], version: matcher[ 2 ] )
}
}
然后,给定一些测试数据:
def originals = [
'Sample.ear/WEB-INF/lib/org.apache.sling.installer.core-3.3.5-R1242752.jar',
'Sample.ear/WEB-INF/lib/org.apache.sling.installer.factory.configuration-1.0.2.jar',
'Sample.ear/WEB-INF/lib/cq-jcrclustersupport-0.1.6.jar',
'Sample.ear/WEB-INF/lib/com.day.jcr.vault-2.3.24.jar',
]
def replacements = [
'Hotfix.zip/lib/org.apache.sling.installer.core-3.2.6-R12345.jar',
'Hotfiz.zip/lib/org.apache.sling.installer.factory.configuration-2.1.4.jar',
'Hotfiz.zip/lib/cq-jcrclustersupport-5.6.7.jar',
]
我们可以生成原始库的映射,并对替换执行相同的操作:
Map<String,LibVersion> originalVersions = originals.collectEntries {
LibVersion.fromFilename( it ).with { v ->
[ (v.library):v ]
}
}
Map<String,LibVersion> replacementVersions = replacements.collectEntries {
LibVersion.fromFilename( it ).with { v ->
[ (v.library):v ]
}
}
然后,我们可以循环遍历替换文件并打印出文件是新文件(在原始地图中找不到)或替换版本大于原始文件
replacementVersions.each { k, v ->
def orig = originalVersions[ k ]
if( !orig || orig < v ) {
println "Should replace $orig with $v"
}
}
这打印:
Should replace org.apache.sling.installer.factory.configuration: 1.0.2 with org.apache.sling.installer.factory.configuration: 2.1.4
Should replace cq-jcrclustersupport: 0.1.6 with cq-jcrclustersupport: 5.6.7