Skip to content

Commit eb67470

Browse files
committed
Merge branch '2.6.x' into 2.7.x
See gh-31987
2 parents d8e6d9b + b905d7f commit eb67470

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

buildSrc/src/main/java/org/springframework/boot/build/docs/ApplicationRunner.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
2323
import java.util.ArrayList;
24+
import java.util.HashMap;
2425
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Map.Entry;
28+
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
2530
import java.util.stream.Collectors;
2631

2732
import org.gradle.api.DefaultTask;
@@ -52,6 +57,11 @@ public class ApplicationRunner extends DefaultTask {
5257

5358
private final Property<String> expectedLogging = getProject().getObjects().property(String.class);
5459

60+
private final Property<String> applicationJar = getProject().getObjects().property(String.class)
61+
.convention("/opt/apps/myapp.jar");
62+
63+
private final Map<String, String> normalizations = new HashMap<>();
64+
5565
private FileCollection classpath;
5666

5767
@OutputFile
@@ -83,6 +93,25 @@ public Property<String> getExpectedLogging() {
8393
return this.expectedLogging;
8494
}
8595

96+
@Input
97+
Map<String, String> getNormalizations() {
98+
return this.normalizations;
99+
}
100+
101+
@Input
102+
public Property<String> getApplicationJar() {
103+
return this.applicationJar;
104+
}
105+
106+
public void normalizeTomcatPort() {
107+
this.normalizations.put("(Tomcat started on port\\(s\\): )[\\d]+( \\(http\\))", "$18080$2");
108+
this.normalizations.put("(Tomcat initialized with port\\(s\\): )[\\d]+( \\(http\\))", "$18080$2");
109+
}
110+
111+
public void normalizeLiveReloadPort() {
112+
this.normalizations.put("(LiveReload server is running on port )[\\d]+", "$135729");
113+
}
114+
86115
@TaskAction
87116
void runApplication() throws IOException {
88117
List<String> command = new ArrayList<>();
@@ -98,6 +127,7 @@ void runApplication() throws IOException {
98127
.start();
99128
awaitLogging(process);
100129
process.destroy();
130+
normalizeLogging();
101131
}
102132

103133
private void awaitLogging(Process process) {
@@ -126,4 +156,57 @@ private List<String> outputLines() {
126156
}
127157
}
128158

159+
private void normalizeLogging() {
160+
List<String> outputLines = outputLines();
161+
List<String> normalizedLines = normalize(outputLines);
162+
Path outputPath = this.output.get().getAsFile().toPath();
163+
try {
164+
Files.write(outputPath, normalizedLines);
165+
}
166+
catch (IOException ex) {
167+
throw new RuntimeException("Failed to write normalized lines of output to '" + outputPath + "'", ex);
168+
}
169+
}
170+
171+
private List<String> normalize(List<String> lines) {
172+
List<String> normalizedLines = lines;
173+
Map<String, String> normalizations = new HashMap<>(this.normalizations);
174+
normalizations.put("(Starting .* using Java .* on ).*( with PID [\\d]+ \\().*( started by ).*( in ).*(\\))",
175+
"$1myhost$2" + this.applicationJar.get() + "$3myuser$4/opt/apps/$5");
176+
for (Entry<String, String> normalization : normalizations.entrySet()) {
177+
Pattern pattern = Pattern.compile(normalization.getKey());
178+
normalizedLines = normalize(normalizedLines, pattern, normalization.getValue());
179+
}
180+
return normalizedLines;
181+
}
182+
183+
private List<String> normalize(List<String> lines, Pattern pattern, String replacement) {
184+
boolean matched = false;
185+
List<String> normalizedLines = new ArrayList<>();
186+
for (String line : lines) {
187+
Matcher matcher = pattern.matcher(line);
188+
StringBuffer transformed = new StringBuffer();
189+
while (matcher.find()) {
190+
matched = true;
191+
matcher.appendReplacement(transformed, replacement);
192+
}
193+
matcher.appendTail(transformed);
194+
normalizedLines.add(transformed.toString());
195+
}
196+
if (!matched) {
197+
reportUnmatchedNormalization(lines, pattern);
198+
}
199+
return normalizedLines;
200+
}
201+
202+
private void reportUnmatchedNormalization(List<String> lines, Pattern pattern) {
203+
StringBuilder message = new StringBuilder(
204+
"'" + pattern + "' did not match any of the following lines of output:");
205+
message.append(String.format("%n"));
206+
for (String line : lines) {
207+
message.append(String.format("%s%n", line));
208+
}
209+
throw new IllegalStateException(message.toString());
210+
}
211+
129212
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ task runRemoteSpringApplicationExample(type: org.springframework.boot.build.docs
278278
args = ["https://myapp.example.com", "--spring.devtools.remote.secret=secret", "--spring.devtools.livereload.port=0"]
279279
output = file("$buildDir/example-output/remote-spring-application.txt")
280280
expectedLogging = "Started RemoteSpringApplication in "
281+
applicationJar = "/Users/myuser/.m2/repository/org/springframework/boot/spring-boot-devtools/${project.version}/spring-boot-devtools-${project.version}.jar"
282+
normalizeLiveReloadPort()
281283
}
282284

283285
task runSpringApplicationExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@@ -286,6 +288,7 @@ task runSpringApplicationExample(type: org.springframework.boot.build.docs.Appli
286288
args = ["--server.port=0"]
287289
output = file("$buildDir/example-output/spring-application.txt")
288290
expectedLogging = "Started MyApplication in "
291+
normalizeTomcatPort()
289292
}
290293

291294
task runLoggingFormatExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@@ -294,6 +297,7 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
294297
args = ["--spring.main.banner-mode=off", "--server.port=0"]
295298
output = file("$buildDir/example-output/logging-format.txt")
296299
expectedLogging = "Started MyApplication in "
300+
normalizeTomcatPort()
297301
}
298302

299303
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {

0 commit comments

Comments
 (0)