2013-03-15 11 views

risposta

19

Questo script scala, che va da riga di comando, si occupa di questo, la conversione del file pom.xml a SBT dipendenze stampati su schermo. Quindi è sufficiente copiare la pasta una volta per ogni file pom.xml.

Nota: il pom.xml deve essere nella stessa cartella dello script. Poi da linea di comando si esegue: 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

avevo bisogno di aggiungere 'importazione scala.xml._' con scala 2.10.0 – pic

+2

@George Pligor I adattato questo un po 'per creare un Seq di dipendenze (con il mio bug aggiunto). L'ho reso Apache 2.0 se qualche commento me lo faccia sapere :) https://github.com/matanster/pomToSbt – matanster

+0

thx! Ho aggiunto la possibilità di passare l'argomento del file pom usando amm: https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da –

0

Questo blog entry spiega un modo possibile. C'è un commento che punta a un plugin che gestisce casi più complessi.

4

Ho migliorato la risposta di George Pligor (e corretto alcuni bug) per creare un file build.sbt completo che incorpori le dipendenze da pom.xml. Per convertire un Maven pom.xml-build.sbt:

  1. Inserire il codice in un file chiamato PomToSbt.scala accanto al pom.xml
  2. Tipo scala PomToSbt.scala > build.sbt
  3. Le dipendenze da pom.xml saranno estratti e inseriti in un file completo build.sbt.

Ecco il codice:

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; se sono necessari aggiornamenti li farò lì.

+0

thx! Ho aggiunto la possibilità di passare l'argomento del file pom usando amm: https://gist.github.com/dportabella/3512f92a60325d8375e5ceb942b911da –

1

Mike, ecco il codice che funziona con Scala 11 almeno:

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) 
Problemi correlati