Skip to content

Instantly share code, notes, and snippets.

@dbuschman7
Created February 24, 2023 01:52
Show Gist options
  • Save dbuschman7/1bfa13c1e04d1f087ae79b6639cd3eb2 to your computer and use it in GitHub Desktop.
Save dbuschman7/1bfa13c1e04d1f087ae79b6639cd3eb2 to your computer and use it in GitHub Desktop.
Parse Java system properties to extract jars from the class path
//> using scala "2.13"
//> using platform "jvm"
//> using lib "com.rabbitmq:amqp-client:5.16.0"
//> using lib "com.typesafe.play::play-json:2.7.4"
import play.api.libs.json.{Json, OFormat}
object SystemProperties extends App {
val sysProps = new SystemProperties(sys.props.get("java.class.path").getOrElse(""))
Console.println("Java Version : " + sysProps.javaVersion)
val libs = sysProps.jarDependencies()
//
libs.foreach(println)
}
final class SystemProperties(rawData: String) {
private lazy val rawLines: Seq[String] = rawData.split(":").toSeq
private val commonLibSplitKeys: Set[String] = Set("maven2", "jcenter.bintray.com")
def jarDependencies(extraSplitKeys: String*): Set[JarDependency] = {
val allKeys = commonLibSplitKeys ++ extraSplitKeys
rawLines.flatMap { l =>
val key = allKeys.find(l.contains(_)).getOrElse("not.going.to.match")
val jarPath: Option[String] = l.split(key).tail.headOption
// println(s"Raw - $l - $key - JarPath - $jarPath")
jarPath.map(JarDependency.parse)
}.toSet
}
def javaVersion: String = sys.props.get("java.version").getOrElse("unknown")
}
case class JarDependency(group: String, artifact: String, version: String, scalaVersion: Option[String], jar: String)
object JarDependency {
val sVersions: Set[String] = Set("_2.11", "_2.12", "_2.13")
implicit class StringPimps(s: String) {
def notEmpty: Option[String] =
s match {
case "" => None
case _ => Option(s)
}
def notBlank: Option[String] = s.notEmpty.flatMap(_ => s.trim.notEmpty)
}
def parse(in: String): JarDependency = {
def stripVersion(in: String): (String, String) =
sVersions.find { v =>
in.contains(v)
} match {
case None => ("", in)
case Some(sVer) =>
in.indexOf(sVer) match {
case n if n < 1 => ("", in)
case n => (in.substring(n + 1), in.substring(0, n))
}
}
val parts: Array[String] = in.split("/").reverse.filterNot(_.isEmpty)
val jar = parts.head
val version = parts.drop(1).head
val (scalaVersion, artifact) = stripVersion(parts.drop(2).head)
val group = parts.drop(3).toSeq.reverse.mkString(".")
JarDependency(group, artifact, version, scalaVersion.notBlank, jar)
}
implicit val __json: OFormat[JarDependency] = Json.format[JarDependency]
}
@dbuschman7
Copy link
Author

scala-cli run SystemProperties.scala

Compiling project (Scala 2.13.8, JVM)
Compiled project (Scala 2.13.8, JVM)
Java Version : 1.8.0_333
JarDependency(com.rabbitmq,amqp-client,5.16.0,None,amqp-client-5.16.0.jar)
JarDependency(com.fasterxml.jackson.core,jackson-core,2.9.8,None,jackson-core-2.9.8.jar)
JarDependency(org.slf4j,slf4j-api,1.7.36,None,slf4j-api-1.7.36.jar)
JarDependency(org.scala-lang,scala-reflect,2.13.8,None,scala-reflect-2.13.8.jar)
JarDependency(com.typesafe.play,play-json,2.7.4,Some(2.13),play-json_2.13-2.7.4.jar)
JarDependency(com.fasterxml.jackson.datatype,jackson-datatype-jsr310,2.9.8,None,jackson-datatype-jsr310-2.9.8.jar)
JarDependency(joda-time,joda-time,2.10.1,None,joda-time-2.10.1.jar)
JarDependency(com.typesafe.play,play-functional,2.7.4,Some(2.13),play-functional_2.13-2.7.4.jar)
JarDependency(com.fasterxml.jackson.datatype,jackson-datatype-jdk8,2.9.8,None,jackson-datatype-jdk8-2.9.8.jar)
JarDependency(com.fasterxml.jackson.core,jackson-databind,2.9.8,None,jackson-databind-2.9.8.jar)
JarDependency(org.scala-lang,scala-library,2.13.8,None,scala-library-2.13.8.jar)
JarDependency(com.fasterxml.jackson.core,jackson-annotations,2.9.8,None,jackson-annotations-2.9.8.jar)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment