Skip to content

Instantly share code, notes, and snippets.

@MaxNevermind
Last active July 1, 2018 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxNevermind/e02ed8c185419372874e560c5a489193 to your computer and use it in GitHub Desktop.
Save MaxNevermind/e02ed8c185419372874e560c5a489193 to your computer and use it in GitHub Desktop.
// You need Maven installed to run it.
lazy val mavenDependencyTree = taskKey[Unit]("Prints a Maven dependency tree")
mavenDependencyTree := {
val scalaReleaseSuffix = "_" + scalaVersion.value.split('.').take(2).mkString(".")
val pomXml =
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<dependencies>
{
libraryDependencies.value.map(moduleId => {
val suffix = moduleId.crossVersion match {
case binary: sbt.librarymanagement.Binary => scalaReleaseSuffix
case _ => ""
}
<dependency>
<groupId>{moduleId.organization}</groupId>
<artifactId>{moduleId.name + suffix}</artifactId>
<version>{moduleId.revision}</version>
</dependency>
})
}
</dependencies>
</project>
val printer = new scala.xml.PrettyPrinter(160, 2)
val pomString = printer.format(pomXml)
val pomPath = java.nio.file.Files.createTempFile("", ".xml").toString
val pw = new java.io.PrintWriter(new File(pomPath))
pw.write(pomString)
pw.close()
println(s"Formed pom file: $pomPath")
import sys.process._
s"mvn -f $pomPath dependency:tree".!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment