Skip to content

Commit 843a1e4

Browse files
author
Dave Syer
committed
Look in other places for settings.xml
Fixes gh-208
1 parent 5618395 commit 843a1e4

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,10 @@ also use a system property `maven.repo.local` (or `maven.home` which
577577
defaults to `${user.home}/.m2`) when you launch the thin jar, but not
578578
a command line flag. The Maven and Gradle plugins respond to the
579579
`settings.xml` and also (with Maven) to `-Dmaven.repo.local` as a
580-
command line flag.
580+
command line flag. When the launcher runs it also looks in `${thin.root}/..`
581+
for a `settings.xml` file and uses that in preference to any other location
582+
if it exists. (For historical reasons it also looks in `${thin.root}/.m2`
583+
but that directory is unlikely to exist.)
581584

582585
## How to Configure a Proxy
583586

launcher/src/main/java/org/springframework/boot/loader/thin/MavenSettingsReader.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
4040

4141
/**
42-
* {@code MavenSettingsReader} reads settings from a user's Maven settings.xml file,
43-
* decrypting them if necessary using settings-security.xml.
42+
* {@code MavenSettingsReader} reads settings from a user's Maven settings.xml
43+
* file, decrypting them if necessary using settings-security.xml.
4444
*
4545
* @author Andy Wilkinson
4646
* @since 1.3.0
@@ -57,7 +57,20 @@ public MavenSettingsReader() {
5757

5858
public MavenSettingsReader(String homeDir) {
5959
if (homeDir == null) {
60-
homeDir = System.getProperty("settings.home", System.getProperty("user.home"));
60+
homeDir = System.getProperty("settings.home");
61+
if (homeDir == null) {
62+
homeDir = System.getProperty("maven.repo.local");
63+
if (homeDir != null) {
64+
homeDir = new File(homeDir).getParent();
65+
if (homeDir != null) {
66+
homeDir = new File(homeDir).getParent();
67+
} else {
68+
homeDir = System.getProperty("user.home");
69+
}
70+
} else {
71+
homeDir = System.getProperty("user.home");
72+
}
73+
}
6174
}
6275
this.homeDir = homeDir;
6376
}
@@ -77,8 +90,7 @@ public static void applySettings(MavenSettings settings, DefaultRepositorySystem
7790
try {
7891
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session,
7992
new LocalRepository(settings.getLocalRepository())));
80-
}
81-
catch (NoLocalRepositoryManagerException e) {
93+
} catch (NoLocalRepositoryManagerException e) {
8294
throw new IllegalStateException("Cannot set local repository to " + settings.getLocalRepository(), e);
8395
}
8496
}
@@ -92,14 +104,27 @@ private Settings loadSettings() {
92104
File settingsFile = new File(this.homeDir, ".m2/settings.xml");
93105
if (settingsFile.exists()) {
94106
log.info("Reading settings from: " + settingsFile);
95-
}
96-
else {
107+
} else {
97108
log.info("No settings found at: " + settingsFile);
98-
String home = System.getProperty("user.home");
99-
if (!new File(home).getAbsolutePath().equals(new File(this.homeDir).getAbsolutePath())) {
100-
settingsFile = new File(home, ".m2/settings.xml");
101-
if (settingsFile.exists()) {
102-
log.info("Reading settings from: " + settingsFile);
109+
settingsFile = new File(this.homeDir, "../settings.xml");
110+
if (settingsFile.exists()) {
111+
log.info("Reading settings from: " + settingsFile);
112+
} else {
113+
String home = System.getProperty("maven.repo.local");
114+
if (home != null
115+
&& !new File(home).getAbsolutePath().equals(new File(this.homeDir).getAbsolutePath())) {
116+
settingsFile = new File(home, "../settings.xml");
117+
if (settingsFile.exists()) {
118+
log.info("Reading settings from: " + settingsFile);
119+
}
120+
} else {
121+
home = System.getProperty("user.home");
122+
if (!new File(home).getAbsolutePath().equals(new File(this.homeDir).getAbsolutePath())) {
123+
settingsFile = new File(home, ".m2/settings.xml");
124+
if (settingsFile.exists()) {
125+
log.info("Reading settings from: " + settingsFile);
126+
}
127+
}
103128
}
104129
}
105130
}
@@ -112,8 +137,7 @@ private Settings loadSettings(File settingsFile) {
112137
request.setSystemProperties(System.getProperties());
113138
try {
114139
return new DefaultSettingsBuilderFactory().newInstance().build(request).getEffectiveSettings();
115-
}
116-
catch (SettingsBuildingException ex) {
140+
} catch (SettingsBuildingException ex) {
117141
throw new IllegalStateException("Failed to build settings from " + settingsFile, ex);
118142
}
119143
}
@@ -136,8 +160,7 @@ private void setField(Class<?> sourceClass, String fieldName, Object target, Obj
136160
Field field = sourceClass.getDeclaredField(fieldName);
137161
field.setAccessible(true);
138162
field.set(target, value);
139-
}
140-
catch (Exception ex) {
163+
} catch (Exception ex) {
141164
throw new IllegalStateException("Failed to set field '" + fieldName + "' on '" + target + "'", ex);
142165
}
143166
}
@@ -151,8 +174,7 @@ private class SpringBootSecDispatcher extends DefaultSecDispatcher {
151174
this._configurationFile = file.getAbsolutePath();
152175
try {
153176
this._cipher = new DefaultPlexusCipher();
154-
}
155-
catch (PlexusCipherException e) {
177+
} catch (PlexusCipherException e) {
156178
throw new IllegalStateException(e);
157179
}
158180
}

launcher/src/test/java/org/springframework/boot/loader/thin/MavenSettingsReaderTests.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ public void canReadSettings() {
4040
assertThat(settings).isNotNull();
4141
}
4242

43+
@Test
44+
public void canReadSettingsAtThinRoot() {
45+
MavenSettingsReader reader = new MavenSettingsReader(
46+
"src/test/resources/settings/proxy/.m2");
47+
MavenSettings settings = reader.readSettings();
48+
assertThat(settings).isNotNull();
49+
}
50+
51+
@Test
52+
public void canReadSettingsAtMavenRepoLocal() {
53+
System.setProperty("maven.repo.local",
54+
"src/test/resources/settings/proxy/.m2/repository");
55+
try {
56+
57+
MavenSettingsReader reader = new MavenSettingsReader();
58+
MavenSettings settings = reader.readSettings();
59+
assertThat(settings).isNotNull();
60+
RemoteRepository remote = new RemoteRepository.Builder("central", null, "https://central-mirror.example.com/maven2")
61+
.setContentType("default").build();
62+
RemoteRepository mirror = settings.getMirrorSelector().getMirror(remote);
63+
assertThat(mirror).isNotNull();
64+
} finally {
65+
System.clearProperty("maven.repo.local");
66+
}
67+
}
68+
4369
@Test
4470
public void proxyConfiguration() {
4571
MavenSettingsReader reader = new MavenSettingsReader(
@@ -130,7 +156,8 @@ public void credentialsConfiguration() {
130156
RemoteRepository remote = new RemoteRepository.Builder(repo.getId(), null, repo.getUrl()).build();
131157
Authentication authentication = settings.getAuthenticationSelector().getAuthentication(remote);
132158
assertThat(authentication).isNotNull();
133-
remote = new RemoteRepository.Builder(repo.getId(), null, repo.getUrl()).setAuthentication(authentication).build();
159+
remote = new RemoteRepository.Builder(repo.getId(), null, repo.getUrl()).setAuthentication(authentication)
160+
.build();
134161
AuthenticationContext context = AuthenticationContext.forRepository(session,
135162
remote);
136163
assertThat(context).isNotNull();
@@ -145,7 +172,8 @@ public void mirrorConfiguration() {
145172
assertThat(settings.getActiveProfiles().get(0).getRepositories().get(0))
146173
.isNotNull();
147174
MavenSettingsReader.applySettings(settings, session);
148-
RemoteRepository remote = new RemoteRepository.Builder("remote", null, "https://remote").setContentType("default").build();
175+
RemoteRepository remote = new RemoteRepository.Builder("remote", null, "https://remote")
176+
.setContentType("default").build();
149177
RemoteRepository mirror = settings.getMirrorSelector().getMirror(remote);
150178
assertThat(mirror).isNotNull();
151179
}

0 commit comments

Comments
 (0)