diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2016-04-02 13:02:23 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-04-02 13:02:23 -0400 |
commit | 47c619c5cec7964b1244a20a5cd379e7efefa197 (patch) | |
tree | 288bc5f687ff58c8fad3baac7e37c8b679b9b404 | |
parent | ae0f718c39b1dadf706944b558f0ce5457461b97 (diff) | |
download | cbt-47c619c5cec7964b1244a20a5cd379e7efefa197.tar.gz cbt-47c619c5cec7964b1244a20a5cd379e7efefa197.tar.bz2 cbt-47c619c5cec7964b1244a20a5cd379e7efefa197.zip |
Improve cbt's maven resolver to handle variable substitutions for pom xml tag contents
-rw-r--r-- | stage1/resolver.scala | 56 | ||||
-rw-r--r-- | test/simple/Main.scala | 2 | ||||
-rw-r--r-- | test/simple/build/build.scala | 6 |
3 files changed, 47 insertions, 17 deletions
diff --git a/stage1/resolver.scala b/stage1/resolver.scala index 1dd4d6c..703ac18 100644 --- a/stage1/resolver.scala +++ b/stage1/resolver.scala @@ -259,6 +259,12 @@ object Classifier{ case class JavaDependency( groupId: String, artifactId: String, version: String, classifier: Classifier = Classifier.none )(implicit val logger: Logger) extends ArtifactInfo{ + assert(groupId != "", toString) + assert(artifactId != "", toString) + assert(version != "", toString) + assert(groupId != null, toString) + assert(artifactId != null, toString) + assert(version != null, toString) override def needsUpdate = false override def canBeCached = true @@ -310,7 +316,7 @@ case class JavaDependency( // ========== pom traversal ========== - lazy val pomParents: Seq[JavaDependency] = { + lazy val transitivePom: Seq[JavaDependency] = { (pomXml \ "parent").collect{ case parent => JavaDependency( @@ -318,12 +324,12 @@ case class JavaDependency( (parent \ "artifactId").text, (parent \ "version").text )(logger) - } + }.flatMap(_.transitivePom) :+ this } lazy val properties: Map[String, String] = ( - pomParents.flatMap(_.properties) ++ { - val props = (pomXml \ "properties").flatMap(_.child).map{ + transitivePom.flatMap{ d => + val props = (d.pomXml \ "properties").flatMap(_.child).map{ tag => tag.label -> tag.text } logger.pom(s"Found properties in $pom: $props") @@ -331,17 +337,15 @@ case class JavaDependency( } ).toMap - lazy val dependencyVersions: Map[(String,String), String] = - pomParents.flatMap( + lazy val dependencyVersions: Map[String, (String,String)] = + transitivePom.flatMap( p => - p.dependencyVersions - ++ (p.pomXml \ "dependencyManagement" \ "dependencies" \ "dependency").map{ xml => val groupId = p.lookup(xml,_ \ "groupId").get val artifactId = p.lookup(xml,_ \ "artifactId").get val version = p.lookup(xml,_ \ "version").get - (groupId, artifactId) -> version + artifactId -> (groupId, version) } ).toMap @@ -349,14 +353,27 @@ case class JavaDependency( if(classifier == Classifier.sources) Seq() else (pomXml \ "dependencies" \ "dependency").collect{ case xml if (xml \ "scope").text == "" && (xml \ "optional").text != "true" => - val groupId = lookup(xml,_ \ "groupId").get val artifactId = lookup(xml,_ \ "artifactId").get + val groupId = + lookup(xml,_ \ "groupId").getOrElse( + dependencyVersions + .get(artifactId).map(_._1) + .getOrElse( + throw new Exception(s"$artifactId not found in \n$dependencyVersions") + ) + ) + val version = + lookup(xml,_ \ "version").getOrElse( + dependencyVersions + .get(artifactId).map(_._2) + .getOrElse( + throw new Exception(s"$artifactId not found in \n$dependencyVersions") + ) + ) JavaDependency( - groupId, - artifactId, - lookup(xml,_ \ "version").getOrElse( dependencyVersions(groupId, artifactId) ), + groupId, artifactId, version, Classifier( Some( (xml \ "classifier").text ).filterNot(_ == "").filterNot(_ == null) ) - )(logger) + ) }.toVector } def lookup( xml: Node, accessor: Node => NodeSeq ): Option[String] = { @@ -365,7 +382,16 @@ case class JavaDependency( accessor(xml).headOption.flatMap{v => //println("found: "++v.text) v.text match { - case Substitution(path) => Option(properties(path)) + case Substitution(path) => Option( + properties.get(path).orElse( + transitivePom.reverse.flatMap{ d => + Some(path.split("\\.").toList).collect{ + case "project" :: path => + path.foldLeft(d.pomXml:NodeSeq){ case (xml,tag) => xml \ tag }.text + }.filter(_ != "") + }.headOption + ) + .getOrElse( throw new Exception(s"Can't find $path in \n$properties.\n\npomParents: $transitivePom\n\n pomXml:\n$pomXml" ))) //println("lookup "++path ++ ": "++(pomXml\path).text) case value => Option(value) } diff --git a/test/simple/Main.scala b/test/simple/Main.scala index 43542ff..1c423ca 100644 --- a/test/simple/Main.scala +++ b/test/simple/Main.scala @@ -1,4 +1,6 @@ import ai.x.diff +import org.eclipse.jgit.lib.Ref +import com.spotify.missinglink.ArtifactLoader object Main extends App{ println(diff.DiffShow.diff("a","b")) } diff --git a/test/simple/build/build.scala b/test/simple/build/build.scala index f3efc19..d3887b3 100644 --- a/test/simple/build/build.scala +++ b/test/simple/build/build.scala @@ -6,7 +6,9 @@ class Build(context: cbt.Context) extends BasicBuild(context){ ScalaDependency("com.typesafe.play", "play-json", "2.4.4"), JavaDependency("joda-time", "joda-time", "2.9.2"), GitDependency("https://github.com/xdotai/diff.git", "2e275642041006ff39efde22da7742c2e9a0f63f"), - // the below tests pom inheritance with dependencyManagement and variable substitution - JavaDependency("org.eclipse.jgit", "org.eclipse.jgit", "4.2.0.201601211800-r") + // the below tests pom inheritance with dependencyManagement and variable substitution for pom properties + JavaDependency("org.eclipse.jgit", "org.eclipse.jgit", "4.2.0.201601211800-r"), + // the below tests pom inheritance with variable substitution for pom xml tag contents + JavaDependency("com.spotify", "missinglink-core", "0.1.1") ) ++ super.dependencies } |