Skip to content

Commit 47de05b

Browse files
committed
Update Devtools to ignore manifest Class-Path entries that do not exist
Closes gh-8623
1 parent 4563272 commit 47de05b

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,7 +96,7 @@ private static List<URL> getUrlsFromClassPathOfJarManifestIfPossible(URL url) {
9696
return Collections.<URL>emptyList();
9797
}
9898
try {
99-
return getUrlsFromClassPathAttribute(url, jarFile.getManifest());
99+
return getUrlsFromManifestClassPathAttribute(jarFile);
100100
}
101101
catch (IOException ex) {
102102
throw new IllegalStateException(
@@ -118,7 +118,9 @@ private static JarFile getJarFileIfPossible(URL url) {
118118
return null;
119119
}
120120

121-
private static List<URL> getUrlsFromClassPathAttribute(URL base, Manifest manifest) {
121+
private static List<URL> getUrlsFromManifestClassPathAttribute(JarFile jarFile)
122+
throws IOException {
123+
Manifest manifest = jarFile.getManifest();
122124
if (manifest == null) {
123125
return Collections.<URL>emptyList();
124126
}
@@ -129,9 +131,18 @@ private static List<URL> getUrlsFromClassPathAttribute(URL base, Manifest manife
129131
}
130132
String[] entries = StringUtils.delimitedListToStringArray(classPath, " ");
131133
List<URL> urls = new ArrayList<URL>(entries.length);
134+
File parent = new File(jarFile.getName()).getParentFile();
132135
for (String entry : entries) {
133136
try {
134-
urls.add(new URL(base, entry));
137+
File referenced = new File(parent, entry);
138+
if (referenced.exists()) {
139+
urls.add(referenced.toURI().toURL());
140+
}
141+
else {
142+
System.err.println("Ignoring Class-Path entry " + entry + " found in"
143+
+ jarFile.getName() + " as " + referenced
144+
+ " does not exist");
145+
}
135146
}
136147
catch (MalformedURLException ex) {
137148
throw new IllegalStateException(

spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ChangeableUrlsTests.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,15 +74,19 @@ public void skipsUrls() throws Exception {
7474

7575
@Test
7676
public void urlsFromJarClassPathAreConsidered() throws Exception {
77-
URL projectCore = makeUrl("project-core");
78-
URL projectWeb = makeUrl("project-web");
7977
File relative = this.temporaryFolder.newFolder();
78+
File jarWithClassPath = makeJarFileWithUrlsInManifestClassPath(
79+
"project-core/target/classes/", "project-web/target/classes/",
80+
"does-not-exist/target/classes", relative.getName() + "/");
81+
new File(jarWithClassPath.getParentFile(), "project-core/target/classes")
82+
.mkdirs();
83+
new File(jarWithClassPath.getParentFile(), "project-web/target/classes").mkdirs();
8084
ChangeableUrls urls = ChangeableUrls
8185
.fromUrlClassLoader(new URLClassLoader(new URL[] {
82-
makeJarFileWithUrlsInManifestClassPath(projectCore, projectWeb,
83-
relative.getName() + "/"),
84-
makeJarFileWithNoManifest() }));
85-
assertThat(urls.toList()).containsExactly(projectCore, projectWeb,
86+
jarWithClassPath.toURI().toURL(), makeJarFileWithNoManifest() }));
87+
assertThat(urls.toList()).containsExactly(
88+
new URL(jarWithClassPath.toURI().toURL(), "project-core/target/classes/"),
89+
new URL(jarWithClassPath.toURI().toURL(), "project-web/target/classes/"),
8690
relative.toURI().toURL());
8791
}
8892

@@ -95,15 +99,15 @@ private URL makeUrl(String name) throws IOException {
9599
return file.toURI().toURL();
96100
}
97101

98-
private URL makeJarFileWithUrlsInManifestClassPath(Object... urls) throws Exception {
102+
private File makeJarFileWithUrlsInManifestClassPath(Object... urls) throws Exception {
99103
File classpathJar = this.temporaryFolder.newFile("classpath.jar");
100104
Manifest manifest = new Manifest();
101105
manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(),
102106
"1.0");
103107
manifest.getMainAttributes().putValue(Attributes.Name.CLASS_PATH.toString(),
104108
StringUtils.arrayToDelimitedString(urls, " "));
105109
new JarOutputStream(new FileOutputStream(classpathJar), manifest).close();
106-
return classpathJar.toURI().toURL();
110+
return classpathJar;
107111
}
108112

109113
private URL makeJarFileWithNoManifest() throws Exception {

0 commit comments

Comments
 (0)