Skip to content

Instantly share code, notes, and snippets.

@pk11
Created December 14, 2012 16:25
Show Gist options
  • Save pk11/4286662 to your computer and use it in GitHub Desktop.
Save pk11/4286662 to your computer and use it in GitHub Desktop.
mvn2sbt - generates an sbt file based on a simple (java) pom.xml
import scala.xml._
import java.io._
/**
* based on https://gist.github.com/388334
*/
object mvn2sbt {
private def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
val p = new java.io.PrintWriter(f)
try { op(p) } finally { p.close() }
}
def main(args: Array[String]): Unit = {
val prefix = args.headOption.getOrElse("")
val pom = XML.loadString(scala.io.Source.fromFile(prefix + "pom.xml").mkString)
val organization = pom \ "groupId" text
val version = pom \ "version" text
val name = pom \ "artifactId" text
val data: Seq[(String, String, String, Option[String], Boolean)] = (pom \\ "dependency") map { d =>
val groupId = d \ "groupId" text
val artifactId = d \ "artifactId" text
val versionNum = d \ "version" text
val provided: Boolean = (d \ "optional").map(_.text.toBoolean).seq.headOption.getOrElse(false)
val scope: Option[String] = (d \ "scope").map(_.text).seq.headOption
(groupId, artifactId, versionNum, scope, provided)
}
def dep(a: String, g: String, v: String, scope: Option[String], isProvided: Boolean): String = {
val sep = "%"
val provided = if (isProvided) " % \"provided\"" else ""
"""libraryDependencies += "%s" %s "%s" %s "%s" %s %s""" format (g, sep, a, sep, v, scope.map(x => " % " + "\"" + x + "\"").getOrElse(""), provided)
}
val m = data map {
case (g, a, v, scope, provided) => dep(a, g, v, scope, provided)
} mkString ("\n\n")
val buildFile =
"organization := \"" + organization + "\"\n\n" +
"name := \"" + name + "\"\n\n" +
"version := \"" + version + "\"\n\n" +
"javacOptions ++= Seq(\"-source\",\"1.7\",\"-target\",\"1.7\", \"-encoding\", \"UTF-8\")" + "\n\n" +
"""libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M2" % "test" """ + "\n\n" +
"""parallelExecution in Test := false """ + "\n\n" +
m
printToFile(new File(prefix + "build.sbt"))(p => {
p.println(buildFile)
})
println("[Done] "+prefix+"build.sbt is created.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment