From 13902ecf1d73dea657c355d8baf358a2778f4715 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 15 Mar 2018 17:13:51 +0100 Subject: [PATCH] Documented custom settings.xml setup; fixes gh-552 --- spring-cloud-contract-stub-runner/README.adoc | 7 ++++ .../contract/stubrunner/AetherFactories.java | 41 +++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/spring-cloud-contract-stub-runner/README.adoc b/spring-cloud-contract-stub-runner/README.adoc index 1f84509604..64a10cb65c 100644 --- a/spring-cloud-contract-stub-runner/README.adoc +++ b/spring-cloud-contract-stub-runner/README.adoc @@ -449,6 +449,13 @@ an existing `DiscoveryClient` its results will be ignored. However, if you want `stubrunner.cloud.delegate.enabled` to `true` and then your existing `DiscoveryClient` results will be merged with the stubbed ones. +The default Maven configuration used by Stub Runner can be tweaked either +via the following system properties or environment variables + +- `maven.repo.local` - path to the custom maven local repository location +- `org.apache.maven.user-settings` - path to custom maven user settings location +- `org.apache.maven.global-settings` - path to maven global settings location + === Stub Runner Boot Application Spring Cloud Contract Stub Runner Boot is a Spring Boot application that exposes REST endpoints to 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 edc538e61a..55d6cf3aca 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,6 +16,14 @@ package org.springframework.cloud.contract.stubrunner; +import java.io.File; + +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.repository.RepositoryPolicy; +import org.springframework.cloud.contract.stubrunner.util.StringUtils; import shaded.org.apache.maven.repository.internal.MavenRepositorySystemUtils; import shaded.org.apache.maven.settings.Settings; import shaded.org.apache.maven.settings.building.DefaultSettingsBuilderFactory; @@ -31,14 +39,6 @@ import shaded.org.eclipse.aether.spi.connector.transport.TransporterFactory; import shaded.org.eclipse.aether.transport.file.FileTransporterFactory; import shaded.org.eclipse.aether.transport.http.HttpTransporterFactory; -import java.io.File; - -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.RepositoryPolicy; - class AetherFactories { private static final String MAVEN_LOCAL_REPOSITORY_LOCATION = "maven.repo.local"; @@ -67,22 +67,37 @@ class AetherFactories { private static String localRepositoryDirectory() { String localRepoLocationFromSettings = settings().getLocalRepository(); - return System.getProperty(MAVEN_LOCAL_REPOSITORY_LOCATION, localRepoLocationFromSettings != null - ? localRepoLocationFromSettings - : System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository"); + return readPropertyFromSystemProps(localRepoLocationFromSettings); + } + + private static String readPropertyFromSystemProps( + String localRepoLocationFromSettings) { + String mavenLocalRepo = fromSystemPropOrEnv(MAVEN_LOCAL_REPOSITORY_LOCATION); + return StringUtils.hasText(mavenLocalRepo) ? mavenLocalRepo : + localRepoLocationFromSettings != null ? localRepoLocationFromSettings + : System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository"; + } + + // system prop takes precedence over env var + private static String fromSystemPropOrEnv(String prop) { + String resolvedProp = System.getProperty(prop); + if (StringUtils.hasText(resolvedProp)) { + return resolvedProp; + } + return System.getenv(prop); } private static Settings settings() { SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); SettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); - String user = System.getProperty(MAVEN_USER_SETTINGS_LOCATION); + String user = fromSystemPropOrEnv(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); + String global = fromSystemPropOrEnv(MAVEN_GLOBAL_SETTINGS_LOCATION); if (global != null) { request.setGlobalSettingsFile(new File(global)); }