|
| 1 | +/* |
| 2 | + * Copyright 2025 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.build.context.properties; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.StandardOpenOption; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import org.gradle.api.DefaultTask; |
| 30 | +import org.gradle.api.Task; |
| 31 | +import org.gradle.api.file.FileCollection; |
| 32 | +import org.gradle.api.file.RegularFileProperty; |
| 33 | +import org.gradle.api.tasks.InputFiles; |
| 34 | +import org.gradle.api.tasks.OutputFile; |
| 35 | +import org.gradle.api.tasks.PathSensitive; |
| 36 | +import org.gradle.api.tasks.PathSensitivity; |
| 37 | +import org.gradle.api.tasks.TaskAction; |
| 38 | +import org.gradle.api.tasks.VerificationException; |
| 39 | + |
| 40 | +/** |
| 41 | + * {@link Task} that checks aggregated Spring configuration metadata. |
| 42 | + * |
| 43 | + * @author Andy Wilkinson |
| 44 | + */ |
| 45 | +public abstract class CheckAggregatedSpringConfigurationMetadata extends DefaultTask { |
| 46 | + |
| 47 | +private FileCollection configurationPropertyMetadata; |
| 48 | + |
| 49 | +@OutputFile |
| 50 | +public abstract RegularFileProperty getReportLocation(); |
| 51 | + |
| 52 | +@InputFiles |
| 53 | +@PathSensitive(PathSensitivity.RELATIVE) |
| 54 | +public FileCollection getConfigurationPropertyMetadata() { |
| 55 | +return this.configurationPropertyMetadata; |
| 56 | +} |
| 57 | + |
| 58 | +public void setConfigurationPropertyMetadata(FileCollection configurationPropertyMetadata) { |
| 59 | +this.configurationPropertyMetadata = configurationPropertyMetadata; |
| 60 | +} |
| 61 | + |
| 62 | +@TaskAction |
| 63 | +void check() throws IOException { |
| 64 | +Report report = createReport(); |
| 65 | +File reportFile = getReportLocation().get().getAsFile(); |
| 66 | +Files.write(reportFile.toPath(), report, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 67 | +if (report.hasProblems()) { |
| 68 | +throw new VerificationException( |
| 69 | +"Problems found in aggregated Spring configuration metadata. See " + reportFile + " for details."); |
| 70 | +} |
| 71 | +} |
| 72 | + |
| 73 | +private Report createReport() { |
| 74 | +ConfigurationProperties configurationProperties = ConfigurationProperties |
| 75 | +.fromFiles(this.configurationPropertyMetadata); |
| 76 | +Set<String> propertyNames = configurationProperties.stream() |
| 77 | +.map(ConfigurationProperty::getName) |
| 78 | +.collect(Collectors.toSet()); |
| 79 | +List<ConfigurationProperty> missingReplacement = configurationProperties.stream() |
| 80 | +.filter(ConfigurationProperty::isDeprecated) |
| 81 | +.filter((deprecated) -> { |
| 82 | +String replacement = deprecated.getDeprecation().replacement(); |
| 83 | +return replacement != null && !propertyNames.contains(replacement); |
| 84 | +}) |
| 85 | +.toList(); |
| 86 | +return new Report(missingReplacement); |
| 87 | +} |
| 88 | + |
| 89 | +private static final class Report implements Iterable<String> { |
| 90 | + |
| 91 | +private final List<ConfigurationProperty> propertiesWithMissingReplacement; |
| 92 | + |
| 93 | +private Report(List<ConfigurationProperty> propertiesWithMissingReplacement) { |
| 94 | +this.propertiesWithMissingReplacement = propertiesWithMissingReplacement; |
| 95 | +} |
| 96 | + |
| 97 | +private boolean hasProblems() { |
| 98 | +return !this.propertiesWithMissingReplacement.isEmpty(); |
| 99 | +} |
| 100 | + |
| 101 | +@Override |
| 102 | +public Iterator<String> iterator() { |
| 103 | +List<String> lines = new ArrayList<>(); |
| 104 | +if (this.propertiesWithMissingReplacement.isEmpty()) { |
| 105 | +lines.add("No problems found."); |
| 106 | +} |
| 107 | +else { |
| 108 | +lines.add("The following properties have a replacement that does not exist:"); |
| 109 | +lines.add(""); |
| 110 | +lines.addAll(this.propertiesWithMissingReplacement.stream() |
| 111 | +.map((property) -> "\t" + property.getName() + " (replacement " |
| 112 | ++ property.getDeprecation().replacement() + ")") |
| 113 | +.toList()); |
| 114 | +} |
| 115 | +lines.add(""); |
| 116 | +return lines.iterator(); |
| 117 | +} |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +} |
0 commit comments