-
Notifications
You must be signed in to change notification settings - Fork 41.3k
Refactor ConventionsPlugin class #20805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b5b4b60
8f2738a
c07a044
13e7406
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,16 +39,8 @@ | |
import org.gradle.api.file.CopySpec; | ||
import org.gradle.api.plugins.JavaBasePlugin; | ||
import org.gradle.api.plugins.JavaPlugin; | ||
import org.gradle.api.plugins.JavaPluginExtension; | ||
import org.gradle.api.plugins.quality.CheckstyleExtension; | ||
import org.gradle.api.plugins.quality.CheckstylePlugin; | ||
import org.gradle.api.publish.PublishingExtension; | ||
import org.gradle.api.publish.maven.MavenPom; | ||
import org.gradle.api.publish.maven.MavenPomDeveloperSpec; | ||
import org.gradle.api.publish.maven.MavenPomIssueManagement; | ||
import org.gradle.api.publish.maven.MavenPomLicenseSpec; | ||
import org.gradle.api.publish.maven.MavenPomOrganization; | ||
import org.gradle.api.publish.maven.MavenPomScm; | ||
import org.gradle.api.publish.maven.MavenPublication; | ||
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; | ||
import org.gradle.api.resources.TextResourceFactory; | ||
|
@@ -111,6 +103,7 @@ | |
* | ||
* @author Andy Wilkinson | ||
* @author Christoph Dreis | ||
* @author Mike Smithson | ||
*/ | ||
public class ConventionsPlugin implements Plugin<Project> { | ||
|
||
|
@@ -123,53 +116,81 @@ public void apply(Project project) { | |
|
||
private void applyJavaConventions(Project project) { | ||
project.getPlugins().withType(JavaBasePlugin.class, (java) -> { | ||
project.getPlugins().apply(TestFailuresPlugin.class); | ||
applyTestFailuresPlugin(project); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that applying the plugin is a single line and wrapping it in a method doesn't make the intent any clearer, I'd prefer to keep it inlined here. |
||
configureSpringJavaFormat(project); | ||
project.setProperty("sourceCompatibility", "1.8"); | ||
project.getTasks().withType(JavaCompile.class, (compile) -> { | ||
compile.getOptions().setEncoding("UTF-8"); | ||
withOptionalBuildJavaHome(project, (javaHome) -> { | ||
compile.getOptions().setFork(true); | ||
compile.getOptions().getForkOptions().setJavaHome(new File(javaHome)); | ||
compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac"); | ||
setSourceCompatibility(project); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to set the property directly here rather than wrapping it in a method call. |
||
configureJavaCompileConventions(project); | ||
configureJavadocConventions(project); | ||
configureTestConventions(project); | ||
configureJarManifestConventions(project); | ||
}); | ||
} | ||
|
||
private void applyMavenPublishingConventions(Project project) { | ||
new MavenPomConventions().apply(project); | ||
} | ||
|
||
private void configureJarManifestConventions(Project project) { | ||
project.getTasks().withType(Jar.class, (jar) -> { | ||
project.afterEvaluate((evaluated) -> { | ||
jar.metaInf((metaInf) -> copyLegalFiles(project, metaInf)); | ||
jar.manifest((manifest) -> { | ||
Map<String, Object> attributes = new TreeMap<>(); | ||
attributes.put("Automatic-Module-Name", project.getName().replace("-", ".")); | ||
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility")); | ||
attributes.put("Built-By", "Spring"); | ||
attributes.put("Implementation-Title", project.getDescription()); | ||
attributes.put("Implementation-Version", project.getVersion()); | ||
manifest.attributes(attributes); | ||
}); | ||
List<String> args = compile.getOptions().getCompilerArgs(); | ||
if (!args.contains("-parameters")) { | ||
args.add("-parameters"); | ||
} | ||
}); | ||
project.getTasks().withType(Javadoc.class, (javadoc) -> { | ||
javadoc.getOptions().source("1.8").encoding("UTF-8"); | ||
withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc")); | ||
}); | ||
project.getPlugins().apply(TestRetryPlugin.class); | ||
project.getTasks().withType(Test.class, (test) -> { | ||
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); | ||
test.useJUnitPlatform(); | ||
test.setMaxHeapSize("1024M"); | ||
project.getPlugins().withType(TestRetryPlugin.class, (testRetryPlugin) -> { | ||
TestRetryTaskExtension testRetry = test.getExtensions().getByType(TestRetryTaskExtension.class); | ||
testRetry.getFailOnPassedAfterRetry().set(true); | ||
testRetry.getMaxRetries().set(3); | ||
}); | ||
}); | ||
} | ||
|
||
private void configureTestConventions(Project project) { | ||
project.getPlugins().apply(TestRetryPlugin.class); | ||
project.getTasks().withType(Test.class, (test) -> { | ||
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); | ||
test.useJUnitPlatform(); | ||
test.setMaxHeapSize("1024M"); | ||
project.getPlugins().withType(TestRetryPlugin.class, (testRetryPlugin) -> { | ||
TestRetryTaskExtension testRetry = test.getExtensions().getByType(TestRetryTaskExtension.class); | ||
testRetry.getFailOnPassedAfterRetry().set(true); | ||
testRetry.getMaxRetries().set(3); | ||
}); | ||
project.getTasks().withType(Jar.class, (jar) -> { | ||
project.afterEvaluate((evaluated) -> { | ||
jar.metaInf((metaInf) -> copyLegalFiles(project, metaInf)); | ||
jar.manifest((manifest) -> { | ||
Map<String, Object> attributes = new TreeMap<>(); | ||
attributes.put("Automatic-Module-Name", project.getName().replace("-", ".")); | ||
attributes.put("Build-Jdk-Spec", project.property("sourceCompatibility")); | ||
attributes.put("Built-By", "Spring"); | ||
attributes.put("Implementation-Title", project.getDescription()); | ||
attributes.put("Implementation-Version", project.getVersion()); | ||
manifest.attributes(attributes); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
private void configureJavadocConventions(Project project) { | ||
project.getTasks().withType(Javadoc.class, (javadoc) -> { | ||
javadoc.getOptions().source("1.8").encoding("UTF-8"); | ||
withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc")); | ||
}); | ||
} | ||
|
||
private void configureJavaCompileConventions(Project project) { | ||
project.getTasks().withType(JavaCompile.class, (compile) -> { | ||
compile.getOptions().setEncoding("UTF-8"); | ||
withOptionalBuildJavaHome(project, (javaHome) -> { | ||
compile.getOptions().setFork(true); | ||
compile.getOptions().getForkOptions().setJavaHome(new File(javaHome)); | ||
compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac"); | ||
}); | ||
List<String> args = compile.getOptions().getCompilerArgs(); | ||
if (!args.contains("-parameters")) { | ||
args.add("-parameters"); | ||
} | ||
}); | ||
} | ||
|
||
private void setSourceCompatibility(Project project) { | ||
project.setProperty("sourceCompatibility", "1.8"); | ||
} | ||
|
||
private void applyTestFailuresPlugin(Project project) { | ||
project.getPlugins().apply(TestFailuresPlugin.class); | ||
} | ||
|
||
private void copyLegalFiles(Project project, CopySpec metaInf) { | ||
copyNoticeFile(project, metaInf); | ||
copyLicenseFile(project, metaInf); | ||
|
@@ -233,66 +254,4 @@ private void applyAsciidoctorConventions(Project project) { | |
new AsciidoctorConventions().apply(project); | ||
} | ||
|
||
private void applyMavenPublishingConventions(Project project) { | ||
project.getPlugins().withType(MavenPublishPlugin.class).all((mavenPublish) -> { | ||
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); | ||
if (project.hasProperty("deploymentRepository")) { | ||
publishing.getRepositories().maven((mavenRepository) -> { | ||
mavenRepository.setUrl(project.property("deploymentRepository")); | ||
mavenRepository.setName("deployment"); | ||
}); | ||
} | ||
publishing.getPublications().withType(MavenPublication.class) | ||
.all((mavenPublication) -> customizePom(mavenPublication.getPom(), project)); | ||
project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> { | ||
JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class); | ||
extension.withJavadocJar(); | ||
extension.withSourcesJar(); | ||
}); | ||
}); | ||
} | ||
|
||
private void customizePom(MavenPom pom, Project project) { | ||
pom.getUrl().set("https://projects.spring.io/spring-boot/#"); | ||
pom.getDescription().set(project.provider(project::getDescription)); | ||
pom.organization(this::customizeOrganization); | ||
pom.licenses(this::customizeLicences); | ||
pom.developers(this::customizeDevelopers); | ||
pom.scm(this::customizeScm); | ||
pom.issueManagement(this::customizeIssueManagement); | ||
} | ||
|
||
private void customizeOrganization(MavenPomOrganization organization) { | ||
organization.getName().set("Pivotal Software, Inc."); | ||
organization.getUrl().set("https://spring.io"); | ||
} | ||
|
||
private void customizeLicences(MavenPomLicenseSpec licences) { | ||
licences.license((licence) -> { | ||
licence.getName().set("Apache License, Version 2.0"); | ||
licence.getUrl().set("http://www.apache.org/licenses/LICENSE-2.0"); | ||
}); | ||
} | ||
|
||
private void customizeDevelopers(MavenPomDeveloperSpec developers) { | ||
developers.developer((developer) -> { | ||
developer.getName().set("Pivotal"); | ||
developer.getEmail().set("[email protected]"); | ||
developer.getOrganization().set("Pivotal Software, Inc."); | ||
developer.getOrganizationUrl().set("https://www.spring.io"); | ||
}); | ||
} | ||
|
||
private void customizeScm(MavenPomScm scm) { | ||
scm.getConnection().set("scm:git:git://github.com/spring-projects/spring-boot.git"); | ||
scm.getDeveloperConnection().set("scm:git:ssh://[email protected]/spring-projects/spring-boot.git"); | ||
scm.getUrl().set("https://github.com/spring-projects/spring-boot"); | ||
|
||
} | ||
|
||
private void customizeIssueManagement(MavenPomIssueManagement issueManagement) { | ||
issueManagement.getSystem().set("GitHub"); | ||
issueManagement.getUrl().set("https://github.com/spring-projects/spring-boot/issues"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.build; | ||
|
||
import org.apache.maven.artifact.repository.MavenArtifactRepository; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.plugins.JavaPlugin; | ||
import org.gradle.api.plugins.JavaPluginExtension; | ||
import org.gradle.api.publish.PublishingExtension; | ||
import org.gradle.api.publish.maven.MavenPom; | ||
import org.gradle.api.publish.maven.MavenPomDeveloperSpec; | ||
import org.gradle.api.publish.maven.MavenPomIssueManagement; | ||
import org.gradle.api.publish.maven.MavenPomLicenseSpec; | ||
import org.gradle.api.publish.maven.MavenPomOrganization; | ||
import org.gradle.api.publish.maven.MavenPomScm; | ||
import org.gradle.api.publish.maven.MavenPublication; | ||
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; | ||
|
||
/** | ||
* Plugin to apply Maven Publishing conventions to projects that are part of Spring Boot's | ||
* build. Conventions are applied in response to various plugins being applied. | ||
* | ||
* When the {@link MavenPublishPlugin Maven Publish plugin} is applied: | ||
* | ||
* <ul> | ||
* <li>If the {@code deploymentRepository} property has been set, a | ||
* {@link MavenArtifactRepository Maven artifact repository} is configured to publish to | ||
* it. | ||
* <li>The poms of all {@link MavenPublication Maven publications} are customized to meet | ||
* Maven Central's requirements. | ||
* <li>If the {@link JavaPlugin Java plugin} has also been applied, creation of Javadoc | ||
* and source jars is enabled. | ||
* </ul> | ||
* | ||
* <p/> | ||
* | ||
* @author Mike Smithson | ||
*/ | ||
public class MavenPomConventions implements Plugin<Project> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class doesn't need to be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class applies more than pom conventions. I think |
||
|
||
@Override | ||
public void apply(Project project) { | ||
project.getPlugins().withType(MavenPublishPlugin.class).all((mavenPublish) -> { | ||
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); | ||
if (project.hasProperty("deploymentRepository")) { | ||
publishing.getRepositories().maven((mavenRepository) -> { | ||
mavenRepository.setUrl(project.property("deploymentRepository")); | ||
mavenRepository.setName("deployment"); | ||
}); | ||
} | ||
publishing.getPublications().withType(MavenPublication.class) | ||
.all((mavenPublication) -> customizePom(mavenPublication.getPom(), project)); | ||
project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> { | ||
JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class); | ||
extension.withJavadocJar(); | ||
extension.withSourcesJar(); | ||
}); | ||
}); | ||
} | ||
|
||
private void customizePom(MavenPom pom, Project project) { | ||
pom.getUrl().set("https://projects.spring.io/spring-boot/#"); | ||
pom.getDescription().set(project.provider(project::getDescription)); | ||
pom.organization(this::customizeOrganization); | ||
pom.licenses(this::customizeLicences); | ||
pom.developers(this::customizeDevelopers); | ||
pom.scm(this::customizeScm); | ||
pom.issueManagement(this::customizeIssueManagement); | ||
} | ||
|
||
private void customizeOrganization(MavenPomOrganization organization) { | ||
organization.getName().set("Pivotal Software, Inc."); | ||
organization.getUrl().set("https://spring.io"); | ||
} | ||
|
||
private void customizeLicences(MavenPomLicenseSpec licences) { | ||
licences.license((licence) -> { | ||
licence.getName().set("Apache License, Version 2.0"); | ||
licence.getUrl().set("http://www.apache.org/licenses/LICENSE-2.0"); | ||
}); | ||
} | ||
|
||
private void customizeDevelopers(MavenPomDeveloperSpec developers) { | ||
developers.developer((developer) -> { | ||
developer.getName().set("Pivotal"); | ||
developer.getEmail().set("[email protected]"); | ||
developer.getOrganization().set("Pivotal Software, Inc."); | ||
developer.getOrganizationUrl().set("https://www.spring.io"); | ||
}); | ||
} | ||
|
||
private void customizeScm(MavenPomScm scm) { | ||
scm.getConnection().set("scm:git:git://github.com/spring-projects/spring-boot.git"); | ||
scm.getDeveloperConnection().set("scm:git:ssh://[email protected]/spring-projects/spring-boot.git"); | ||
scm.getUrl().set("https://github.com/spring-projects/spring-boot"); | ||
|
||
} | ||
|
||
private void customizeIssueManagement(MavenPomIssueManagement issueManagement) { | ||
issueManagement.getSystem().set("GitHub"); | ||
issueManagement.getUrl().set("https://github.com/spring-projects/spring-boot/issues"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The javadoc above should be updated to replace the list of actions when the
MavenPublishPlugin
is applied and to link to the new Maven conventions class instead.