Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I convert a pom xml to sbt dependencies?

Tags:

pom.xml

sbt

I have some projects that have all the dependencies stored inside pom.xml files.

How could I retrieve the dependencies from inside so I could easily place them to a project that uses sbt?

Copy pasting all of them is just time consuming..

like image 510
George Pligoropoulos Avatar asked Mar 15 '13 10:03

George Pligoropoulos


People also ask

How do you save dependencies in POM XML?

For that, you need to run a simple command cmd at the same location where pom. xml is present. The command is mvn dependency:analyze, once you run this command you will see those dependencies which are not correct. That's how you can add and manage dependencies in pom.

What is POM XML dependencies?

POM is an acronym for Project Object Model. The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom.

Where are dependencies downloaded in SBT?

All new SBT versions (after 0.7. x ) by default put the downloaded JARS into the . ivy2 directory in your home directory. If you are using Linux, this is usually /home/<username>/.


1 Answers

This scala script, which runs from command line, takes care of that, converting the pom.xml file to sbt dependencies printed on screen. Then you only need to copy paste once for each pom.xml file.

Note: the pom.xml must be in the same folder as the script. Then from command line you execute: scala scriptname.scala

import scala.xml._

(XML.load("pom.xml") \\ "dependencies") \ "dependency" foreach ((dependency: Node) => {
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("[-\\.]", "_")

print("val %s = \"%s\" %% \"%s\" %% \"%s\"".format(artifactValName, groupId, artifactId, version))
scope match {
  case "" => print("\n")
  case _ => print(" %% \"%s\"\n".format(scope))
}
None
});
like image 138
George Pligoropoulos Avatar answered Sep 28 '22 02:09

George Pligoropoulos