我有一些项目将所有依赖项都存储在pom.xml文件中。
如何从内部检索依赖项,以便可以轻松地将它们放置到使用 sbt 的项目中?
复制粘贴所有这些只是耗时..
这个从命令行运行的 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
});
我已经增强了 George Pligor 的答案(并修复了一些错误),因此build.sbt
创建了一个完整的文件,其中包含来自pom.xml
. 要将 Maven 转换pom.xml
为build.sbt
:
PomToSbt.scala
next 的文件中pom.xml
scala PomToSbt.scala > build.sbt
pom.xml
将被提取并放入一个完整的build.sbt
文件中。这是代码:
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)
我做了一个要点;如果需要任何更新,我会在那里进行。
迈克,这是至少适用于 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)
此博客条目解释了一种可能的方法。有一条评论指向处理更复杂情况的插件。
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)