Skip to content

Commit ffe55c5

Browse files
committed
Check thin.root than user.home for settings.xml
Fixes gh-99
1 parent b8b4dfd commit ffe55c5

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void dispose() {
157157
private DependencyResolver() {
158158
}
159159

160-
private void initialize() {
160+
private void initialize(Properties properties) {
161161
if (this.container == null) {
162162
synchronized (lock) {
163163
if (this.container == null) {
@@ -185,7 +185,7 @@ private void initialize() {
185185
throw new IllegalStateException("Cannot create container", e);
186186
}
187187
this.container = container;
188-
this.settings = new MavenSettingsReader().readSettings();
188+
this.settings = new MavenSettingsReader(properties.getProperty(THIN_ROOT)).readSettings();
189189
}
190190
}
191191
}
@@ -203,7 +203,7 @@ public List<Dependency> dependencies(final Resource resource,
203203
model = ThinPropertiesModelProcessor.process(model, properties);
204204
return aetherDependencies(model.getDependencies(), properties);
205205
}
206-
initialize();
206+
initialize(properties);
207207
try {
208208
log.info("Computing dependencies from pom and properties");
209209
ProjectBuildingRequest request = getProjectBuildingRequest(properties);
@@ -252,7 +252,7 @@ private List<Dependency> aetherDependencies(
252252
Dependency converted = new Dependency(artifact, "runtime");
253253
list.add(converted);
254254
}
255-
initialize();
255+
initialize(properties);
256256
List<ArtifactResult> result = collectNonTransitive(list, properties);
257257
list = new ArrayList<>();
258258
for (ArtifactResult item : result) {
@@ -289,9 +289,10 @@ private String coordinates(Dependency dependency) {
289289
}
290290

291291
public File resolve(Dependency dependency) {
292-
initialize();
292+
Properties properties = new Properties();
293+
initialize(properties);
293294
// TODO: do we need a version of this with non-empty properties?
294-
return collectNonTransitive(Arrays.asList(dependency), new Properties())
295+
return collectNonTransitive(Arrays.asList(dependency), properties)
295296
.iterator().next().getArtifact().getFile();
296297
}
297298

@@ -569,7 +570,7 @@ public Model readModel(Resource resource) {
569570
}
570571

571572
public Model readModel(final Resource resource, final Properties properties) {
572-
initialize();
573+
initialize(properties);
573574
try {
574575
ProjectBuildingRequest request = getProjectBuildingRequest(properties);
575576
request.setResolveDependencies(false);

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

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public MavenSettingsReader() {
5656
}
5757

5858
public MavenSettingsReader(String homeDir) {
59+
if (homeDir==null) {
60+
homeDir = System.getProperty("user.home");
61+
}
5962
this.homeDir = homeDir;
6063
}
6164

@@ -97,7 +100,18 @@ private Settings loadSettings() {
97100
}
98101
else {
99102
log.info("No settings found at: " + settingsFile);
103+
String home = System.getProperty("user.home");
104+
if (!new File(home).getAbsolutePath().equals(new File(this.homeDir).getAbsolutePath())) {
105+
settingsFile = new File(home, ".m2/settings.xml");
106+
if (settingsFile.exists()) {
107+
log.info("Reading settings from: " + settingsFile);
108+
}
109+
}
100110
}
111+
return loadSettings(settingsFile);
112+
}
113+
114+
private Settings loadSettings(File settingsFile) {
101115
SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
102116
request.setUserSettingsFile(settingsFile);
103117
request.setSystemProperties(System.getProperties());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.thin;
18+
19+
import org.junit.Ignore;
20+
import org.junit.runner.RunWith;
21+
import org.junit.runners.Suite;
22+
import org.junit.runners.Suite.SuiteClasses;
23+
24+
/**
25+
* A test suite for probing weird ordering problems in the tests.
26+
*
27+
* @author Dave Syer
28+
*/
29+
@RunWith(Suite.class)
30+
@SuiteClasses({ DependencyResolverSettingsTests.class, ThinJarLauncherTests.class })
31+
@Ignore
32+
public class AdhocTestSuite {
33+
34+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public boolean matches(Dependency value) {
6262

6363
private ProjectBuildingRequest getProjectBuildingRequest(
6464
DependencyResolver resolver) {
65-
ReflectionTestUtils.invokeMethod(resolver, "initialize");
6665
Properties properties = new Properties();
66+
ReflectionTestUtils.invokeMethod(resolver, "initialize", properties);
6767
ProjectBuildingRequest request = ReflectionTestUtils.invokeMethod(resolver,
6868
"getProjectBuildingRequest", properties);
6969
return request;

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.maven.artifact.repository.ArtifactRepository;
2222
import org.apache.maven.project.ProjectBuildingRequest;
23+
import org.assertj.core.api.filter.NotFilter;
2324
import org.junit.After;
2425
import org.junit.Test;
2526

@@ -55,17 +56,28 @@ public void testVanilla() throws Exception {
5556

5657
@Test
5758
public void testProxy() throws Exception {
58-
DependencyResolver.close();
5959
System.setProperty("user.home", "src/test/resources/settings/proxy");
60+
DependencyResolver.close();
6061
DependencyResolver resolver = DependencyResolver.instance();
6162
ProjectBuildingRequest request = getProjectBuildingRequest(resolver);
6263
assertThat(request.getRemoteRepositories()).filteredOnNull("proxy").isEmpty();
6364
}
6465

6566
@Test
66-
public void testLocalRepository() throws Exception {
67+
public void testThinRoot() throws Exception {
68+
Properties properties = new Properties();
69+
properties.setProperty("thin.root", "src/test/resources/settings/proxy");
6770
DependencyResolver.close();
71+
DependencyResolver resolver = DependencyResolver.instance();
72+
ProjectBuildingRequest request = getProjectBuildingRequest(resolver, properties);
73+
assertThat(request.getRemoteRepositories())
74+
.filteredOn("proxy", NotFilter.not(null)).isNotEmpty();
75+
}
76+
77+
@Test
78+
public void testLocalRepository() throws Exception {
6879
System.setProperty("user.home", "src/test/resources/settings/local");
80+
DependencyResolver.close();
6981
DependencyResolver resolver = DependencyResolver.instance();
7082
ProjectBuildingRequest request = getProjectBuildingRequest(resolver);
7183
assertThat(request.getLocalRepository().getUrl())
@@ -74,9 +86,9 @@ public void testLocalRepository() throws Exception {
7486

7587
@Test
7688
public void testSnaphotsEnabledByDefault() throws Exception {
77-
DependencyResolver.close();
7889
System.setProperty("user.home",
7990
"src/test/resources/settings/snapshots/defaultWithNoSnapshotsElement");
91+
DependencyResolver.close();
8092
DependencyResolver resolver = DependencyResolver.instance();
8193
ProjectBuildingRequest request = getProjectBuildingRequest(resolver);
8294
List<ArtifactRepository> repositories = request.getRemoteRepositories();
@@ -87,8 +99,13 @@ public void testSnaphotsEnabledByDefault() throws Exception {
8799

88100
private ProjectBuildingRequest getProjectBuildingRequest(
89101
DependencyResolver resolver) {
90-
ReflectionTestUtils.invokeMethod(resolver, "initialize");
91102
Properties properties = new Properties();
103+
return getProjectBuildingRequest(resolver, properties);
104+
}
105+
106+
private ProjectBuildingRequest getProjectBuildingRequest(DependencyResolver resolver,
107+
Properties properties) {
108+
ReflectionTestUtils.invokeMethod(resolver, "initialize", properties);
92109
ProjectBuildingRequest request = ReflectionTestUtils.invokeMethod(resolver,
93110
"getProjectBuildingRequest", properties);
94111
return request;

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

+2
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ public void emptyProperties() throws Exception {
8989
launcher.launch(args);
9090
verify(resolver).dependencies(any(Resource.class), props.capture());
9191
assertThat(props.getValue()).isEmpty();
92+
DependencyResolver.close();
9293
}
9394

9495
@Test
9596
public void profileProperties() throws Exception {
9697
String[] args = new String[] { "--thin.classpath",
9798
"--thin.archive=src/test/resources/apps/basic", "--thin.profile=foo" };
9899
ThinJarLauncher launcher = new ThinJarLauncher(args);
100+
DependencyResolver.close();
99101
DependencyResolver resolver = mock(DependencyResolver.class);
100102
ReflectionTestUtils.setField(DependencyResolver.class, "instance", resolver);
101103
ArgumentCaptor<Properties> props = ArgumentCaptor.forClass(Properties.class);

0 commit comments

Comments
 (0)