Skip to content

Commit 1085dcc

Browse files
committed
Merge pull request #20805 from mikesmithson
* gh-20805: Polish "Split ConventionsPlugin into separate, more focussed classes" Split ConventionsPlugin into separate, more focussed classes Closes gh-20805
2 parents 2d360ef + ba1d4ab commit 1085dcc

File tree

3 files changed

+327
-254
lines changed

3 files changed

+327
-254
lines changed

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

Lines changed: 7 additions & 254 deletions
Original file line numberDiff line numberDiff line change
@@ -16,283 +16,36 @@
1616

1717
package org.springframework.boot.build;
1818

19-
import java.io.File;
20-
import java.io.IOException;
21-
import java.io.InputStream;
22-
import java.io.InputStreamReader;
23-
import java.net.URISyntaxException;
24-
import java.net.URL;
25-
import java.nio.charset.StandardCharsets;
26-
import java.util.List;
27-
import java.util.Map;
28-
import java.util.TreeMap;
29-
import java.util.function.Consumer;
30-
31-
import io.spring.javaformat.gradle.FormatTask;
32-
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
33-
import org.apache.maven.artifact.repository.MavenArtifactRepository;
3419
import org.asciidoctor.gradle.jvm.AsciidoctorJPlugin;
35-
import org.gradle.api.GradleException;
3620
import org.gradle.api.Plugin;
3721
import org.gradle.api.Project;
38-
import org.gradle.api.artifacts.DependencySet;
39-
import org.gradle.api.file.CopySpec;
4022
import org.gradle.api.plugins.JavaBasePlugin;
41-
import org.gradle.api.plugins.JavaPlugin;
42-
import org.gradle.api.plugins.JavaPluginExtension;
43-
import org.gradle.api.plugins.quality.CheckstyleExtension;
44-
import org.gradle.api.plugins.quality.CheckstylePlugin;
45-
import org.gradle.api.publish.PublishingExtension;
46-
import org.gradle.api.publish.maven.MavenPom;
47-
import org.gradle.api.publish.maven.MavenPomDeveloperSpec;
48-
import org.gradle.api.publish.maven.MavenPomIssueManagement;
49-
import org.gradle.api.publish.maven.MavenPomLicenseSpec;
50-
import org.gradle.api.publish.maven.MavenPomOrganization;
51-
import org.gradle.api.publish.maven.MavenPomScm;
52-
import org.gradle.api.publish.maven.MavenPublication;
5323
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
54-
import org.gradle.api.resources.TextResourceFactory;
55-
import org.gradle.api.tasks.bundling.Jar;
56-
import org.gradle.api.tasks.compile.JavaCompile;
57-
import org.gradle.api.tasks.javadoc.Javadoc;
58-
import org.gradle.api.tasks.testing.Test;
59-
import org.gradle.testretry.TestRetryPlugin;
60-
import org.gradle.testretry.TestRetryTaskExtension;
61-
62-
import org.springframework.boot.build.testing.TestFailuresPlugin;
63-
import org.springframework.util.FileCopyUtils;
6424

6525
/**
6626
* Plugin to apply conventions to projects that are part of Spring Boot's build.
6727
* Conventions are applied in response to various plugins being applied.
6828
*
69-
* <p/>
70-
*
71-
* When the {@link JavaBasePlugin Java base plugin} is applied:
72-
*
73-
* <ul>
74-
* <li>{@code sourceCompatibility} is set to {@code 1.8}
75-
* <li>{@link SpringJavaFormatPlugin Spring Java Format}, {@link CheckstylePlugin
76-
* Checkstyle}, {@link TestFailuresPlugin Test Failures}, and {@link TestRetryPlugin Test
77-
* Retry} plugins are applied
78-
* <li>{@link Test} tasks are configured to use JUnit Platform and use a max heap of 1024M
79-
* <li>{@link JavaCompile}, {@link Javadoc}, and {@link FormatTask} tasks are configured
80-
* to use UTF-8 encoding
81-
* <li>{@link JavaCompile} tasks are configured to use {@code -parameters}
82-
* <li>{@link Jar} tasks are configured to produce jars with LICENSE.txt and NOTICE.txt
83-
* files and the following manifest entries:
84-
* <ul>
85-
* <li>{@code Automatic-Module-Name}
86-
* <li>{@code Build-Jdk-Spec}
87-
* <li>{@code Built-By}
88-
* <li>{@code Implementation-Title}
89-
* <li>{@code Implementation-Version}
90-
* </ul>
91-
* </ul>
92-
*
93-
* <p/>
29+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions}
30+
* are applied.
9431
*
95-
* When the {@link MavenPublishPlugin Maven Publish plugin} is applied:
96-
*
97-
* <ul>
98-
* <li>If the {@code deploymentRepository} property has been set, a
99-
* {@link MavenArtifactRepository Maven artifact repository} is configured to publish to
100-
* it.
101-
* <li>The poms of all {@link MavenPublication Maven publications} are customized to meet
102-
* Maven Central's requirements.
103-
* <li>If the {@link JavaPlugin Java plugin} has also been applied, creation of Javadoc
104-
* and source jars is enabled.
105-
* </ul>
106-
*
107-
* <p/>
32+
* When the {@link MavenPublishPlugin} is applied, the conventions in
33+
* {@link MavenPublishingConventions} are applied.
10834
*
10935
* When the {@link AsciidoctorJPlugin} is applied, the conventions in
11036
* {@link AsciidoctorConventions} are applied.
11137
*
11238
* @author Andy Wilkinson
11339
* @author Christoph Dreis
40+
* @author Mike Smithson
11441
*/
11542
public class ConventionsPlugin implements Plugin<Project> {
11643

11744
@Override
11845
public void apply(Project project) {
119-
applyJavaConventions(project);
120-
applyAsciidoctorConventions(project);
121-
applyMavenPublishingConventions(project);
122-
}
123-
124-
private void applyJavaConventions(Project project) {
125-
project.getPlugins().withType(JavaBasePlugin.class, (java) -> {
126-
project.getPlugins().apply(TestFailuresPlugin.class);
127-
configureSpringJavaFormat(project);
128-
project.setProperty("sourceCompatibility", "1.8");
129-
project.getTasks().withType(JavaCompile.class, (compile) -> {
130-
compile.getOptions().setEncoding("UTF-8");
131-
withOptionalBuildJavaHome(project, (javaHome) -> {
132-
compile.getOptions().setFork(true);
133-
compile.getOptions().getForkOptions().setJavaHome(new File(javaHome));
134-
compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac");
135-
});
136-
List<String> args = compile.getOptions().getCompilerArgs();
137-
if (!args.contains("-parameters")) {
138-
args.add("-parameters");
139-
}
140-
});
141-
project.getTasks().withType(Javadoc.class, (javadoc) -> {
142-
javadoc.getOptions().source("1.8").encoding("UTF-8");
143-
withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc"));
144-
});
145-
project.getPlugins().apply(TestRetryPlugin.class);
146-
project.getTasks().withType(Test.class, (test) -> {
147-
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java"));
148-
test.useJUnitPlatform();
149-
test.setMaxHeapSize("1024M");
150-
project.getPlugins().withType(TestRetryPlugin.class, (testRetryPlugin) -> {
151-
TestRetryTaskExtension testRetry = test.getExtensions().getByType(TestRetryTaskExtension.class);
152-
testRetry.getFailOnPassedAfterRetry().set(true);
153-
testRetry.getMaxRetries().set(3);
154-
});
155-
});
156-
project.getTasks().withType(Jar.class, (jar) -> {
157-
project.afterEvaluate((evaluated) -> {
158-
jar.metaInf((metaInf) -> copyLegalFiles(project, metaInf));
159-
jar.manifest((manifest) -> {
160-
Map<String, Object> attributes = new TreeMap<>();
161-
attributes.put("Automatic-Module-Name", project.getName().replace("-", "."));
162-
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility"));
163-
attributes.put("Built-By", "Spring");
164-
attributes.put("Implementation-Title", project.getDescription());
165-
attributes.put("Implementation-Version", project.getVersion());
166-
manifest.attributes(attributes);
167-
});
168-
});
169-
});
170-
});
171-
}
172-
173-
private void copyLegalFiles(Project project, CopySpec metaInf) {
174-
copyNoticeFile(project, metaInf);
175-
copyLicenseFile(project, metaInf);
176-
}
177-
178-
private void copyNoticeFile(Project project, CopySpec metaInf) {
179-
try {
180-
InputStream notice = getClass().getClassLoader().getResourceAsStream("NOTICE.txt");
181-
String noticeContent = FileCopyUtils.copyToString(new InputStreamReader(notice, StandardCharsets.UTF_8))
182-
.replace("${version}", project.getVersion().toString());
183-
TextResourceFactory resourceFactory = project.getResources().getText();
184-
File file = createLegalFile(resourceFactory.fromString(noticeContent).asFile(), "NOTICE.txt");
185-
metaInf.from(file);
186-
}
187-
catch (IOException ex) {
188-
throw new GradleException("Failed to copy NOTICE.txt", ex);
189-
}
190-
}
191-
192-
private void copyLicenseFile(Project project, CopySpec metaInf) {
193-
URL license = getClass().getClassLoader().getResource("LICENSE.txt");
194-
try {
195-
TextResourceFactory resourceFactory = project.getResources().getText();
196-
File file = createLegalFile(resourceFactory.fromUri(license.toURI()).asFile(), "LICENSE.txt");
197-
metaInf.from(file);
198-
}
199-
catch (URISyntaxException ex) {
200-
throw new GradleException("Failed to copy LICENSE.txt", ex);
201-
}
202-
}
203-
204-
private File createLegalFile(File source, String filename) {
205-
File legalFile = new File(source.getParentFile(), filename);
206-
source.renameTo(legalFile);
207-
return legalFile;
208-
}
209-
210-
private void withOptionalBuildJavaHome(Project project, Consumer<String> consumer) {
211-
String buildJavaHome = (String) project.findProperty("buildJavaHome");
212-
if (buildJavaHome != null && !buildJavaHome.isEmpty()) {
213-
consumer.accept(buildJavaHome);
214-
}
215-
}
216-
217-
private void configureSpringJavaFormat(Project project) {
218-
project.getPlugins().apply(SpringJavaFormatPlugin.class);
219-
project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8"));
220-
project.getPlugins().apply(CheckstylePlugin.class);
221-
CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
222-
checkstyle.setToolVersion("8.29");
223-
checkstyle.getConfigDirectory().set(project.getRootProject().file("src/checkstyle"));
224-
String version = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion();
225-
DependencySet checkstyleDependencies = project.getConfigurations().getByName("checkstyle").getDependencies();
226-
checkstyleDependencies
227-
.add(project.getDependencies().create("io.spring.javaformat:spring-javaformat-checkstyle:" + version));
228-
checkstyleDependencies
229-
.add(project.getDependencies().create("io.spring.nohttp:nohttp-checkstyle:0.0.3.RELEASE"));
230-
}
231-
232-
private void applyAsciidoctorConventions(Project project) {
46+
new JavaConventions().apply(project);
47+
new MavenPublishingConventions().apply(project);
23348
new AsciidoctorConventions().apply(project);
23449
}
23550

236-
private void applyMavenPublishingConventions(Project project) {
237-
project.getPlugins().withType(MavenPublishPlugin.class).all((mavenPublish) -> {
238-
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
239-
if (project.hasProperty("deploymentRepository")) {
240-
publishing.getRepositories().maven((mavenRepository) -> {
241-
mavenRepository.setUrl(project.property("deploymentRepository"));
242-
mavenRepository.setName("deployment");
243-
});
244-
}
245-
publishing.getPublications().withType(MavenPublication.class)
246-
.all((mavenPublication) -> customizePom(mavenPublication.getPom(), project));
247-
project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> {
248-
JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class);
249-
extension.withJavadocJar();
250-
extension.withSourcesJar();
251-
});
252-
});
253-
}
254-
255-
private void customizePom(MavenPom pom, Project project) {
256-
pom.getUrl().set("https://projects.spring.io/spring-boot/#");
257-
pom.getDescription().set(project.provider(project::getDescription));
258-
pom.organization(this::customizeOrganization);
259-
pom.licenses(this::customizeLicences);
260-
pom.developers(this::customizeDevelopers);
261-
pom.scm(this::customizeScm);
262-
pom.issueManagement(this::customizeIssueManagement);
263-
}
264-
265-
private void customizeOrganization(MavenPomOrganization organization) {
266-
organization.getName().set("Pivotal Software, Inc.");
267-
organization.getUrl().set("https://spring.io");
268-
}
269-
270-
private void customizeLicences(MavenPomLicenseSpec licences) {
271-
licences.license((licence) -> {
272-
licence.getName().set("Apache License, Version 2.0");
273-
licence.getUrl().set("http://www.apache.org/licenses/LICENSE-2.0");
274-
});
275-
}
276-
277-
private void customizeDevelopers(MavenPomDeveloperSpec developers) {
278-
developers.developer((developer) -> {
279-
developer.getName().set("Pivotal");
280-
developer.getEmail().set("[email protected]");
281-
developer.getOrganization().set("Pivotal Software, Inc.");
282-
developer.getOrganizationUrl().set("https://www.spring.io");
283-
});
284-
}
285-
286-
private void customizeScm(MavenPomScm scm) {
287-
scm.getConnection().set("scm:git:git://github.com/spring-projects/spring-boot.git");
288-
scm.getDeveloperConnection().set("scm:git:ssh://[email protected]/spring-projects/spring-boot.git");
289-
scm.getUrl().set("https://github.com/spring-projects/spring-boot");
290-
291-
}
292-
293-
private void customizeIssueManagement(MavenPomIssueManagement issueManagement) {
294-
issueManagement.getSystem().set("GitHub");
295-
issueManagement.getUrl().set("https://github.com/spring-projects/spring-boot/issues");
296-
}
297-
29851
}

0 commit comments

Comments
 (0)