Skip to content

Instantly share code, notes, and snippets.

@lonniev
Last active November 23, 2021 13:53
Show Gist options
  • Save lonniev/55fc0c601fae43cced5fe90619a7fe1e to your computer and use it in GitHub Desktop.
Save lonniev/55fc0c601fae43cced5fe90619a7fe1e to your computer and use it in GitHub Desktop.
A small Groovy script that scans a directory of folders for jar files and extracts POM-like dependency statements out of relevant content
package com.intercax.groovy.jarSniffer
import java.util.jar.*
/**
* @author Lonnie VanZandt fluent version
* @author Ivan Krizsan original idea
*/
// map of manually-resolved jars by their file names
manualMap = [
"activation.jar": [ groupId: "com.sun.activation", artifactId: "javax.activation", version: "1.2.0" ],
"annotation.jar": [ groupId: "com.nomagic", artifactId: "annotation", version: "19.0" ],
"brand.jar": [ groupId: "com.nomagic", artifactId: "brand", version: "19.0" ],
"brand_api.jar": [ groupId: "com.nomagic", artifactId: "brand_api", version: "19.0" ],
"batik.jar": [ groupId: "org.apache.batik", artifactId: "batik", version: "1.7" ],
"EccpressoAll.jar": [ groupId: "com.certicom", artifactId: "EccpressoAll", version: "1.0" ],
"HTMLEditorLight.jar": [ groupId: "sferyx.administration.editors", artifactId: "HTMLEditorLight", version: "1.0" ],
"bridj-0.7.0.jar": [ groupId: "com.nativelibs4java", artifactId: "bridj", version: "0.7.0" ],
"commons-httpclient-3.1.jar": [ groupId: "commons-httpclient", artifactId: "commons-httpclient", version: "3.1" ],
"commons-logging-1.0.4.jar": [ groupId: "commons-logging", artifactId: "commons-logging", version: "1.0.4" ],
"concurrent.jar": [ groupId: "jboss.org", artifactId: "concurrent", version: "5.0.1.GA" ],
"flexlm.jar": [ groupId: "com.macrovision", artifactId: "flexlm", version: "1.0" ],
"jhall.jar": [ groupId: "com.sun", artifactId: "jhall", version: "2.0_03" ],
"jide-action.jar": [ groupId: "com.jidesoft", artifactId: "jide-action", version: "3.7.6" ],
"jide-common.jar": [ groupId: "com.jidesoft", artifactId: "jide-common", version: "3.7.6" ],
"jide-components.jar": [ groupId: "com.jidesoft", artifactId: "jide-components", version: "3.7.6" ],
"jide-charts.jar": [ groupId: "com.jidesoft", artifactId: "jide-charts", version: "3.7.6" ],
"jide-diff.jar": [ groupId: "com.jidesoft", artifactId: "jide-diff", version: "3.7.6" ],
"jide-dock.jar": [ groupId: "com.jidesoft", artifactId: "jide-dock", version: "3.7.6" ],
"jide-editor.jar": [ groupId: "com.jidesoft", artifactId: "jide-editor", version: "3.7.6" ],
"jide-gantt.jar": [ groupId: "com.jidesoft", artifactId: "jide-gantt", version: "3.7.6" ],
"jide-grids.jar": [ groupId: "com.jidesoft", artifactId: "jide-grids", version: "3.7.6" ],
"jide-shortcut.jar": [ groupId: "com.jidesoft", artifactId: "jide-shortcut", version: "3.7.6" ],
"gson-2.2.4.jar": [groupId: "com.google.code.gson", artifactId: "gson", version: "2.2.4"],
"jimi.jar": [ groupId: "com.sun", artifactId: "jimi", version: "1.0" ],
"jna.jar": [ groupId: "com.sun", artifactId: "jna", version: "4.3.0" ],
"jtidy.jar": [ groupId: "net.sf.jtidy", artifactId: "jtidy", version: "r938" ],
"jsr250-api-1.0.jar": [ groupId: "javax.annotation", artifactId: "jsr250-api", version: "1.0" ],
"jsr305.jar": [ groupId: "com.google.code.findbugs", artifactId: "jsr305", version: "3.0.2" ],
"lucene-analyzers-common-7.5.0.jar": [ groupId: "org.apache.lucene", artifactId: "lucene-analyzers-common", version: "7.5.0" ],
"lucene-core-7.5.0.jar": [ groupId: "org.apache.lucene", artifactId: "lucene-core", version: "7.5.0" ],
"lucene-misc-7.5.0.jar": [ groupId: "org.apache.lucene", artifactId: "lucene-misc", version: "7.5.0" ],
"patch.jar": [ groupId: "com.nomagic", artifactId: "patch", version: "1.0" ],
"poi-3.14-20160307.jar": [ groupId: "org.apache.poi", artifactId: "poi", version: "3.14" ],
"poi-ooxml-3.14-20160307.jar": [ groupId: "org.apache.poi", artifactId: "poi-ooxml", version: "3.14" ],
"poi-ooxml-schemas-3.14-20160307.jar": [ groupId: "org.apache.poi", artifactId: "poi-ooxml-schemas", version: "3.14" ],
"tw_common.jar": [ groupId: "com.nomagic", artifactId: "tw_common", version: "19.0" ],
"tw_common_api.jar": [ groupId: "com.nomagic", artifactId: "tw_common_api", version: "19.0" ],
"xalan.jar": [ groupId: "org.apache", artifactId: "xalan", version: "2.7.0" ],
"y.jar": [ groupId: "com.yworks", artifactId: "yfiles", version: "1.0" ],
"md_api.jar": [ groupId: "com.nomagic", artifactId: "md_api", version: "20200314_0350-ae099fab" ],
"md_common_api.jar": [ groupId: "com.nomagic", artifactId: "md_common_api", version: "20200314_0350-ae099fab" ],
"md_common_test.jar": [ groupId: "com.nomagic", artifactId: "md_common_test", version: "20200314_0350-ae099fab" ],
"md_common.jar": [ groupId: "com.nomagic", artifactId: "md_common", version: "20200314_0350-ae099fab" ],
"md.jar": [ groupId: "com.nomagic", artifactId: "md", version: "20200314_0350-ae099fab" ],
"xmlbeans-2.6.0.jar": [ groupId: "org.apache.xmlbeans", artifactId: "xmlbeans", version: "2.6.0" ],
]
new File( "library-version.txt" ).text =
findJars( "/Applications/MagicDraw 19sp4/lib" )
.collect { jar ->
extractPomContents( jar ).collect{ txt ->
processMavenPomFileContents( txt )
}
?: missingPomFileContent( jar )
}
.flatten()
.sort()
.join( "\n" )
println "Done!"
def List findJars( final String somePath ) {
new File( somePath ).listFiles()
.findAll { it.isFile() }
.findAll { it.getName().endsWith(".jar") }
}
def extractPomContents( final File someFile )
{
def asJar = new JarFile( someFile )
asJar.entries()
.findAll{ it -> it.name =~ "pom.xml" }
.collect{ it -> asJar.getInputStream( it ).text }
}
def missingPomFileContent( final File someFile )
{
groupId = manualMap[ someFile.name ]?.groupId ?: someFile.name
artifactId = manualMap[ someFile.name ]?.artifactId ?: someFile.name
version = manualMap[ someFile.name ]?.version ?: someFile.name
"""\
|<dependency>
| <groupId>${groupId}</groupId>
| <artifactId>${artifactId}</artifactId>
| <version>${version}</version>
|</dependency>
""".stripMargin()
}
def processMavenPomFileContents( final String fileContents )
{
def pomXml = new XmlSlurper().parseText( fileContents )
def parentArtifactId = pomXml.parent.artifactId.text()
def parentGroupId = pomXml.parent.groupId.text()
def parentVersion = pomXml.parent.version.text()
def artifactId = pomXml.artifactId.text()
def groupId = pomXml.groupId.text()
def version = pomXml.version.text()
"""\
|<dependency>
| <groupId>${groupId ?: parentGroupId}</groupId>
| <artifactId>${artifactId ?: parentArtifactId}</artifactId>
| <version>${version ?: parentVersion}</version>
|</dependency>
""".stripMargin()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment