Last active
August 29, 2015 13:56
-
-
Save nexdrew/8961936 to your computer and use it in GitHub Desktop.
Extracts "sources.jar" files from a local JBoss Maven Repo distribution and copies them to their respective module in a local JBoss EAP/AS distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.attribute.BasicFileAttributes | |
import java.nio.file.* | |
//-- first validate args | |
def exit = { code, msg -> | |
println msg | |
System.exit(code) | |
} | |
if(args.length != 2) { | |
exit(1, "Need source and destination arguments") | |
} | |
def isDir = { d -> return d.exists() && d.isDirectory() } | |
def mavenRepoRoot = new File(args[0]) | |
def jbossHome = new File(args[1]) | |
[mavenRepoRoot, jbossHome].each { | |
if(!isDir(it)) { | |
exit(1, "Argument '$it' is not a valid directory") | |
} | |
} | |
def jbossModules = new File(jbossHome, "modules") | |
if(!isDir(jbossModules)) { | |
exit(1, "Directory '$jbossHome' does not contain a 'modules' sub-directory") | |
} | |
def good = [], copyMap = [:], notFound = [:], copied = [] | |
//-- walk EAP/AS modules and find jars without matching "-sources.jar" | |
//-- btw, Files.walkFileTree is possibly the coolest addition to Java 1.7 | |
Files.walkFileTree(jbossModules.toPath(), new SimpleFileVisitor<Path>() { | |
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { | |
def p = path.toString(), jarName = null, srcName = null, parent = null | |
if(p.endsWith(".jar") && !p.endsWith("-sources.jar")) { | |
jarName = path.getName(path.getNameCount()-1).toString() | |
srcName = jarName[0..-5] + "-sources.jar" | |
parent = path.getParent() | |
if(new File(parent.toFile(), srcName).exists()) { | |
good += jarName | |
} else { | |
copyMap.put(srcName, parent) | |
notFound.put(srcName, jarName) | |
} | |
} | |
return FileVisitResult.CONTINUE | |
} | |
}) | |
//-- if necessary, visit all source jars in maven repo and see if they match needed module | |
//-- on match, attempt copy to respective module dir | |
if(!copyMap.isEmpty()) { | |
Files.walkFileTree(mavenRepoRoot.toPath(), new SimpleFileVisitor<Path>() { | |
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { | |
def file = path.getName(path.getNameCount()-1).toString() | |
if(file.endsWith("-sources.jar")) { | |
if(copyMap.containsKey(file)) { | |
//println "FOUND MATCH: $file" | |
def copyTo = copyMap.remove(file) | |
try { | |
Files.copy(path, new File(copyTo.toFile(), file).toPath()) | |
} catch(e) { | |
e.printStackTrace() | |
return FileVisitResult.CONTINUE | |
} | |
copied += notFound.remove(file) | |
} | |
} | |
return FileVisitResult.CONTINUE | |
} | |
}) | |
} | |
//-- print results - yay! | |
if(!good.isEmpty()) { | |
println "\n--------------------------------------------------------------" | |
println "------------------ these already had source ------------------" | |
println "--------------------------------------------------------------" | |
good.each { println "-- $it" } | |
println "--------------------------------------------------------------" | |
} | |
if(!copied.isEmpty()) { | |
println "\n==============================================================" | |
println "================= source was copied for these ================" | |
println "==============================================================" | |
copied.each { println "== $it" } | |
println "==============================================================" | |
} | |
if(!notFound.isEmpty()) { | |
println "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" | |
println "!!!!!!!!!!!!!!!! no source was found for these !!!!!!!!!!!!!!!" | |
println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" | |
notFound.values().each { println "!! $it" } | |
println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample use: