From 348cc15dbaa92bd6c16b266696d22f6212f151ba Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Sat, 22 Apr 2017 01:51:21 +0200 Subject: [PATCH] Fix #506: Support Maven snapshots without `snapshotVersions` --- .../coursier/maven/MavenRepository.scala | 5 +- .../src/main/scala/coursier/maven/Pom.scala | 18 ++++++- .../0.1.0-SNAPSHOT/maven-metadata.xml | 13 +++++ ...ecial-0.1.0-20170421.034426-82-sources.jar | 1 + ...pshot-special-0.1.0-20170421.034426-82.jar | 1 + ...pshot-special-0.1.0-20170421.034426-82.pom | 9 ++++ ...t-special-0.1.0-20170421.034426-82.pom.md5 | 1 + ...-special-0.1.0-20170421.034426-82.pom.sha1 | 1 + .../test/scala/coursier/test/MavenTests.scala | 53 +++++++++++++++++++ 9 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/maven-metadata.xml create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.jar create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.md5 create mode 100644 tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.sha1 create mode 100644 tests/jvm/src/test/scala/coursier/test/MavenTests.scala diff --git a/core/shared/src/main/scala/coursier/maven/MavenRepository.scala b/core/shared/src/main/scala/coursier/maven/MavenRepository.scala index f19e05a22..0ccb6c223 100644 --- a/core/shared/src/main/scala/coursier/maven/MavenRepository.scala +++ b/core/shared/src/main/scala/coursier/maven/MavenRepository.scala @@ -34,7 +34,10 @@ object MavenRepository { ): Option[String] = snapshotVersioning .snapshotVersions - .find(v => v.classifier == classifier && v.extension == extension) + .find(v => + (v.classifier == classifier || v.classifier == "*") && + (v.extension == extension || v.extension == "*") + ) .map(_.value) .filter(_.nonEmpty) diff --git a/core/shared/src/main/scala/coursier/maven/Pom.scala b/core/shared/src/main/scala/coursier/maven/Pom.scala index 268375234..579c54e58 100644 --- a/core/shared/src/main/scala/coursier/maven/Pom.scala +++ b/core/shared/src/main/scala/coursier/maven/Pom.scala @@ -322,6 +322,19 @@ object Pom { )) } + /** If `snapshotVersion` is missing, guess it based on + * `version`, `timestamp` and `buildNumber`, as is done in: + * https://github.com/sbt/ivy/blob/2.3.x-sbt/src/java/org/apache/ivy/plugins/resolver/IBiblioResolver.java + */ + def guessedSnapshotVersion( + version: String, + timestamp: String, + buildNumber: Int + ): SnapshotVersion = { + val value = s"${version.dropRight("SNAPSHOT".length)}$timestamp-$buildNumber" + SnapshotVersion("*", "*", value, None) + } + def snapshotVersioning(node: Node): String \/ SnapshotVersioning = { import Scalaz._ @@ -392,7 +405,10 @@ object Pom { buildNumber, localCopy, lastUpdatedOpt, - snapshotVersions + if (!snapshotVersions.isEmpty) + snapshotVersions + else + buildNumber.map(bn => guessedSnapshotVersion(version, timestamp, bn)).toList ) } diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/maven-metadata.xml b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/maven-metadata.xml new file mode 100644 index 000000000..a5ff9f919 --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/maven-metadata.xml @@ -0,0 +1,13 @@ + + + com.abc + test-snapshot-special + 0.1.0-SNAPSHOT + + + 20170421.034426 + 82 + + 20170421034426 + + diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar new file mode 100644 index 000000000..f1adef63c --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar @@ -0,0 +1 @@ +nothing here diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.jar b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.jar new file mode 100644 index 000000000..f1adef63c --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.jar @@ -0,0 +1 @@ +nothing here diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom new file mode 100644 index 000000000..6a351f420 --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom @@ -0,0 +1,9 @@ + + + 4.0.0 + com.abc + test-snapshot-special + 0.1.0-SNAPSHOT + Test + test.com + diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.md5 b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.md5 new file mode 100644 index 000000000..fe856b469 --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.md5 @@ -0,0 +1 @@ +16e31f3b60344230b62f1c6a9367edde diff --git a/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.sha1 b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.sha1 new file mode 100644 index 000000000..346b74c10 --- /dev/null +++ b/tests/jvm/src/test/resources/test-repo/http/abc.com/com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.pom.sha1 @@ -0,0 +1 @@ +563042a54b97e31af4529ea0db79ba2c4c2bb6cc diff --git a/tests/jvm/src/test/scala/coursier/test/MavenTests.scala b/tests/jvm/src/test/scala/coursier/test/MavenTests.scala new file mode 100644 index 000000000..fd7411606 --- /dev/null +++ b/tests/jvm/src/test/scala/coursier/test/MavenTests.scala @@ -0,0 +1,53 @@ +package coursier.test + +import coursier.{ Attributes, Dependency, Module } +import coursier.maven.MavenRepository + +import utest._ + +object MavenTests extends TestSuite { + + // only tested on the JVM for lack of support of XML attributes in the platform-dependent XML stubs + + val tests = TestSuite { + 'testSnapshotNoVersioning - { + + val dep = Dependency( + Module("com.abc", "test-snapshot-special"), + "0.1.0-SNAPSHOT", + transitive = false, + attributes = Attributes() + ) + + val repoBase = getClass.getResource("/test-repo/http/abc.com").toString.stripSuffix("/") + "/" + val repo = MavenRepository(repoBase) + + val mainJarUrl = repoBase + "com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82.jar" + val sourcesJarUrl = repoBase + "com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar" + + * - CentralTests.withArtifacts( + dep = dep, + artifactType = "jar", + extraRepo = Some(repo), + classifierOpt = None + ) { + case Seq(artifact) => + assert(artifact.url == mainJarUrl) + case other => + throw new Exception(s"Unexpected number of artifacts\n${other.mkString("\n")}") + } + + * - CentralTests.withArtifacts( + dep = dep, + artifactType = "jar", + extraRepo = Some(repo), + classifierOpt = Some("sources") + ) { + case Seq(artifact) => + assert(artifact.url == sourcesJarUrl) + case other => + throw new Exception(s"Unexpected number of artifacts\n${other.mkString("\n")}") + } + } + } +}