diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java index 8552f92047..dc6a901492 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java @@ -16,11 +16,6 @@ package org.springframework.cloud.contract.stubrunner; -import static java.nio.file.Files.createTempDirectory; -import static org.springframework.cloud.contract.stubrunner.AetherFactories.newRepositorySystem; -import static org.springframework.cloud.contract.stubrunner.AetherFactories.newSession; -import static org.springframework.cloud.contract.stubrunner.util.ZipCategory.unzipTo; - import java.io.File; import java.io.IOException; import java.net.URI; @@ -33,6 +28,7 @@ import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.Proxy; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ArtifactRequest; @@ -46,6 +42,11 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.contract.stubrunner.StubRunnerOptions.StubRunnerProxyOptions; import org.springframework.util.StringUtils; +import static java.nio.file.Files.createTempDirectory; +import static org.springframework.cloud.contract.stubrunner.AetherFactories.newRepositorySystem; +import static org.springframework.cloud.contract.stubrunner.AetherFactories.newSession; +import static org.springframework.cloud.contract.stubrunner.util.ZipCategory.unzipTo; + /** * @author Mariusz Smykula */ @@ -61,6 +62,7 @@ public class AetherStubDownloader implements StubDownloader { private final List remoteRepos; private final RepositorySystem repositorySystem; private final RepositorySystemSession session; + private final boolean workOffline; public AetherStubDownloader(StubRunnerOptions stubRunnerOptions) { if (log.isDebugEnabled()) { @@ -81,6 +83,7 @@ public class AetherStubDownloader implements StubDownloader { } this.repositorySystem = newRepositorySystem(); this.session = newSession(this.repositorySystem, stubRunnerOptions.workOffline); + this.workOffline = stubRunnerOptions.workOffline; } private boolean remoteReposMissing() { @@ -102,6 +105,7 @@ public class AetherStubDownloader implements StubDownloader { if (remoteReposMissing()) { log.error("Remote repositories for stubs are not specified and work offline flag wasn't passed"); } + this.workOffline = false; } private List remoteRepositories(StubRunnerOptions stubRunnerOptions) { @@ -121,7 +125,6 @@ public class AetherStubDownloader implements StubDownloader { final StubRunnerProxyOptions p = stubRunnerOptions.getProxyOptions(); builder.setProxy(new Proxy(null, p.getProxyHost(), p.getProxyPort())); } - remoteRepos.add(builder.build()); } } @@ -149,12 +152,20 @@ public class AetherStubDownloader implements StubDownloader { ArtifactResult result = this.repositorySystem.resolveArtifact(this.session, request); log.info("Resolved artifact [" + artifact + "] to " + result.getArtifact().getFile()); + if (resolvedFromLocalRepo(result) && shouldDownloadFromRemote()) { + throw new IllegalStateException("The artifact was found in the local repository " + + "but you have explicitly stated that it should be downloaded from a remote one"); + } File temporaryFile = unpackStubJarToATemporaryFolder( result.getArtifact().getFile().toURI()); log.info("Unpacked file to [" + temporaryFile + "]"); return temporaryFile; } + catch (IllegalStateException ise) { + throw ise; + } catch (Exception e) { + //TODO: Start throwing this exception instead of returning null log.warn( "Exception occurred while trying to download a stub for group [" + stubsGroup + "] module [" + stubsModule @@ -165,6 +176,14 @@ public class AetherStubDownloader implements StubDownloader { } + private boolean resolvedFromLocalRepo(ArtifactResult result) { + return result.getRepository() instanceof LocalRepository; + } + + private boolean shouldDownloadFromRemote() { + return !remoteReposMissing() && !this.workOffline; + } + private String getVersion(String stubsGroup, String stubsModule, String version, String classifier) { if (!StringUtils.hasText(version) || LATEST_VERSION_IN_IVY.equals(version)) { diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/AetherStubDownloaderSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/AetherStubDownloaderSpec.groovy index b954505c13..3e3a262639 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/AetherStubDownloaderSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/AetherStubDownloaderSpec.groovy @@ -31,6 +31,22 @@ class AetherStubDownloaderSpec extends Specification { jar != null } + def 'Should throw an exception when a jar is in local m2 and not in remote repo'() { + given: + StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder() + .withStubRepositoryRoot("https://test.jfrog.io/test/libs-snapshot-local") + .build() + + AetherStubDownloader aetherStubDownloader = new AetherStubDownloader(stubRunnerOptions) + + when: + def jar = aetherStubDownloader.downloadAndUnpackStubJar(new StubConfiguration("org.springframework.cloud", "spring-cloud-contract-spec", "+", "")) + + then: + IllegalStateException e = thrown(IllegalStateException) + e.message.contains("The artifact was found in the local repository but you have explicitly stated that it should be downloaded from a remote one") + } + @RestoreSystemProperties def 'Should use local repository from settings.xml'() { given: