2013-03-15 92 views

回答

19

此阶脚本,从命令行运行,需要照顾的是,转换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 
}); 
+5

2.10.0 – pic 2014-02-23 10:37:47

+2

@George Pligor我改编这一点来创建一个Seq的依赖关系(添加了我自己的bug)。我做了它的Apache 2.0如果有任何意见,请让我知道:) https://github.com/matanster/pomToSbt – matanster 2015-03-02 17:15:07

+0

thx!我添加了使用amm传递pom文件参数的可能性:https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da – 2016-05-30 18:48:27

0

blog entry解释了一种可能的方式。有一条评论指向处理更复杂情况的插件。

4

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

  1. 将此代码到一个名为下一PomToSbt.scala文件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) 

I made a gist;如果需要更新,我会让他们在那里。

+0

thx!我添加了使用amm传递pom文件参数的可能性:https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da – 2016-05-30 18:47:07

1

迈克,这里至少有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) 
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)