Skip to content

Commit

Permalink
Interpolate property values for repositories
Browse files Browse the repository at this point in the history
Update RepositoryConfigurationFactory to apply a RegexBasedInterpolator
to repository IDs and URLs.

Fixes spring-projectsgh-4318
Closes spring-projectsgh-4319
  • Loading branch information
mbenson authored and philwebb committed Nov 11, 2015
1 parent 5df8079 commit 5e7376f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

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;
Expand Down Expand Up @@ -78,17 +82,37 @@ private static void addDefaultCacheAsRepository(String localRepository,
}

private static void addActiveProfileRepositories(List<Profile> activeProfiles,
List<RepositoryConfiguration> repositoryConfiguration) {
List<RepositoryConfiguration> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public void run() {
"foo:bar");
}

@Test
public void interpolationProfileRepositories() {
SystemProperties.doWithSystemProperties(new Runnable() {
@Override
public void run() {
List<RepositoryConfiguration> 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<RepositoryConfiguration> configurations, String... expectedNames) {
assertThat(configurations, hasSize(expectedNames.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@
</repository>
</repositories>
</profile>
<profile>
<id>interpolation-profile</id>
<activation>
<property>
<name>interpolate</name>
<value>true</value>
</property>
</activation>
<properties>
<repo.base>maven.example.com</repo.base>
<repo.content>${repo.base}/content</repo.content>
</properties>
<repositories>
<repository>
<id>interpolate-releases</id>
<url>${repo.content}/releases</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>interpolate-snapshots</id>
<url>${repo.content}/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>

</settings>

0 comments on commit 5e7376f

Please sign in to comment.