Skip to content

Commit 3e8cafa

Browse files
committed
Add support for spring-devtools.properties
Allow `META-INF/spring-devtools.properties` files to be used by application developers to declare is specific jars should be included or excluded from the RestartClassLoader. A typical example where this might be used is a company that develops it's own set of internal JARs that are used by developers but not usually imported into their IDE. See gh-3316
1 parent 78f739d commit 3e8cafa

File tree

6 files changed

+212
-28
lines changed

6 files changed

+212
-28
lines changed

spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import java.util.Collections;
2424
import java.util.Iterator;
2525
import java.util.List;
26-
import java.util.regex.Pattern;
26+
27+
import org.springframework.boot.devtools.settings.DevToolsSettings;
2728

2829
/**
2930
* A filtered collections of URLs which can be change after the application has started.
@@ -32,48 +33,24 @@
3233
*/
3334
final class ChangeableUrls implements Iterable<URL> {
3435

35-
private static final String[] SKIPPED_PROJECTS = { "spring-boot",
36-
"spring-boot-devtools", "spring-boot-autoconfigure", "spring-boot-actuator",
37-
"spring-boot-starter" };
38-
39-
private static final Pattern STARTER_PATTERN = Pattern
40-
.compile("\\/spring-boot-starter-[\\w-]+\\/");
41-
4236
private final List<URL> urls;
4337

4438
private ChangeableUrls(URL... urls) {
39+
DevToolsSettings settings = DevToolsSettings.get();
4540
List<URL> reloadableUrls = new ArrayList<URL>(urls.length);
4641
for (URL url : urls) {
47-
if (isReloadable(url)) {
42+
if ((settings.isRestartInclude(url) || isFolderUrl(url.toString()))
43+
&& !settings.isRestartExclude(url)) {
4844
reloadableUrls.add(url);
4945
}
5046
}
5147
this.urls = Collections.unmodifiableList(reloadableUrls);
5248
}
5349

54-
private boolean isReloadable(URL url) {
55-
String urlString = url.toString();
56-
return isFolderUrl(urlString) && !isSkipped(urlString);
57-
}
58-
5950
private boolean isFolderUrl(String urlString) {
6051
return urlString.startsWith("file:") && urlString.endsWith("/");
6152
}
6253

63-
private boolean isSkipped(String urlString) {
64-
// Skip certain spring-boot projects to allow them to be imported in the same IDE
65-
for (String skipped : SKIPPED_PROJECTS) {
66-
if (urlString.contains("/" + skipped + "/target/classes/")) {
67-
return true;
68-
}
69-
}
70-
// Skip all starter projects
71-
if (STARTER_PATTERN.matcher(urlString).find()) {
72-
return true;
73-
}
74-
return false;
75-
}
76-
7754
@Override
7855
public Iterator<URL> iterator() {
7956
return this.urls.iterator();
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2012-2015 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.devtools.settings;
18+
19+
import java.net.URL;
20+
import java.util.ArrayList;
21+
import java.util.Enumeration;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.regex.Pattern;
26+
27+
import org.springframework.core.io.UrlResource;
28+
import org.springframework.core.io.support.PropertiesLoaderUtils;
29+
30+
/**
31+
* DevTools settings loaded from {@literal /META-INF/spring-devtools.properties} files.
32+
*
33+
* @author Phillip Webb
34+
* @since 1.3.0
35+
*/
36+
public class DevToolsSettings {
37+
38+
/**
39+
* The location to look for settings properties. Can be present in multiple JAR files.
40+
*/
41+
public static final String SETTINGS_RESOURCE_LOCATION = "META-INF/spring-devtools.properties";
42+
43+
private static DevToolsSettings settings;
44+
45+
private final List<Pattern> restartIncludePatterns = new ArrayList<Pattern>();
46+
47+
private final List<Pattern> restartExcludePatterns = new ArrayList<Pattern>();
48+
49+
DevToolsSettings() {
50+
}
51+
52+
void add(Map<?, ?> properties) {
53+
Map<String, Pattern> includes = getPatterns(properties, "restart.include.");
54+
this.restartIncludePatterns.addAll(includes.values());
55+
Map<String, Pattern> excludes = getPatterns(properties, "restart.exclude.");
56+
this.restartExcludePatterns.addAll(excludes.values());
57+
}
58+
59+
private Map<String, Pattern> getPatterns(Map<?, ?> properties, String prefix) {
60+
Map<String, Pattern> patterns = new LinkedHashMap<String, Pattern>();
61+
for (Map.Entry<?, ?> entry : properties.entrySet()) {
62+
String name = String.valueOf(entry.getKey());
63+
if (name.startsWith(prefix)) {
64+
Pattern pattern = Pattern.compile((String) entry.getValue());
65+
patterns.put(name, pattern);
66+
}
67+
}
68+
return patterns;
69+
}
70+
71+
public boolean isRestartInclude(URL url) {
72+
return isMatch(url.toString(), this.restartIncludePatterns);
73+
}
74+
75+
public boolean isRestartExclude(URL url) {
76+
return isMatch(url.toString(), this.restartExcludePatterns);
77+
}
78+
79+
private boolean isMatch(String url, List<Pattern> patterns) {
80+
for (Pattern pattern : patterns) {
81+
if (pattern.matcher(url).find()) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
88+
public static DevToolsSettings get() {
89+
if (settings == null) {
90+
settings = load();
91+
}
92+
return settings;
93+
}
94+
95+
static DevToolsSettings load() {
96+
return load(SETTINGS_RESOURCE_LOCATION);
97+
}
98+
99+
static DevToolsSettings load(String location) {
100+
try {
101+
DevToolsSettings settings = new DevToolsSettings();
102+
Enumeration<URL> urls = Thread.currentThread().getContextClassLoader()
103+
.getResources(location);
104+
while (urls.hasMoreElements()) {
105+
settings.add(PropertiesLoaderUtils
106+
.loadProperties(new UrlResource(urls.nextElement())));
107+
}
108+
return settings;
109+
}
110+
catch (Exception ex) {
111+
throw new IllegalStateException("Unable to load devtools settings from "
112+
+ "location [" + location + "]", ex);
113+
}
114+
}
115+
116+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
restart.exclude.spring-boot=/spring-boot/target/classes/
2+
restart.exclude.spring-boot-devtools=/spring-boot-devtools/target/classes/
3+
restart.exclude.spring-boot-autoconfigure=/spring-boot-autoconfigure/target/classes/
4+
restart.exclude.spring-boot-actuator=/spring-boot-actuator/target/classes/
5+
restart.exclude.spring-boot-starter=/spring-boot-starter/target/classes/
6+
restart.exclude.spring-boot-starters=/spring-boot-starter-[\\w-]+/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2015 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.devtools.settings;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.net.URL;
22+
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.TemporaryFolder;
26+
27+
import static org.hamcrest.Matchers.equalTo;
28+
import static org.junit.Assert.assertThat;
29+
import static org.junit.Assert.assertTrue;
30+
31+
/**
32+
* Tests for {@link DevToolsSettings}.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
public class DevToolsSettingsTests {
37+
38+
@Rule
39+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
40+
41+
private static final String ROOT = DevToolsSettingsTests.class.getPackage().getName()
42+
.replace(".", "/") + "/";
43+
44+
@Test
45+
public void includePatterns() throws Exception {
46+
DevToolsSettings settings = DevToolsSettings
47+
.load(ROOT + "spring-devtools-include.properties");
48+
assertThat(settings.isRestartInclude(new URL("file://test/a")), equalTo(true));
49+
assertThat(settings.isRestartInclude(new URL("file://test/b")), equalTo(true));
50+
assertThat(settings.isRestartInclude(new URL("file://test/c")), equalTo(false));
51+
}
52+
53+
@Test
54+
public void excludePatterns() throws Exception {
55+
DevToolsSettings settings = DevToolsSettings
56+
.load(ROOT + "spring-devtools-exclude.properties");
57+
assertThat(settings.isRestartExclude(new URL("file://test/a")), equalTo(true));
58+
assertThat(settings.isRestartExclude(new URL("file://test/b")), equalTo(true));
59+
assertThat(settings.isRestartExclude(new URL("file://test/c")), equalTo(false));
60+
}
61+
62+
@Test
63+
public void defaultIncludePatterns() throws Exception {
64+
DevToolsSettings settings = DevToolsSettings.get();
65+
assertTrue(settings.isRestartExclude(makeUrl("spring-boot")));
66+
assertTrue(settings.isRestartExclude(makeUrl("spring-boot-autoconfigure")));
67+
assertTrue(settings.isRestartExclude(makeUrl("spring-boot-actuator")));
68+
assertTrue(settings.isRestartExclude(makeUrl("spring-boot-starter")));
69+
assertTrue(settings.isRestartExclude(makeUrl("spring-boot-starter-some-thing")));
70+
}
71+
72+
private URL makeUrl(String name) throws IOException {
73+
File file = this.temporaryFolder.newFolder();
74+
file = new File(file, name);
75+
file = new File(file, "target");
76+
file = new File(file, "classes");
77+
file.mkdirs();
78+
return file.toURI().toURL();
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
restart.exclude.a=a.*
2+
restart.exclude.b=b.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
restart.include.a=a.*
2+
restart.include.b=b.*

0 commit comments

Comments
 (0)