Skip to content

Commit 5f9fddd

Browse files
committed
Explicitly apply mirror and auth settings to remote repos in the CLI
Previously, the CLI relied on Aether using the session's mirror selector and authentication selector to customize the configured repositories. These selectors are only used to configure what Aether calls recessive repositories (repositories discovered when resolving an artifact), rather than the explicitly configured repositories that are typically used. This commit updates AetherGrapeEngine to apply mirror and authentication configuration to every added repository, bringing its behaviour for these two settings into line with what it already does for proxy configuration. Fixes #1354
1 parent 8e84151 commit 5f9fddd

File tree

3 files changed

+113
-13
lines changed

3 files changed

+113
-13
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,41 @@ protected void addRepository(RemoteRepository repository) {
263263
if (this.repositories.contains(repository)) {
264264
return;
265265
}
266+
267+
repository = getPossibleMirror(repository);
268+
repository = applyProxy(repository);
269+
repository = applyAuthentication(repository);
270+
271+
this.repositories.add(0, repository);
272+
}
273+
274+
private RemoteRepository getPossibleMirror(RemoteRepository remoteRepository) {
275+
RemoteRepository mirror = this.session.getMirrorSelector().getMirror(
276+
remoteRepository);
277+
if (mirror != null) {
278+
return mirror;
279+
}
280+
281+
return remoteRepository;
282+
}
283+
284+
private RemoteRepository applyProxy(RemoteRepository repository) {
266285
if (repository.getProxy() == null) {
267286
RemoteRepository.Builder builder = new RemoteRepository.Builder(repository);
268287
builder.setProxy(this.session.getProxySelector().getProxy(repository));
269288
repository = builder.build();
270289
}
271-
this.repositories.add(0, repository);
290+
return repository;
291+
}
292+
293+
private RemoteRepository applyAuthentication(RemoteRepository repository) {
294+
if (repository.getAuthentication() == null) {
295+
RemoteRepository.Builder builder = new RemoteRepository.Builder(repository);
296+
builder.setAuthentication(this.session.getAuthenticationSelector()
297+
.getAuthentication(repository));
298+
repository = builder.build();
299+
}
300+
return repository;
272301
}
273302

274303
@Override

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@
5858
public class SettingsXmlRepositorySystemSessionAutoConfiguration implements
5959
RepositorySystemSessionAutoConfiguration {
6060

61-
private static final String DEFAULT_HOME_DIR = System.getProperty("user.home");
62-
6361
private final String homeDir;
6462

6563
public SettingsXmlRepositorySystemSessionAutoConfiguration() {
66-
this(DEFAULT_HOME_DIR);
64+
this(System.getProperty("user.home"));
6765
}
6866

6967
SettingsXmlRepositorySystemSessionAutoConfiguration(String homeDir) {

spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818

1919
import groovy.lang.GroovyClassLoader;
2020

21+
import java.io.File;
2122
import java.net.URI;
2223
import java.net.URL;
2324
import java.util.Arrays;
2425
import java.util.Collections;
2526
import java.util.HashMap;
27+
import java.util.List;
2628
import java.util.Map;
2729

2830
import org.eclipse.aether.DefaultRepositorySystemSession;
29-
import org.eclipse.aether.util.repository.JreProxySelector;
31+
import org.eclipse.aether.repository.Authentication;
32+
import org.eclipse.aether.repository.RemoteRepository;
3033
import org.junit.Test;
3134
import org.springframework.test.util.ReflectionTestUtils;
3235

3336
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertNotNull;
3438
import static org.junit.Assert.assertTrue;
3539

3640
/**
@@ -42,10 +46,14 @@ public class AetherGrapeEngineTests {
4246

4347
private final GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
4448

45-
private final AetherGrapeEngine grapeEngine = AetherGrapeEngineFactory.create(
46-
this.groovyClassLoader, Arrays.asList(new RepositoryConfiguration("central",
47-
URI.create("http://repo1.maven.org/maven2"), false)),
48-
new DependencyResolutionContext());
49+
private final AetherGrapeEngine grapeEngine = createGrapeEngine();
50+
51+
private AetherGrapeEngine createGrapeEngine() {
52+
return AetherGrapeEngineFactory.create(this.groovyClassLoader, Arrays
53+
.asList(new RepositoryConfiguration("central", URI
54+
.create("http://repo1.maven.org/maven2"), false)),
55+
new DependencyResolutionContext());
56+
}
4957

5058
@Test
5159
public void dependencyResolution() {
@@ -59,10 +67,53 @@ public void dependencyResolution() {
5967

6068
@Test
6169
public void proxySelector() {
62-
DefaultRepositorySystemSession session = (DefaultRepositorySystemSession) ReflectionTestUtils
63-
.getField(this.grapeEngine, "session");
64-
assertTrue((session.getProxySelector() instanceof CompositeProxySelector)
65-
|| (session.getProxySelector() instanceof JreProxySelector));
70+
doWithCustomUserHome(new Runnable() {
71+
72+
@Override
73+
public void run() {
74+
AetherGrapeEngine grapeEngine = createGrapeEngine();
75+
76+
DefaultRepositorySystemSession session = (DefaultRepositorySystemSession) ReflectionTestUtils
77+
.getField(grapeEngine, "session");
78+
79+
assertTrue(session.getProxySelector() instanceof CompositeProxySelector);
80+
}
81+
});
82+
}
83+
84+
@Test
85+
public void repositoryMirrors() {
86+
doWithCustomUserHome(new Runnable() {
87+
88+
@SuppressWarnings("unchecked")
89+
@Override
90+
public void run() {
91+
AetherGrapeEngine grapeEngine = createGrapeEngine();
92+
93+
List<RemoteRepository> repositories = (List<RemoteRepository>) ReflectionTestUtils
94+
.getField(grapeEngine, "repositories");
95+
assertEquals(1, repositories.size());
96+
assertEquals("central-mirror", repositories.get(0).getId());
97+
}
98+
});
99+
}
100+
101+
@Test
102+
public void repositoryAuthentication() {
103+
doWithCustomUserHome(new Runnable() {
104+
105+
@SuppressWarnings("unchecked")
106+
@Override
107+
public void run() {
108+
AetherGrapeEngine grapeEngine = createGrapeEngine();
109+
110+
List<RemoteRepository> repositories = (List<RemoteRepository>) ReflectionTestUtils
111+
.getField(grapeEngine, "repositories");
112+
assertEquals(1, repositories.size());
113+
Authentication authentication = repositories.get(0).getAuthentication();
114+
assertNotNull(authentication);
115+
}
116+
});
66117
}
67118

68119
@SuppressWarnings("unchecked")
@@ -190,4 +241,26 @@ private Map<String, Object> createExclusion(String group, String module) {
190241
exclusion.put("module", module);
191242
return exclusion;
192243
}
244+
245+
private void doWithCustomUserHome(Runnable action) {
246+
doWithSystemProperty("user.home",
247+
new File("src/test/resources").getAbsolutePath(), action);
248+
}
249+
250+
private void doWithSystemProperty(String key, String value, Runnable action) {
251+
String previousValue = setOrClearSystemProperty(key, value);
252+
try {
253+
action.run();
254+
}
255+
finally {
256+
setOrClearSystemProperty(key, previousValue);
257+
}
258+
}
259+
260+
private String setOrClearSystemProperty(String key, String value) {
261+
if (value != null) {
262+
return System.setProperty(key, value);
263+
}
264+
return System.clearProperty(key);
265+
}
193266
}

0 commit comments

Comments
 (0)