Skip to content

Commit 980b83c

Browse files
mmoayyedwilkinsona
authored andcommitted
Locate additional metadata when using Gradle 4
Closes gh-9758
1 parent aedeaa9 commit 980b83c

File tree

1 file changed

+21
-5
lines changed
  • spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor

1 file changed

+21
-5
lines changed

spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,34 @@ private FileObject createMetadataResource() throws IOException {
110110

111111
private InputStream getAdditionalMetadataStream() throws IOException {
112112
// Most build systems will have copied the file to the class output location
113-
FileObject fileObject = this.environment.getFiler()
114-
.getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
113+
FileObject fileObject = this.environment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
114+
115115
File file = new File(fileObject.toUri());
116+
116117
if (!file.exists()) {
117118
// Gradle keeps things separate
118119
String path = file.getPath();
120+
119121
int index = path.lastIndexOf(CLASSES_FOLDER);
120122
if (index >= 0) {
121-
path = path.substring(0, index) + RESOURCES_FOLDER
122-
+ path.substring(index + CLASSES_FOLDER.length());
123-
file = new File(path);
123+
/*
124+
Gradle 4 introduces a new 'java' directory which causes issues for one-to-one path mapping
125+
of class locations and resources. Ensure resources can be found under '/build/resources/main'
126+
rather than '/build/resources/java/main'.
127+
*/
128+
final String pathBeforeClassFolder = path.substring(0, index);
129+
/*
130+
In order to retrieve the the class output resource, we MUST pass in a relative path.
131+
An empty path causes a InvalidArgumentException as it must be resolvable. In reality,
132+
this means nothing since we are going to traverse upstream to its parent to locate
133+
the 'main' directory.
134+
*/
135+
FileObject classOutputLocation = this.environment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "dummy");
136+
File classesFolder = new File(classOutputLocation.toUri());
137+
File resourcesFolder = new File(pathBeforeClassFolder + RESOURCES_FOLDER + '/' + classesFolder.getParentFile().getName());
138+
file = new File(resourcesFolder, ADDITIONAL_METADATA_PATH);
124139
}
140+
125141
}
126142
return (file.exists() ? new FileInputStream(file)
127143
: fileObject.toUri().toURL().openStream());

0 commit comments

Comments
 (0)