我怎么能将pom xml转换为sbt依赖项?

时间:2013-03-15 10:46:08

标签: sbt pom.xml

我有一些项目将所有依赖项存储在 pom.xml 文件中。

如何从内部检索依赖项,以便我可以轻松地将它们放到使用sbt的项目中?

复制粘贴所有这些只是耗时......

5 个答案:

答案 0 :(得分:20)

此scala脚本从命令行运行,负责处理,将pom.xml文件转换为打印在屏幕上的sbt依赖项。然后,您只需为每个pom.xml文件复制一次粘贴。

注意:pom.xml必须与脚本位于同一文件夹中。然后从命令行执行: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
});

答案 1 :(得分:7)

我已经增强了George Pligor的答案(并修复了一些错误),因此创建了一个包含build.sbt依赖项的完整pom.xml文件。要将Maven pom.xml转换为build.sbt

  1. 将此代码放入PomToSbt.scala
  2. 旁边名为pom.xml的文件中
  3. 输入scala PomToSbt.scala > build.sbt
  4. 将提取pom.xml的相关性并将其放入完整的build.sbt文件中。
  5. 以下是代码:

    import scala.xml._
    
    val lines = (XML.load("pom.xml") \\ "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""""
      }
    
      s"""  "$groupId" %% "$artifactId" % "$version"$scope2"""
    }
    
    val buildSbt = io.Source.fromURL("https://raw.githubusercontent.com/mslinn/sbtTemplate/master/build.sbt").mkString
    val libText = "libraryDependencies ++= Seq("
    val buildSbt2 = buildSbt.replace(libText, libText + lines.mkString("\n", ",\n", ""))
    println(buildSbt2)
    

    I made a gist;如果需要更新,我会在那里进行更新。

答案 2 :(得分:1)

blog entry解释了一种可行的方法。有一个注释指向一个处理更复杂案件的插件。

答案 3 :(得分:1)

迈克,这里是至少与Scala 11一起使用的代码:

import scala.xml._

//build.sbt file
val lines = (XML.load("pom.xml") \\ "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""""
  }

  s"""  "$groupId" %% "$artifactId" % "$version"$scope2"""
}

val buildSbt: String = io.Source.fromURL("https://raw.githubusercontent.com/mslinn/sbtTemplate/master/build.sbt").mkString

val libText = "libraryDependencies ++= Seq\\("
val buildSbt2 = buildSbt.replaceFirst(libText, libText + lines.mkString("\n", ",\n", ""))
println(buildSbt2)

答案 4 :(得分:0)

import scala.xml._
// To convert a Maven pom.xml to build.sbt:
// 1) Place this code into a file called PomToSbt.scala next to pom.xml
// 2) Type scala PomtoSbt.scala > build.sbt
// The dependencies from pom.xml will be extracted and place into a complete build.sbt file
// Because most pom.xml files only refernence non-Scala dependencies, I did not use %%
val lines = (XML.load("pom.xml") \\ "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""""
  }

  s"""  "$groupId" %  "$artifactId" % "$version"$scope2"""
}

val buildSbt = io.Source.fromURL("https://raw.githubusercontent.com/mslinn/sbtTemplate/master/build.sbt").mkString
val libText = "libraryDependencies ++= Seq("
val buildSbt2 = buildSbt.replace(libText, libText + lines.mkString("\n", ",\n", ""))
println(buildSbt2)