19

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

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

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

4

5 回答 5

22

这个从命令行运行的 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
});
于 2013-03-15T10:46:07.667 回答
8

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

  1. 将此代码放入名为PomToSbt.scalanext 的文件中pom.xml
  2. 类型scala PomToSbt.scala > build.sbt
  3. 来自的依赖项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)

我做了一个要点;如果需要任何更新,我会在那里进行。

于 2015-11-24T21:35:27.640 回答
2

迈克,这是至少适用于 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)
于 2016-06-02T20:45:55.430 回答
1

博客条目解释了一种可能的方法。有一条评论指向处理更复杂情况的插件。

于 2013-07-21T21:16:37.837 回答
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)
于 2017-10-23T06:41:46.567 回答