Configure CLI with repositories from active profiles in settings.xml
This commit enhances the CLI to use the repositories configured in the profiles declared in a user's Maven settings.xml file during dependency resolution. A profile must be active for its repositories to be used. Closes gh-2703 Closes gh-3483
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.cli.compiler;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
import org.springframework.boot.cli.util.SystemProperties;
|
||||
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link RepositoryConfigurationFactory}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class RepositoryConfigurationFactoryTests {
|
||||
|
||||
@Test
|
||||
public void defaultRepositories() {
|
||||
SystemProperties.doWithSystemProperties(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
assertRepositoryConfiguration(repositoryConfiguration, "central",
|
||||
"local", "spring-snapshot", "spring-milestone");
|
||||
}
|
||||
}, "user.home:src/test/resources/maven-settings/basic");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snapshotRepositoriesDisabled() {
|
||||
SystemProperties.doWithSystemProperties(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
assertRepositoryConfiguration(repositoryConfiguration, "central",
|
||||
"local");
|
||||
}
|
||||
}, "user.home:src/test/resources/maven-settings/basic",
|
||||
"disableSpringSnapshotRepos:true");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activeByDefaultProfileRepositories() {
|
||||
SystemProperties.doWithSystemProperties(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
assertRepositoryConfiguration(repositoryConfiguration, "central",
|
||||
"local", "spring-snapshot", "spring-milestone",
|
||||
"active-by-default");
|
||||
}
|
||||
}, "user.home:src/test/resources/maven-settings/active-profile-repositories");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activeByPropertyProfileRepositories() {
|
||||
SystemProperties.doWithSystemProperties(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
assertRepositoryConfiguration(repositoryConfiguration, "central",
|
||||
"local", "spring-snapshot", "spring-milestone",
|
||||
"active-by-property");
|
||||
}
|
||||
},
|
||||
"user.home:src/test/resources/maven-settings/active-profile-repositories",
|
||||
"foo:bar");
|
||||
}
|
||||
|
||||
private void assertRepositoryConfiguration(
|
||||
List<RepositoryConfiguration> configurations, String... expectedNames) {
|
||||
assertThat(configurations, hasSize(expectedNames.length));
|
||||
Set<String> actualNames = new HashSet<String>();
|
||||
for (RepositoryConfiguration configuration : configurations) {
|
||||
actualNames.add(configuration.getName());
|
||||
}
|
||||
assertThat(actualNames, hasItems(expectedNames));
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.boot.cli.util.SystemProperties;
|
||||
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -89,25 +90,33 @@ public class SettingsXmlRepositorySystemSessionAutoConfigurationTests {
|
||||
}
|
||||
});
|
||||
|
||||
System.setProperty("foo", "bar");
|
||||
try {
|
||||
new SettingsXmlRepositorySystemSessionAutoConfiguration(
|
||||
"src/test/resources/maven-settings/property-interpolation").apply(
|
||||
session, this.repositorySystem);
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("foo");
|
||||
}
|
||||
SystemProperties.doWithSystemProperties(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new SettingsXmlRepositorySystemSessionAutoConfiguration()
|
||||
.apply(session,
|
||||
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem);
|
||||
}
|
||||
}, "user.home:src/test/resources/maven-settings/property-interpolation",
|
||||
"foo:bar");
|
||||
|
||||
assertThat(session.getLocalRepository().getBasedir().getAbsolutePath(),
|
||||
endsWith(File.separatorChar + "bar" + File.separatorChar + "repository"));
|
||||
}
|
||||
|
||||
private void assertSessionCustomization(String userHome) {
|
||||
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
|
||||
final DefaultRepositorySystemSession session = MavenRepositorySystemUtils
|
||||
.newSession();
|
||||
|
||||
new SettingsXmlRepositorySystemSessionAutoConfiguration(userHome).apply(session,
|
||||
this.repositorySystem);
|
||||
SystemProperties.doWithSystemProperties(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new SettingsXmlRepositorySystemSessionAutoConfiguration()
|
||||
.apply(session,
|
||||
SettingsXmlRepositorySystemSessionAutoConfigurationTests.this.repositorySystem);
|
||||
}
|
||||
}, "user.home:" + userHome);
|
||||
|
||||
RemoteRepository repository = new RemoteRepository.Builder("my-server",
|
||||
"default", "http://maven.example.com").build();
|
||||
@@ -151,4 +160,5 @@ public class SettingsXmlRepositorySystemSessionAutoConfigurationTests {
|
||||
assertEquals("tester", authenticationContext.get(AuthenticationContext.USERNAME));
|
||||
assertEquals("secret", authenticationContext.get(AuthenticationContext.PASSWORD));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.cli.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Utilities for working with System properties in unit tests
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class SystemProperties {
|
||||
|
||||
/**
|
||||
* Performs the given {@code action} with the given system properties set. System
|
||||
* properties are restored to their previous values once the action has run.
|
||||
*
|
||||
* @param action The action to perform
|
||||
* @param systemPropertyPairs The system properties, each in the form
|
||||
* {@code key:value}
|
||||
*/
|
||||
public static void doWithSystemProperties(Runnable action,
|
||||
String... systemPropertyPairs) {
|
||||
Map<String, String> originalValues = new HashMap<String, String>();
|
||||
for (String pair : systemPropertyPairs) {
|
||||
String[] components = pair.split(":");
|
||||
String key = components[0];
|
||||
String value = components[1];
|
||||
originalValues.put(key, System.setProperty(key, value));
|
||||
}
|
||||
try {
|
||||
action.run();
|
||||
}
|
||||
finally {
|
||||
for (Entry<String, String> entry : originalValues.entrySet()) {
|
||||
if (entry.getValue() == null) {
|
||||
System.clearProperty(entry.getKey());
|
||||
}
|
||||
else {
|
||||
System.setProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user