mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Interpolate property values for repositories
Update RepositoryConfigurationFactory to apply a RegexBasedInterpolator to repository IDs and URLs. Fixes gh-4318 Closes gh-4319
This commit is contained in:
parent
5df807926e
commit
5e7376fb3c
@ -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<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);
|
||||
|
@ -90,6 +90,21 @@ public class RepositoryConfigurationFactoryTests {
|
||||
"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));
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user