diff --git a/library/src/main/scala/giter8/MavenHelper.scala b/library/src/main/scala/giter8/MavenHelper.scala index b618eba5..4a55fad1 100644 --- a/library/src/main/scala/giter8/MavenHelper.scala +++ b/library/src/main/scala/giter8/MavenHelper.scala @@ -24,8 +24,10 @@ import org.apache.http.impl.client.DefaultHttpClient // http://hc.apache.org/httpcomponents-client-4.2.x/httpclient/apidocs/ trait MavenHelper { - def fromMaven(org: String, name: String)(process: (String, NodeSeq) => VersionE): VersionE = { - val loc = s"https://repo1.maven.org/maven2/${org.replace('.', '/')}/$name/maven-metadata.xml" + def fromMaven(org: String, name: String, getVersions: Boolean)(process: (String, NodeSeq) => VersionE): VersionE = { + val search = "https://search.maven.org/solrsearch/select" + val loc = s"""$search?q=g:%22$org%22+AND+a:%22$name%22&rows=10&wt=xml${if (getVersions) "&core=gav" else ""}""" + withHttp(loc) { response => val status = response.getStatusLine status.getStatusCode match { diff --git a/library/src/main/scala/maven.scala b/library/src/main/scala/maven.scala index 45413aca..efaf669a 100644 --- a/library/src/main/scala/maven.scala +++ b/library/src/main/scala/maven.scala @@ -48,30 +48,28 @@ object Maven extends JavaTokenParsers with MavenHelper { private def latestVersion( org: String, name: String - ): VersionE = fromMaven(org, name)(findLatestVersion) + ): VersionE = fromMaven(org, name, false)(findLatestVersion) private def latestStableVersion( org: String, name: String - ): VersionE = fromMaven(org, name)(findLatestStableVersion) + ): VersionE = fromMaven(org, name, true)(findLatestStableVersion) private[giter8] def findLatestVersion(loc: String, elem: NodeSeq): VersionE = { - (elem \ "versioning" \ "latest").headOption - .map(_.text) + (elem \ "result" \ "doc" \ "str") + .collectFirst { case x if x.attribute("name").map(_.text) == Some("latestVersion") => x.text } .toRight(s"Found metadata at $loc but can't extract latest version") } private[giter8] def findLatestStableVersion(loc: String, elem: NodeSeq)( implicit svo: Ordering[VersionNumber]): VersionE = { - (elem \ "versioning" \ "latest").headOption.map(_.text) match { - case Some(VersionNumber.Stable(version)) => Right(version.toString) - case _ => - val versions = (elem \ "versioning" \ "versions" \ "version").map(_.text) - val validVersions = versions.collect { - case VersionNumber.Stable(version) => version - } - validVersions.sorted.headOption.map(_.toString).toRight(s"Could not find latest stable version at $loc") + val versions = (elem \ "result" \ "doc" \ "str").collect { + case x if x.attribute("name").map(_.text) == Some("v") => x.text } + val validVersions = versions.collect { + case VersionNumber.Stable(version) => version + } + validVersions.sorted.headOption.map(_.toString).toRight(s"Could not find latest stable version at $loc") } def lsIsGone(artifact: String): VersionE = diff --git a/library/src/test/scala/giter8/MavenTest.scala b/library/src/test/scala/giter8/MavenTest.scala index d27380b6..0cf5cc87 100644 --- a/library/src/test/scala/giter8/MavenTest.scala +++ b/library/src/test/scala/giter8/MavenTest.scala @@ -23,27 +23,34 @@ final class MavenTest extends FlatSpec with Matchers with EitherValues { "Maven" should "resolve the latest version when found" in { val xml = - - org.scala-lang - scala-library - - 2.12.1 - 2.12.1 - - + + + + org.scala-lang + org.scala-lang:scala-library + 2.13.0-M5 + + + - val loc = "https://repo1.maven.org/maven2/org/scala-lang/scala-libary/maven-metadata.xml" - Maven.findLatestVersion(loc, xml).right.value should be("2.12.1") + val loc = + "https://search.maven.org/solrsearch/select?q=g:%22org.scala-lang%22+AND+a:%22scala-library%22&rows=20&wt=xml" + Maven.findLatestVersion(loc, xml).right.value should be("2.13.0-M5") } it should "return an error when the latest version is not found" in { val xml = - - org.scala-lang - scala-library - + + + + org.scala-lang + org.scala-lang:scala-library + + + - val loc = "https://repo1.maven.org/maven2/org/scala-lang/scala-libary/maven-metadata.xml" + val loc = + "https://search.maven.org/solrsearch/select?q=g:%22org.scala-lang%22+AND+a:%22scala-library%22&rows=20&wt=xml" Maven.findLatestVersion(loc, xml).left.value should be( s"Found metadata at $loc but can't extract latest version" ) @@ -51,70 +58,153 @@ final class MavenTest extends FlatSpec with Matchers with EitherValues { it should "resolve the latest stable version when the latest version is not stable" in { val xml = - - org.scalatest - scalatest_2.12 - - 3.2.0-SNAP4 - 3.2.0-SNAP4 - - 3.0.0 - 3.0.1 - 3.2.0-SNAP1 - 3.2.0-SNAP2 - 3.2.0-SNAP3 - 3.2.0-SNAP4 - - 20170217001002 - - + + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.0.6-SNAP2 + 3.0.6-SNAP2 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.0.6-SNAP1 + 3.0.6-SNAP1 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.0.5-M1 + 3.0.5-M1 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.0.5 + 3.0.5 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.2.0-SNAP10 + 3.2.0-SNAP10 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.1.0-SNAP6 + 3.1.0-SNAP6 + + + scalatest_2.12 + org.scalatest + org.scalatest:scalatest_2.12:3.0.4 + 3.0.4 + + + - val loc = "https://repo1.maven.org/maven2/org/scalatest/scalatest_2.12/maven-metadata.xml" - Maven.findLatestStableVersion(loc, xml).right.value should be("3.0.1") + val loc = + "https://search.maven.org/solrsearch/select?q=g:%22org.scalatest%22+AND+a:%22scalatest_2.12%22&rows=20&wt=xml&core=gav" + Maven.findLatestStableVersion(loc, xml).right.value should be("3.0.5") } it should "return the latest version if the latest version is stable" in { val xml = - - org.scalacheck - scalacheck_2.12 - - 1.13.5 - 1.13.5 - - 1.11.6 - 1.12.6 - 1.13.4 - 1.13.5 - - 20170313115803 - - + + + + scalacheck_2.12 + org.scalacheck + org.scalacheck:scalacheck_2.12:1.14.0 + 1.14.0 + + + scalacheck_2.12 + org.scalacheck + org.scalacheck:scalacheck_2.12:1.13.5 + 1.13.5 + + + scalacheck_2.12 + org.scalacheck + org.scalacheck:scalacheck_2.12:1.12.6 + 1.12.6 + + + scalacheck_2.12 + org.scalacheck + org.scalacheck:scalacheck_2.12:1.13.4 + 1.13.4 + + + scalacheck_2.12 + org.scalacheck + org.scalacheck:scalacheck_2.12:1.11.6 + jar + 1.11.6 + + + - val loc = "https://repo1.maven.org/maven2/org/scalacheck/scalacheck_2.12/maven-metadata.xml" - Maven.findLatestStableVersion(loc, xml).right.value should be("1.13.5") + val loc = + "https://search.maven.org/solrsearch/select?q=g:%22org.scalacheck%22+AND+a:%22scalacheck_2.12%22&core=gav&rows=20&wt=xml" + Maven.findLatestStableVersion(loc, xml).right.value should be("1.14.0") } it should "return an error if the latest stable version is not found" in { val xml = - - org.scalaz - scalaz-concurrent_2.12 - - 7.3.0-M10 - 7.3.0-M10 - - 7.3.0-M6 - 7.3.0-M7 - 7.3.0-M8 - 7.3.0-M9 - 7.3.0-M10 - - 20170319035300 - - + + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M24 + 7.3.0-M24 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M23 + 7.3.0-M23 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M22 + 7.3.0-M22 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M21 + 7.3.0-M21 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M20 + 7.3.0-M20 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M19 + 7.3.0-M19 + + + scalaz-concurrent_2.12 + org.scalaz + org.scalaz:scalaz-concurrent_2.12:7.3.0-M18 + 1509498138000 + 7.3.0-M18 + + + - val loc = "https://repo1.maven.org/maven2/org/scalaz/scalaz-concurrent_2.12/maven-metadata.xml" + val loc = + "https://search.maven.org/solrsearch/select?q=g:%22org.scalaz%22+AND+a:%22scalaz-concurrent_2.12%22&rows=20&wt=xml&core=gav" Maven.findLatestStableVersion(loc, xml).left.value should be( s"Could not find latest stable version at $loc" )