的路徑列表,下面是一個可行的解決方案,你可能不得不根據你如何配置jaxb2插件來跺腳。
配置一個gmaven-plugin
執行的生命週期(例如,在初始化階段)早期以下列配置運行...
開始用函數來收集參考架構的文件對象(這是一個細化Tim的答案):
def findRefs { f ->
def relPaths = new XmlSlurper().parse(f).depthFirst().findAll {
it.name()=='include'
}*[email protected]*.text()
relPaths.collect { new File(f.absoluteFile.parent + "/" + it).canonicalFile }
}
包裹在一個函數的結果,直到所有的孩子迭代中發現:
def recursiveFindRefs = { schemaFiles ->
def outputs = [] as Set
def inputs = schemaFiles as Queue
// Breadth-first examine all refs in all schema files
while (xsd = inputs.poll()) {
outputs << xsd
findRefs(xsd).each {
if (!outputs.contains(it)) inputs.add(it)
}
}
outputs
}
當你解析Maven項目來確定要做什麼時,真正的魔法就出現了。 首先,找到JAXB插件:
jaxb = project.build.plugins.find { it.artifactId == 'jaxb2-maven-plugin' }
然後,解析插件每次執行(如果有多個)。該代碼假設每個執行設置爲schemaDirectory
,schemaFiles
和staleFile
(即,不使用默認值!),並且不使用schemaListFileName
:
jaxb.executions.each { ex ->
log.info("Processing jaxb execution $ex")
// Extract the schema locations; the configuration is an Xpp3Dom
ex.configuration.children.each { conf ->
switch (conf.name) {
case "schemaDirectory":
schemaDirectory = conf.value
break
case "schemaFiles":
schemaFiles = conf.value.split(/,\s*/)
break
case "staleFile":
staleFile = conf.value
break
}
}
最後,我們可以打開schemaFiles,用我們前面定義的函數解析這些:
def schemaHandles = schemaFiles.collect { new File("${project.basedir}/${schemaDirectory}", it) }
def allSchemaHandles = recursiveFindRefs(schemaHandles)
...,並比較他們的最後修改與舊文件的修改時間相對應的時間, 根據需要取消舊文件的鏈接。
def maxLastModified = allSchemaHandles.collect {
it.lastModified()
}.max()
def staleHandle = new File(staleFile)
if (staleHandle.lastModified() < maxLastModified) {
log.info(" New schemas detected; unlinking $staleFile.")
staleHandle.delete()
}
}
傳播點運算符是必需的,因爲'findAll'返回一個List:'*。@ schemaLocation'。無論如何,謝謝你的代碼片段,效果很好':)'+1! – albciff 2016-03-16 23:06:45