Skip to content

Commit e0be40c

Browse files
committed
Polish "Locate additional metadata when using Gradle 4"
Closes gh-9732
1 parent 980b83c commit e0be40c

File tree

2 files changed

+117
-29
lines changed
  • spring-boot-tools/spring-boot-configuration-processor/src

2 files changed

+117
-29
lines changed

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

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileInputStream;
21+
import java.io.FileNotFoundException;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.OutputStream;
@@ -110,37 +111,33 @@ private FileObject createMetadataResource() throws IOException {
110111

111112
private InputStream getAdditionalMetadataStream() throws IOException {
112113
// Most build systems will have copied the file to the class output location
113-
FileObject fileObject = this.environment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
114-
115-
File file = new File(fileObject.toUri());
116-
117-
if (!file.exists()) {
118-
// Gradle keeps things separate
119-
String path = file.getPath();
120-
121-
int index = path.lastIndexOf(CLASSES_FOLDER);
122-
if (index >= 0) {
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);
139-
}
140-
141-
}
114+
FileObject fileObject = this.environment.getFiler()
115+
.getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
116+
File file = locateAdditionalMetadataFile(new File(fileObject.toUri()));
142117
return (file.exists() ? new FileInputStream(file)
143118
: fileObject.toUri().toURL().openStream());
144119
}
145120

121+
File locateAdditionalMetadataFile(File standardLocation) throws IOException {
122+
if (standardLocation.exists()) {
123+
return standardLocation;
124+
}
125+
return new File(locateGradleResourcesFolder(standardLocation),
126+
ADDITIONAL_METADATA_PATH);
127+
}
128+
129+
private File locateGradleResourcesFolder(File standardAdditionalMetadataLocation)
130+
throws FileNotFoundException {
131+
String path = standardAdditionalMetadataLocation.getPath();
132+
int index = path.lastIndexOf(CLASSES_FOLDER);
133+
if (index < 0) {
134+
throw new FileNotFoundException();
135+
}
136+
String buildFolderPath = path.substring(0, index);
137+
File classOutputLocation = standardAdditionalMetadataLocation.getParentFile()
138+
.getParentFile();
139+
return new File(buildFolderPath,
140+
RESOURCES_FOLDER + '/' + classOutputLocation.getName());
141+
}
142+
146143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-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.configurationprocessor;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import javax.annotation.processing.ProcessingEnvironment;
23+
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.TemporaryFolder;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.Mockito.mock;
30+
31+
/**
32+
* Tests for {@link MetadataStore}.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
public class MetadataStoreTests {
37+
38+
@Rule
39+
public final TemporaryFolder temp = new TemporaryFolder();
40+
41+
private final MetadataStore metadataStore = new MetadataStore(
42+
mock(ProcessingEnvironment.class));
43+
44+
@Test
45+
public void additionalMetadataIsLocatedInMavenBuild() throws IOException {
46+
File app = this.temp.newFolder("app");
47+
File classesLocation = new File(app, "target/classes");
48+
File metaInf = new File(classesLocation, "META-INF");
49+
metaInf.mkdirs();
50+
File additionalMetadata = new File(metaInf,
51+
"additional-spring-configuration-metadata.json");
52+
additionalMetadata.createNewFile();
53+
assertThat(
54+
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
55+
"META-INF/additional-spring-configuration-metadata.json")))
56+
.isEqualTo(additionalMetadata);
57+
}
58+
59+
@Test
60+
public void additionalMetadataIsLocatedInGradle3Build() throws IOException {
61+
File app = this.temp.newFolder("app");
62+
File classesLocation = new File(app, "build/classes/main");
63+
File resourcesLocation = new File(app, "build/resources/main");
64+
File metaInf = new File(resourcesLocation, "META-INF");
65+
metaInf.mkdirs();
66+
File additionalMetadata = new File(metaInf,
67+
"additional-spring-configuration-metadata.json");
68+
additionalMetadata.createNewFile();
69+
assertThat(
70+
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
71+
"META-INF/additional-spring-configuration-metadata.json")))
72+
.isEqualTo(additionalMetadata);
73+
}
74+
75+
@Test
76+
public void additionalMetadataIsLocatedInGradle4Build() throws IOException {
77+
File app = this.temp.newFolder("app");
78+
File classesLocation = new File(app, "build/classes/java/main");
79+
File resourcesLocation = new File(app, "build/resources/main");
80+
File metaInf = new File(resourcesLocation, "META-INF");
81+
metaInf.mkdirs();
82+
File additionalMetadata = new File(metaInf,
83+
"additional-spring-configuration-metadata.json");
84+
additionalMetadata.createNewFile();
85+
assertThat(
86+
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
87+
"META-INF/additional-spring-configuration-metadata.json")))
88+
.isEqualTo(additionalMetadata);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)