From 5e7376fb3c3c3dd91ac465e16d05cfa3c217f155 Mon Sep 17 00:00:00 2001 From: Matt Benson Date: Tue, 27 Oct 2015 17:27:15 -0500 Subject: [PATCH] Interpolate property values for repositories Update RepositoryConfigurationFactory to apply a RegexBasedInterpolator to repository IDs and URLs. Fixes gh-4318 Closes gh-4319 --- .../RepositoryConfigurationFactory.java | 34 +++++++++++++++--- .../RepositoryConfigurationFactoryTests.java | 15 ++++++++ .../.m2/settings.xml | 36 +++++++++++++++++++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java index a7d824dcc3..3b60f5d3d9 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java @@ -23,6 +23,10 @@ import java.util.List; import org.apache.maven.settings.Profile; import org.apache.maven.settings.Repository; +import org.codehaus.plexus.interpolation.InterpolationException; +import org.codehaus.plexus.interpolation.Interpolator; +import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; +import org.codehaus.plexus.interpolation.RegexBasedInterpolator; import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration; import org.springframework.boot.cli.compiler.maven.MavenSettings; @@ -78,17 +82,37 @@ public final class RepositoryConfigurationFactory { } private static void addActiveProfileRepositories(List activeProfiles, - List repositoryConfiguration) { + List configurations) { for (Profile activeProfile : activeProfiles) { + Interpolator interpolator = new RegexBasedInterpolator(); + interpolator.addValueSource( + new PropertiesBasedValueSource(activeProfile.getProperties())); for (Repository repository : activeProfile.getRepositories()) { - repositoryConfiguration.add(new RepositoryConfiguration( - repository.getId(), URI.create(repository.getUrl()), - repository.getSnapshots() != null - ? repository.getSnapshots().isEnabled() : false)); + configurations.add(getRepositoryConfiguration(interpolator, repository)); } } } + private static RepositoryConfiguration getRepositoryConfiguration( + Interpolator interpolator, Repository repository) { + String name = interpolate(interpolator, repository.getId()); + String url = interpolate(interpolator, repository.getUrl()); + boolean snapshotsEnabled = false; + if (repository.getSnapshots() != null) { + snapshotsEnabled = repository.getSnapshots().isEnabled(); + } + return new RepositoryConfiguration(name, URI.create(url), snapshotsEnabled); + } + + private static String interpolate(Interpolator interpolator, String value) { + try { + return interpolator.interpolate(value); + } + catch (InterpolationException ex) { + return value; + } + } + private static File getLocalRepositoryDirectory(String localRepository) { if (StringUtils.hasText(localRepository)) { return new File(localRepository); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java index 3755da6903..f640bf75e2 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactoryTests.java @@ -90,6 +90,21 @@ public class RepositoryConfigurationFactoryTests { "foo:bar"); } + @Test + public void interpolationProfileRepositories() { + SystemProperties.doWithSystemProperties(new Runnable() { + @Override + public void run() { + List repositoryConfiguration = RepositoryConfigurationFactory + .createDefaultRepositoryConfiguration(); + assertRepositoryConfiguration(repositoryConfiguration, "central", "local", + "spring-snapshot", "spring-milestone", "interpolate-releases", + "interpolate-snapshots"); + } + }, "user.home:src/test/resources/maven-settings/active-profile-repositories", + "interpolate:true"); + } + private void assertRepositoryConfiguration( List configurations, String... expectedNames) { assertThat(configurations, hasSize(expectedNames.length)); diff --git a/spring-boot-cli/src/test/resources/maven-settings/active-profile-repositories/.m2/settings.xml b/spring-boot-cli/src/test/resources/maven-settings/active-profile-repositories/.m2/settings.xml index e68c6ffc91..8cc312378d 100644 --- a/spring-boot-cli/src/test/resources/maven-settings/active-profile-repositories/.m2/settings.xml +++ b/spring-boot-cli/src/test/resources/maven-settings/active-profile-repositories/.m2/settings.xml @@ -56,6 +56,42 @@ + + interpolation-profile + + + interpolate + true + + + + maven.example.com + ${repo.base}/content + + + + interpolate-releases + ${repo.content}/releases + + true + never + + + false + + + + interpolate-snapshots + ${repo.content}/snapshots + + false + + + true + + + +