Skip to content

Commit 36cf3b9

Browse files
committed
Merge branch '2.7.x'
See gh-31988
2 parents c481299 + eb67470 commit 36cf3b9

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
@@ -261,6 +261,8 @@ task runRemoteSpringApplicationExample(type: org.springframework.boot.build.docs
261261
args = ["https://myapp.example.com", "--spring.devtools.remote.secret=secret", "--spring.devtools.livereload.port=0"]
262262
output = file("$buildDir/example-output/remote-spring-application.txt")
263263
expectedLogging = "Started RemoteSpringApplication in "
264+
applicationJar = "/Users/myuser/.m2/repository/org/springframework/boot/spring-boot-devtools/${project.version}/spring-boot-devtools-${project.version}.jar"
265+
normalizeLiveReloadPort()
264266
}
265267

266268
task runSpringApplicationExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@@ -269,6 +271,7 @@ task runSpringApplicationExample(type: org.springframework.boot.build.docs.Appli
269271
args = ["--server.port=0"]
270272
output = file("$buildDir/example-output/spring-application.txt")
271273
expectedLogging = "Started MyApplication in "
274+
normalizeTomcatPort()
272275
}
273276

274277
task runLoggingFormatExample(type: org.springframework.boot.build.docs.ApplicationRunner) {
@@ -277,6 +280,7 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
277280
args = ["--spring.main.banner-mode=off", "--server.port=0"]
278281
output = file("$buildDir/example-output/logging-format.txt")
279282
expectedLogging = "Started MyApplication in "
283+
normalizeTomcatPort()
280284
}
281285

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

0 commit comments

Comments
 (0)