這裏是這樣做的一個快速和還挺骯髒的方式...
讓我們開始一個類定義庫:
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
非常感謝Tim。它工作很棒,我從中學到很多東西。我現在必須檢查compareTo(...)是否適用於我的所有罐子。我已經掌握了方向,可以解決它,以防萬一失敗。再次感謝!! – Arham