Skip to content

Commit 027093d

Browse files
committed
Use a templated source file for SpringBootVersion
Closes gh-29670
1 parent 1f8700f commit 027093d

File tree

6 files changed

+97
-146
lines changed

6 files changed

+97
-146
lines changed

buildSrc/src/main/java/org/springframework/boot/build/GeneratePropertiesResource.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -104,8 +104,6 @@ class JavaConventions {
104104

105105
private static final String SOURCE_AND_TARGET_COMPATIBILITY = "1.8";
106106

107-
private static final String SPRING_BOOT_PROPERTIES_FILE = "spring-boot.properties";
108-
109107
void apply(Project project) {
110108
project.getPlugins().withType(JavaBasePlugin.class, (java) -> {
111109
project.getPlugins().apply(TestFailuresPlugin.class);
@@ -114,45 +112,36 @@ void apply(Project project) {
114112
configureJavadocConventions(project);
115113
configureTestConventions(project);
116114
configureJarManifestConventions(project);
117-
configureMetaInfResourcesConventions(project);
118115
configureDependencyManagement(project);
119116
configureToolchain(project);
120117
configureProhibitedDependencyChecks(project);
121118
});
122119
}
123120

124121
private void configureJarManifestConventions(Project project) {
125-
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
126-
Set<String> sourceJarTaskNames = sourceSets.stream().map(SourceSet::getSourcesJarTaskName)
127-
.collect(Collectors.toSet());
128-
Set<String> javadocJarTaskNames = sourceSets.stream().map(SourceSet::getJavadocJarTaskName)
129-
.collect(Collectors.toSet());
130-
project.getTasks().withType(Jar.class,
131-
(jar) -> project.afterEvaluate((evaluated) -> jar.manifest((manifest) -> {
132-
Map<String, Object> attributes = new TreeMap<>();
133-
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
134-
attributes.put("Build-Jdk-Spec", SOURCE_AND_TARGET_COMPATIBILITY);
135-
attributes.put("Built-By", "Spring");
136-
attributes.put("Implementation-Title",
137-
determineImplementationTitle(project, sourceJarTaskNames, javadocJarTaskNames, jar));
138-
attributes.put("Implementation-Version", project.getVersion());
139-
manifest.attributes(attributes);
140-
})));
141-
}
142-
143-
private void configureMetaInfResourcesConventions(Project project) {
144122
ExtractResources extractLegalResources = project.getTasks().create("extractLegalResources",
145123
ExtractResources.class);
146124
extractLegalResources.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("legal"));
147125
extractLegalResources.setResourcesNames(Arrays.asList("LICENSE.txt", "NOTICE.txt"));
148126
extractLegalResources.property("version", project.getVersion().toString());
149-
GeneratePropertiesResource generateInfo = project.getTasks().create("generateSpringBootInfo",
150-
GeneratePropertiesResource.class);
151-
generateInfo.getDestinationDirectory().set(project.getLayout().getBuildDirectory().dir("info"));
152-
generateInfo.getPropertiesFileName().set(SPRING_BOOT_PROPERTIES_FILE);
153-
generateInfo.property("version", project.getVersion().toString());
154-
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate(
155-
(evaluated) -> jar.metaInf((metaInf) -> metaInf.from(extractLegalResources, generateInfo))));
127+
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
128+
Set<String> sourceJarTaskNames = sourceSets.stream().map(SourceSet::getSourcesJarTaskName)
129+
.collect(Collectors.toSet());
130+
Set<String> javadocJarTaskNames = sourceSets.stream().map(SourceSet::getJavadocJarTaskName)
131+
.collect(Collectors.toSet());
132+
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
133+
jar.metaInf((metaInf) -> metaInf.from(extractLegalResources));
134+
jar.manifest((manifest) -> {
135+
Map<String, Object> attributes = new TreeMap<>();
136+
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
137+
attributes.put("Build-Jdk-Spec", SOURCE_AND_TARGET_COMPATIBILITY);
138+
attributes.put("Built-By", "Spring");
139+
attributes.put("Implementation-Title",
140+
determineImplementationTitle(project, sourceJarTaskNames, javadocJarTaskNames, jar));
141+
attributes.put("Implementation-Version", project.getVersion());
142+
manifest.attributes(attributes);
143+
});
144+
}));
156145
}
157146

158147
private String determineImplementationTitle(Project project, Set<String> sourceJarTaskNames,

buildSrc/src/test/java/org/springframework/boot/build/ConventionsPluginTests.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ void jarIncludesLegalFiles() throws IOException {
8383
try (JarFile jar = new JarFile(file)) {
8484
assertThatLicenseIsPresent(jar);
8585
assertThatNoticeIsPresent(jar);
86-
assertThatSpringBootPropertiesIsPresent(jar);
8786
Attributes mainAttributes = jar.getManifest().getMainAttributes();
8887
assertThat(mainAttributes.getValue("Implementation-Title"))
8988
.isEqualTo("Test project for manifest customization");
@@ -113,7 +112,6 @@ void sourceJarIsBuilt() throws IOException {
113112
try (JarFile jar = new JarFile(file)) {
114113
assertThatLicenseIsPresent(jar);
115114
assertThatNoticeIsPresent(jar);
116-
assertThatSpringBootPropertiesIsPresent(jar);
117115
Attributes mainAttributes = jar.getManifest().getMainAttributes();
118116
assertThat(mainAttributes.getValue("Implementation-Title"))
119117
.isEqualTo("Source for " + this.projectDir.getName());
@@ -143,7 +141,6 @@ void javadocJarIsBuilt() throws IOException {
143141
try (JarFile jar = new JarFile(file)) {
144142
assertThatLicenseIsPresent(jar);
145143
assertThatNoticeIsPresent(jar);
146-
assertThatSpringBootPropertiesIsPresent(jar);
147144
Attributes mainAttributes = jar.getManifest().getMainAttributes();
148145
assertThat(mainAttributes.getValue("Implementation-Title"))
149146
.isEqualTo("Javadoc for " + this.projectDir.getName());
@@ -168,13 +165,6 @@ private void assertThatNoticeIsPresent(JarFile jar) throws IOException {
168165
assertThat(noticeContent).doesNotContain("${");
169166
}
170167

171-
private void assertThatSpringBootPropertiesIsPresent(JarFile jar) throws IOException {
172-
JarEntry properties = jar.getJarEntry("META-INF/spring-boot.properties");
173-
assertThat(properties).isNotNull();
174-
String content = FileCopyUtils.copyToString(new InputStreamReader(jar.getInputStream(properties)));
175-
assertThat(content).contains("version=");
176-
}
177-
178168
@Test
179169
void testRetryIsConfiguredWithThreeRetriesOnCI() throws IOException {
180170
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {

spring-boot-project/spring-boot/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,26 @@ task extractTomcatConfigProperties(type: Sync) {
155155
}
156156
}
157157

158+
def syncJavaTemplates = tasks.register("syncJavaTemplates", Sync) {
159+
from("src/main/javaTemplates")
160+
into("build/generated-sources/main")
161+
def properties = ["springBootVersion": project.version]
162+
expand(properties)
163+
inputs.properties(properties)
164+
}
165+
166+
plugins.withType(EclipsePlugin) {
167+
eclipse {
168+
synchronizationTasks syncJavaTemplates
169+
}
170+
}
171+
158172
sourceSets {
173+
main {
174+
java {
175+
srcDirs syncJavaTemplates
176+
}
177+
}
159178
test {
160179
output.dir(tomcatConfigProperties, builtBy: "extractTomcatConfigProperties")
161180
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringBootVersion.java renamed to spring-boot-project/spring-boot/src/main/javaTemplates/org/springframework/boot/SpringBootVersion.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616

1717
package org.springframework.boot;
1818

19-
import java.io.IOException;
20-
import java.io.InputStream;
21-
import java.util.Properties;
22-
2319
/**
2420
* Exposes the Spring Boot version.
2521
*
26-
* The version information is read from a file that is stored in the Spring Boot library.
27-
* If the version information cannot be read from the file, consider using a
28-
* reflection-based check instead (for example, checking for the presence of a specific
29-
* Spring Boot method that you intend to call).
30-
*
3122
* @author Drummond Dawson
3223
* @author Hendrig Sellik
3324
* @author Andy Wilkinson
@@ -40,24 +31,11 @@ private SpringBootVersion() {
4031
}
4132

4233
/**
43-
* Return the full version string of the present Spring Boot codebase, or {@code null}
44-
* if it cannot be determined.
45-
* @return the version of Spring Boot or {@code null}
34+
* Return the full version string of the present Spring Boot codebase.
35+
* @return the version of Spring Boot
4636
*/
4737
public static String getVersion() {
48-
InputStream input = SpringBootVersion.class.getClassLoader()
49-
.getResourceAsStream("META-INF/spring-boot.properties");
50-
if (input != null) {
51-
try {
52-
Properties properties = new Properties();
53-
properties.load(input);
54-
return properties.getProperty("version");
55-
}
56-
catch (IOException ex) {
57-
// fall through
58-
}
59-
}
60-
return null;
38+
return "${springBootVersion}";
6139
}
6240

6341
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.core.io.FileSystemResource;
25+
import org.springframework.core.io.support.PropertiesLoaderUtils;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link SpringBootVersion}.
31+
*
32+
* @author Andy Wilkinson
33+
*/
34+
public class SpringBootVersionTests {
35+
36+
@Test
37+
void getVersionShouldReturnVersionMatchingGradleProperties() throws IOException {
38+
String expectedVersion = PropertiesLoaderUtils.loadProperties(new FileSystemResource(findGradleProperties()))
39+
.getProperty("version");
40+
assertThat(SpringBootVersion.getVersion()).isEqualTo(expectedVersion);
41+
}
42+
43+
private File findGradleProperties() {
44+
File current = new File(".").getAbsoluteFile();
45+
while (current != null) {
46+
File gradleProperties = new File(current, "gradle.properties");
47+
System.out.println(gradleProperties);
48+
if (gradleProperties.isFile()) {
49+
return gradleProperties;
50+
}
51+
current = current.getParentFile();
52+
}
53+
throw new IllegalStateException("Could not find gradle.properties");
54+
}
55+
56+
}

0 commit comments

Comments
 (0)