From 6147cbe77527de28eb35380bf56f52c391fa898a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Smyku=C5=82a?= Date: Mon, 18 Apr 2016 21:56:48 +0200 Subject: [PATCH] fix snapshot downloading (#11) --- .../stubrunner/AetherStubDownloader.groovy | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/io/codearte/accurest/maven/stubrunner/AetherStubDownloader.groovy b/src/main/groovy/io/codearte/accurest/maven/stubrunner/AetherStubDownloader.groovy index 82b0471923..d6f58f59f9 100644 --- a/src/main/groovy/io/codearte/accurest/maven/stubrunner/AetherStubDownloader.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/stubrunner/AetherStubDownloader.groovy @@ -11,6 +11,9 @@ import org.eclipse.aether.artifact.DefaultArtifact import org.eclipse.aether.repository.RemoteRepository import org.eclipse.aether.resolution.ArtifactRequest import org.eclipse.aether.resolution.ArtifactResult +import org.eclipse.aether.resolution.VersionRangeRequest +import org.eclipse.aether.resolution.VersionRangeResult +import org.eclipse.aether.version.Version import static io.codearte.accurest.stubrunner.util.ZipCategory.unzipTo import static java.nio.file.Files.createTempDirectory @@ -22,28 +25,37 @@ class AetherStubDownloader implements StubDownloader { private static final String ACCUREST_TEMP_DIR_PREFIX = 'accurest' private static final String ARTIFACT_EXTENSION = 'jar' - private static final String ARTIFACT_VERSION = 'LATEST' + private static final String ARTIFACT_VERSION = '(0,]' private final List remoteRepos - private final RepositorySystem repoSystem - private final RepositorySystemSession repoSession + private final RepositorySystem repositorySystem + private final RepositorySystemSession session - AetherStubDownloader(RepositorySystem repoSystem, List remoteRepos, RepositorySystemSession repoSession) { - this.remoteRepos = remoteRepos - this.repoSystem = repoSystem - this.repoSession = repoSession + AetherStubDownloader(RepositorySystem repositorySystem, List repositories, RepositorySystemSession session) { + this.remoteRepos = repositories + this.repositorySystem = repositorySystem + this.session = session } @Override public File downloadAndUnpackStubJar(boolean workOffline, String stubRepositoryRoot, String stubsGroup, String stubsModule, String classifier) { - Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, ARTIFACT_EXTENSION, ARTIFACT_VERSION) + Version highestVersion = resolveArtifactVersion(stubsGroup, stubsModule, classifier); + log.info("Resolving highest version is $highestVersion") + Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, ARTIFACT_EXTENSION, highestVersion.toString()) ArtifactRequest request = new ArtifactRequest(artifact: artifact, repositories: remoteRepos) log.info("Resolving artifact $artifact from $remoteRepos") - ArtifactResult result = repoSystem.resolveArtifact(repoSession, request) + ArtifactResult result = repositorySystem.resolveArtifact(session, request) log.info("Resolved artifact $artifact to ${result.artifact.file} from ${result.repository}") return unpackStubJarToATemporaryFolder(result.artifact.file.toURI()) } + private Version resolveArtifactVersion(String stubsGroup, String stubsModule, String classifier) { + Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, ARTIFACT_EXTENSION, ARTIFACT_VERSION) + VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, remoteRepos, null); + VersionRangeResult rangeResult = repositorySystem.resolveVersionRange(session, versionRangeRequest); + return rangeResult.highestVersion; + } + private static File unpackStubJarToATemporaryFolder(URI stubJarUri) { File tmpDirWhereStubsWillBeUnzipped = createTempDirectory(ACCUREST_TEMP_DIR_PREFIX).toFile() tmpDirWhereStubsWillBeUnzipped.deleteOnExit()