Created
August 10, 2011 15:34
-
-
Save Riduidel/1137165 to your computer and use it in GitHub Desktop.
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.io.*; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.logging.*; | |
import groovy.lang.Grab; | |
import java.util.jar.*; | |
import java.util.zip.*; | |
public class JarClasspathExport { | |
private boolean createEntryLine | |
private Properties mappings = new Properties() | |
private String defaultGroupId = "NOGROUP" | |
private String defaultVersion = "NOVERSION" | |
/** | |
* This method is courtesy of http://daniel.gredler.net/2007/04/13/mavenizing-oc4j-client-libraries-with-groovy/ | |
*/ | |
def getClassPath(file) { | |
JarFile jar = new JarFile(file) | |
if(!jar.manifest) return null | |
String cp = jar.manifest.mainAttributes.getValue("Class-Path") | |
if(!cp) return null | |
return cp.split("\\s+") | |
} | |
def toGAV(String jarPath) { | |
def returned = [:] | |
def matching = jarPath=~/((.*)\/)?(.*?)(-([0-9]+(\.[0-9]+)*))?((?:-sources)?(?:-src)?)?(-tests)?(-bin)?\.jar/ | |
def groupId = matching[0][2] | |
returned.groupId = groupId!=null ? groupId : defaultGroupId | |
returned.artifactId = matching[0][3] | |
def version = matching[0][5] | |
returned.version = version!=null ? version : defaultVersion | |
if(matching[0][7]!=null && !matching[0][7].equals("")) { | |
println "sources qualifier for ${jarPath} is \""+matching[0][7]+"\"" | |
returned.qualifier = "sources" | |
} | |
if(matching[0][8]!=null && !matching[0][8].equals("")) { | |
returned.qualifier = "tests" | |
} | |
String serialized = returned.groupId+":"+returned.artifactId+":"+returned.version | |
if(mappings.containsKey(serialized)) { | |
String out = mappings.getProperty(serialized) | |
def outMatch = out =~/([^\:]*)\:([^\:]*)\:([^\:]*)/ | |
returned.groupId = outMatch[0][1] | |
returned.artifactId = outMatch[0][2] | |
returned.version = outMatch[0][3] | |
} else { | |
if(createEntryLine) | |
println "\nproperties line SHOULD be\n<entry key=\"$serialized\">$serialized</entry>\n" | |
} | |
def old = serialized | |
serialized = returned.groupId+":"+returned.artifactId+":"+returned.version | |
if(createEntryLine) { | |
if(!old.equals(serialized)) { | |
println "properties line is <entry key=\"$old\">$serialized</entry>" | |
} | |
} | |
return returned | |
} | |
/** | |
* Create an array of dependencies Map from class path jars | |
*/ | |
def createDependenciesFrom(def classpathJars) { | |
def returned = [:] | |
classpathJars.each { jarPath -> | |
returned[jarPath] = toGAV(jarPath) | |
} | |
return returned | |
} | |
def buildPom(File jarPath, File pomPath) { | |
def gav = toGAV(jarPath.name) | |
def writer = new StringWriter() | |
def builder = new groovy.xml.MarkupBuilder(writer) | |
builder.project(xmlns:'http://maven.apache.org/POM/4.0.0', | |
'xmlns:xsi':'http://www.w3.org/2001/XMLSchema-instance', | |
'xsi:schemaLocation':'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd') { | |
groupId(gav.groupId) | |
artifactId(gav.artifactId) | |
version(gav.version) | |
dependencies { | |
createDependenciesFrom(getClassPath(jarPath)).each { dependencyJarPath, dependencyGav -> | |
dependency { | |
mkp.comment dependencyJarPath | |
groupId(dependencyGav.groupId) | |
artifactId(dependencyGav.artifactId) | |
version(dependencyGav.version) | |
if(dependencyGav.containsKey("qualifier")) { | |
qualifier(dependencyGav.qualifier) | |
} | |
} | |
} | |
} | |
} | |
pomPath.parentFile.mkdirs(); | |
if(pomPath.exists()) { | |
pomPath.delete() | |
} | |
pomPath.write(writer.toString(), 'UTF-8') | |
println "pom of "+jarPath+" can be found in "+pomPath | |
} | |
def processFolder(File toExamine) { | |
toExamine.eachFile { file -> | |
if(file.name.endsWith(".jar")) { | |
def pomPath = new File(file.getAbsolutePath()+".pom.xml") | |
buildPom(file, pomPath) | |
} | |
} | |
} | |
/** | |
* Main method, doing the grab stuff and providing usual infos | |
*/ | |
public static void main(args) throws Exception { | |
JarClasspathExport app = new JarClasspathExport(); | |
try { | |
def cli = new CliBuilder(usage:'groovy create-dependencies.groovy ') | |
cli.h(longOpt: 'help', 'provides full help and usage information') | |
cli.j(longOpt: 'jar', 'Path of JAR to examine', args:1, required:false) | |
cli.f(longOpt: 'folder', 'Path of folder in which all ajrs will recursively have their pom built', args:1, required:false) | |
cli.p(longOpt: 'pom', 'Output POM to create', args:1, required:false) | |
cli.m(longOpt: 'mappings', 'Some various mappings for input gavs to output gavs', args:1, required:false) | |
cli.c(longOpt: 'create', 'Create an entry lime for the mappings property file for each artifact', args:0, required:false) | |
def opt = cli.parse(args); | |
if(!opt) { | |
cli.usage | |
} else { | |
if(opt.h) | |
cli.usage(); | |
if(opt.m) { | |
def file = new File(opt.m) | |
println "reading mappings of "+file | |
Properties properties = new Properties(); | |
FileInputStream input = new FileInputStream(file); | |
// To avoid issues due to ":" being interpreted as key to value in .properties files | |
properties.loadFromXML(input); | |
input.close(); | |
app.mappings = properties | |
} | |
app.createEntryLine = opt.c | |
if(opt.j) { | |
def jarPath = new File(opt.j) | |
def pomPath = new File(jarPath.getAbsolutePath()+".pom.xml") | |
if(opt.p) | |
pomPath = new File(opt.p) | |
app.buildPom(jarPath, pomPath) | |
} else if(opt.f) { | |
app.processFolder(new File(opt.f)) | |
} | |
} | |
} catch(Throwable t) { | |
println "something went wrong with ${app.toString()}" | |
throw t | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment