我怎么可以将pom xml转换为sbt依赖关系?
我有一些项目,所有的依赖关系存储在pom.xml文件。我怎么可以将pom xml转换为sbt依赖关系?
如何从内部检索依赖关系,以便我可以轻松地将它们放到使用sbt的项目中?
复制粘贴他们都只是费时..
此阶脚本,从命令行运行,需要照顾的是,转换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
文件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)
I made a gist;如果需要更新,我会让他们在那里。
thx!我添加了使用amm传递pom文件参数的可能性:https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da – 2016-05-30 18:47:07
迈克,这里至少有11 Scala的工作代码:我需要添加 `进口scala.xml._` 使用Scala
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)
2.10.0 – pic 2014-02-23 10:37:47
@George Pligor我改编这一点来创建一个Seq的依赖关系(添加了我自己的bug)。我做了它的Apache 2.0如果有任何意见,请让我知道:) https://github.com/matanster/pomToSbt – matanster 2015-03-02 17:15:07
thx!我添加了使用amm传递pom文件参数的可能性:https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da – 2016-05-30 18:48:27