From a568f0d89d5ddf52e18d4a74d79e81830d496cb1 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Tue, 28 Feb 2017 13:07:24 +0100 Subject: [PATCH] use local repository path from maven settings (#236) without this change the local repository maven setup is completely ignored with this change whatever you have set up locally will be taken into consideration --- .../main/asciidoc/spring-cloud-contract.adoc | 2 +- pom.xml | 2 +- spring-cloud-contract-dependencies/pom.xml | 5 +++ spring-cloud-contract-stub-runner/README.adoc | 7 +++- spring-cloud-contract-stub-runner/pom.xml | 4 ++ .../contract/stubrunner/AetherFactories.java | 39 ++++++++++++++++++- .../AetherStubDownloaderSpec.groovy | 38 ++++++++++++++++-- 7 files changed, 89 insertions(+), 8 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-contract.adoc b/docs/src/main/asciidoc/spring-cloud-contract.adoc index 67fa3aa0a7..0479ba1e76 100644 --- a/docs/src/main/asciidoc/spring-cloud-contract.adoc +++ b/docs/src/main/asciidoc/spring-cloud-contract.adoc @@ -4,7 +4,7 @@ = Spring Cloud Contract -_Documentation Authors: Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Jakub Kubryński, Karol Lassak, +_Documentation Authors: Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Dennis Kieselhorst, Jakub Kubryński, Karol Lassak, Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer_ {spring-cloud-version} diff --git a/pom.xml b/pom.xml index 747dc5a872..6dcdffd7d1 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-build - 1.2.2.BUILD-SNAPSHOT + 1.2.3.BUILD-SNAPSHOT diff --git a/spring-cloud-contract-dependencies/pom.xml b/spring-cloud-contract-dependencies/pom.xml index 4b86eb8f2e..3367c5d9d0 100644 --- a/spring-cloud-contract-dependencies/pom.xml +++ b/spring-cloud-contract-dependencies/pom.xml @@ -163,6 +163,11 @@ maven-aether-provider 3.2.1 + + org.apache.maven + maven-settings-builder + 3.2.1 + diff --git a/spring-cloud-contract-stub-runner/README.adoc b/spring-cloud-contract-stub-runner/README.adoc index ea9b185676..ab207b8fb8 100644 --- a/spring-cloud-contract-stub-runner/README.adoc +++ b/spring-cloud-contract-stub-runner/README.adoc @@ -127,6 +127,11 @@ include::src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/Stu Check the *Common properties for JUnit and Spring* for more information on how to apply global configuration of Stub Runner. +==== Maven settings + +The stub downloader honors Maven settings for a different local repository folder. +Authentication details for repositories and profiles are currently not taken into account, so you need to specify it using the properties mentioned above. + ==== Providing fixed ports You can also run your stubs on fixed ports. You can do it in two different ways. One is to pass it in the properties, and the other via fluent API of @@ -306,4 +311,4 @@ include::src/test/groovy/org/springframework/cloud/contract/stubrunner/serverexa That way your deployed application can send requests to started WireMock servers via the service discovery. Most likely points 1-3 could be set by default in `application.yml` cause they are not likely to change. That way you can provide only the list of stubs to download whenever you start -the Stub Runner Boot. \ No newline at end of file +the Stub Runner Boot. diff --git a/spring-cloud-contract-stub-runner/pom.xml b/spring-cloud-contract-stub-runner/pom.xml index 310f1b08fb..e9c936d727 100644 --- a/spring-cloud-contract-stub-runner/pom.xml +++ b/spring-cloud-contract-stub-runner/pom.xml @@ -90,6 +90,10 @@ org.apache.maven maven-aether-provider + + org.apache.maven + maven-settings-builder + junit junit diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherFactories.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherFactories.java index e7b22dd4ff..086f9bb288 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherFactories.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherFactories.java @@ -16,7 +16,16 @@ package org.springframework.cloud.contract.stubrunner; +import java.io.File; + import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; +import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuilder; +import org.apache.maven.settings.building.SettingsBuildingException; +import org.apache.maven.settings.building.SettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuildingResult; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; @@ -32,6 +41,8 @@ import org.eclipse.aether.transport.http.HttpTransporterFactory; class AetherFactories { private static final String MAVEN_LOCAL_REPOSITORY_LOCATION = "maven.repo.local"; + private static final String MAVEN_USER_SETTINGS_LOCATION = "org.apache.maven.user-settings"; + private static final String MAVEN_GLOBAL_SETTINGS_LOCATION = "org.apache.maven.global-settings"; public static RepositorySystem newRepositorySystem() { DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); @@ -54,7 +65,33 @@ class AetherFactories { } private static String localRepositoryDirectory() { - return System.getProperty(MAVEN_LOCAL_REPOSITORY_LOCATION, System.getProperty("user.home") + "/.m2/repository"); + String localRepoLocationFromSettings = settings().getLocalRepository(); + return System.getProperty(MAVEN_LOCAL_REPOSITORY_LOCATION, localRepoLocationFromSettings != null + ? localRepoLocationFromSettings + : System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository"); + } + + private static Settings settings() { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + SettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + String user = System.getProperty(MAVEN_USER_SETTINGS_LOCATION); + if (user == null) { + request.setUserSettingsFile(new File(new File(System.getProperty("user.home")).getAbsoluteFile(), + File.separator + ".m2" + File.separator + "settings.xml")); + } else { + request.setUserSettingsFile(new File(user)); + } + String global = System.getProperty(MAVEN_GLOBAL_SETTINGS_LOCATION); + if (global != null) { + request.setGlobalSettingsFile(new File(global)); + } + SettingsBuildingResult result; + try { + result = builder.build(request); + } catch (SettingsBuildingException ex) { + throw new IllegalStateException(ex); + } + return result.getEffectiveSettings(); } } 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 5001a9f792..b954505c13 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 @@ -1,9 +1,12 @@ package org.springframework.cloud.contract.stubrunner import io.specto.hoverfly.junit.HoverflyRule +import org.eclipse.aether.RepositorySystemSession; import org.junit.Rule - +import org.springframework.util.ResourceUtils import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + class AetherStubDownloaderSpec extends Specification { @@ -12,19 +15,46 @@ class AetherStubDownloaderSpec extends Specification { def 'Should be able to download from a repository using username and password authentication'() { given: - final StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder() + StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder() .withUsername("andrew.morgan") .withPassword("k+hbZp8rpolRucXB09dGE/CxPXxidQryQUYSGbeo6JE=") .withProxy("localhost", hoverflyRule.proxyPort) .withStubRepositoryRoot("https://test.jfrog.io/test/libs-snapshot-local") .build() - final AetherStubDownloader aetherStubDownloader = new AetherStubDownloader(stubRunnerOptions) + AetherStubDownloader aetherStubDownloader = new AetherStubDownloader(stubRunnerOptions) when: - final def jar = aetherStubDownloader.downloadAndUnpackStubJar(new StubConfiguration("io.test", "test-simulations-svc", "1.0-SNAPSHOT")) + def jar = aetherStubDownloader.downloadAndUnpackStubJar(new StubConfiguration("io.test", "test-simulations-svc", "1.0-SNAPSHOT")) then: jar != null } + + @RestoreSystemProperties + def 'Should use local repository from settings.xml'() { + given: + File tempSettings = File.createTempFile("settings", ".xml") + def m2repoFolder = 'm2repo' + File.separator + 'repository' + tempSettings.text = '' + + ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + m2repoFolder).getAbsolutePath() + '' + System.setProperty("org.apache.maven.user-settings", tempSettings.getAbsolutePath()) + RepositorySystemSession repositorySystemSession = + AetherFactories.newSession(AetherFactories.newRepositorySystem(), true); + + and: + StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder() + .withWorkOffline(true) + .build() + AetherStubDownloader aetherStubDownloader = new AetherStubDownloader(stubRunnerOptions) + + when: + def jar = aetherStubDownloader.downloadAndUnpackStubJar( + new StubConfiguration("org.springframework.cloud.contract.verifier.stubs", + "bootService", "0.0.1-SNAPSHOT")) + + then: + jar != null + repositorySystemSession.getLocalRepository().getBasedir().getAbsolutePath().endsWith(m2repoFolder) + } }