Skip to content

Instantly share code, notes, and snippets.

@dportabella
Last active May 7, 2022 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dportabella/3512f92a60325d8375e5ceb942b911da to your computer and use it in GitHub Desktop.
Save dportabella/3512f92a60325d8375e5ceb942b911da to your computer and use it in GitHub Desktop.
Script to convert Maven dependencies (and exclusions) from a pom.xml to sbt dependencies. Or run it online on http://goo.gl/wnHCjE
#!/usr/bin/env amm
// This script converts Maven dependencies from a pom.xml to sbt dependencies.
// It is based on the answers of George Pligor and Mike Slinn on http://stackoverflow.com/questions/15430346/
// - install https://github.com/lihaoyi/Ammonite
// - make this script executable: chmod +x PomDependenciesToSbt
// - run it with from your shell (e.g bash):
// $ ./PomDependenciesToSbt /path/to/pom.xml
import scala.xml._
import ammonite.ops._
def main(pomPath: String, path: Path = cwd) = {
val lines = (XML.load(pomPath) \\ "dependencies") \ "dependency" map { dependency =>
val groupId = (dependency \ "groupId").text
val artifactId = (dependency \ "artifactId").text
val version = (dependency \ "version").text
val scope = (dependency \ "scope").text
val classifier = (dependency \ "classifier").text
val artifactValName: String = artifactId.replaceAll("[-\\.]", "_")
val scope2 = scope match {
case "" => ""
case _ => s""" % "$scope""""
}
val exclusions = (dependency \ "exclusions" \ "exclusion" map { exclusion =>
val groupId = (exclusion \ "groupId").text
val artifactId = (exclusion \ "artifactId").text
s"""\n exclude("$groupId", "$artifactId")"""
}).mkString
s""" "$groupId" % "$artifactId" % "$version"$scope2$exclusions"""
}
println(lines.mkString("", ",\n", "\n"))
}
@jbyler
Copy link

jbyler commented Apr 18, 2017

This isn't working for me as-is with Ammonite 0.8.2. But adding "@main" on a line above the start of the main method fixes it. Must have been an ammonite change.

@dszakallas
Copy link

I also needed to import ammonite-ops by adding the line

import $ivy.`com.lihaoyi::ammonite-ops:2.4.1`

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