diff --git a/gradle-enterprise-conventions-core/build.gradle b/gradle-enterprise-conventions-core/build.gradle index 264c0b9..45fdbb3 100644 --- a/gradle-enterprise-conventions-core/build.gradle +++ b/gradle-enterprise-conventions-core/build.gradle @@ -1,91 +1,25 @@ plugins { - id "checkstyle" - id "io.spring.javaformat" version "$javaFormatVersion" + id "build-conventions" id "java" id "maven-publish" } description = "Gradle Enterprise Conventions Core" -group = 'io.spring.ge.conventions' - -sourceCompatibility = 1.8 -targetCompatibility = 1.8 repositories { mavenCentral() } dependencies { - checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}") - testImplementation("org.assertj:assertj-core:3.24.2") testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") testImplementation("org.mockito:mockito-core:4.11.0") } -test { - useJUnitPlatform() -} - -java { - withJavadocJar() - withSourcesJar() -} - -checkstyle { - def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")} - config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml") - toolVersion = 9.3 -} - -tasks.withType(GenerateModuleMetadata).all { - enabled = false -} - -if (project.hasProperty("distributionRepository")) { - publishing { - repositories { - maven { - url = "${distributionRepository}" - name = "deployment" - } - } - } -} - publishing { publications { maven(MavenPublication) { from components.java } - withType(MavenPublication) { mavenPublication -> - pom { - name = project.description - description = project.description - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - organization { - name = 'Pivotal Software, Inc.' - url = 'https://spring.io' - } - licenses { - license { - name = 'The Apache Software License, Version 2.0' - url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' - } - } - scm { - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - connection = 'scm:git:https://github.com/spring-io/gradle-enterprise-conventions' - } - developers { - developer { - id = 'wilkinsona' - name = 'Andy Wilkinson' - email = 'awilkinson@pivotal.io' - roles = ["Project lead"] - } - } - } - } } } diff --git a/gradle-enterprise-conventions-gradle-plugin/build.gradle b/gradle-enterprise-conventions-gradle-plugin/build.gradle index 9c83f2a..bfdcf69 100644 --- a/gradle-enterprise-conventions-gradle-plugin/build.gradle +++ b/gradle-enterprise-conventions-gradle-plugin/build.gradle @@ -1,15 +1,10 @@ plugins { - id "checkstyle" - id "io.spring.javaformat" version "$javaFormatVersion" + id "build-conventions" id "java-gradle-plugin" id "maven-publish" } description = "Gradle Enterprise Conventions Gradle plugin" -group = 'io.spring.ge.conventions' - -sourceCompatibility = 1.8 -targetCompatibility = 1.8 repositories { mavenCentral() @@ -17,8 +12,6 @@ repositories { } dependencies { - checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}") - compileOnly("com.gradle:develocity-gradle-plugin:${gradleEnterprisePluginVersion}") implementation(project(":gradle-enterprise-conventions-core")) @@ -39,67 +32,3 @@ gradlePlugin { } } } - -test { - useJUnitPlatform() -} - -java { - withJavadocJar() - withSourcesJar() -} - -checkstyle { - def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")} - config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml") - toolVersion = 9.3 -} - -tasks.withType(GenerateModuleMetadata).all { - enabled = false -} - -if (project.hasProperty("distributionRepository")) { - publishing { - repositories { - maven { - url = "${distributionRepository}" - name = "deployment" - } - } - } -} - -publishing { - publications { - withType(MavenPublication) { mavenPublication -> - pom { - name = project.description - description = project.description - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - organization { - name = 'Pivotal Software, Inc.' - url = 'https://spring.io' - } - licenses { - license { - name = 'The Apache Software License, Version 2.0' - url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' - } - } - scm { - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - connection = 'scm:git:https://github.com/spring-io/gradle-enterprise-conventions' - } - developers { - developer { - id = 'wilkinsona' - name = 'Andy Wilkinson' - email = 'awilkinson@pivotal.io' - roles = ["Project lead"] - } - } - } - } - } -} diff --git a/gradle-enterprise-conventions-maven-extension/build.gradle b/gradle-enterprise-conventions-maven-extension/build.gradle index 93331ed..68c93fc 100644 --- a/gradle-enterprise-conventions-maven-extension/build.gradle +++ b/gradle-enterprise-conventions-maven-extension/build.gradle @@ -1,23 +1,16 @@ plugins { - id "checkstyle" - id "io.spring.javaformat" version "$javaFormatVersion" + id "build-conventions" id "java" id "maven-publish" } description = "Gradle Enterprise Conventions Maven extension" -group = 'io.spring.ge.conventions' - -sourceCompatibility = 1.8 -targetCompatibility = 1.8 repositories { mavenCentral() } dependencies { - checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}") - compileOnly("org.apache.maven:maven-core:3.6.3") compileOnly("org.codehaus.plexus:plexus-component-annotations:1.7.1") @@ -29,77 +22,10 @@ dependencies { testImplementation("org.mockito:mockito-core:4.11.0") } -tasks.withType(Test) { testTask -> - if (testTask.name.startsWith("compatibilityTest_")) { - testTask.filter { - includeTestsMatching "*IntegrationTests" - } - } -} - -test { - useJUnitPlatform() -} - -java { - withJavadocJar() - withSourcesJar() -} - -checkstyle { - def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")} - config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml") - toolVersion = 9.3 -} - -tasks.withType(GenerateModuleMetadata).all { - enabled = false -} - -if (project.hasProperty("distributionRepository")) { - publishing { - repositories { - maven { - url = "${distributionRepository}" - name = "deployment" - } - } - } -} - publishing { publications { maven(MavenPublication) { from components.java } - withType(MavenPublication) { mavenPublication -> - pom { - name = project.description - description = project.description - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - organization { - name = 'Pivotal Software, Inc.' - url = 'https://spring.io' - } - licenses { - license { - name = 'The Apache Software License, Version 2.0' - url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' - } - } - scm { - url = 'https://github.com/spring-io/gradle-enterprise-conventions' - connection = 'scm:git:https://github.com/spring-io/gradle-enterprise-conventions' - } - developers { - developer { - id = 'wilkinsona' - name = 'Andy Wilkinson' - email = 'awilkinson@pivotal.io' - roles = ["Project lead"] - } - } - } - } } } diff --git a/gradle/plugins/build-conventions/build.gradle b/gradle/plugins/build-conventions/build.gradle new file mode 100644 index 0000000..36ea72f --- /dev/null +++ b/gradle/plugins/build-conventions/build.gradle @@ -0,0 +1,32 @@ +plugins { + id "checkstyle" + id "java-gradle-plugin" + id "io.spring.javaformat" version "0.0.39" +} + +repositories { + mavenCentral() +} + +gradlePlugin { + plugins { + conventionsPlugin { + id = "build-conventions" + implementationClass = "io.spring.ge.conventions.build.BuildConventionsPlugin" + } + } +} + +sourceCompatibility = "1.8" +targetCompatibility = "1.8" + +dependencies { + checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.39") + implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.39") +} + +checkstyle { + def archive = configurations.checkstyle.filter { it.name.startsWith("spring-javaformat-checkstyle")} + config = resources.text.fromArchiveEntry(archive, "io/spring/javaformat/checkstyle/checkstyle.xml") + toolVersion = 9.3 +} diff --git a/gradle/plugins/build-conventions/settings.gradle b/gradle/plugins/build-conventions/settings.gradle new file mode 100644 index 0000000..e69de29 diff --git a/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/BuildConventionsPlugin.java b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/BuildConventionsPlugin.java new file mode 100644 index 0000000..15147d4 --- /dev/null +++ b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/BuildConventionsPlugin.java @@ -0,0 +1,32 @@ +/* + * Copyright 2020-2024 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 io.spring.ge.conventions.build; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; + +public class BuildConventionsPlugin implements Plugin { + + @Override + public void apply(Project target) { + target.getPlugins().withType(JavaPlugin.class, (plugin) -> new JavaConventions().apply(target)); + target.getPlugins().withType(MavenPublishPlugin.class, (plugin) -> new MavenPublishConventions().apply(target)); + } + +} diff --git a/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/JavaConventions.java b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/JavaConventions.java new file mode 100644 index 0000000..258fdbf --- /dev/null +++ b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/JavaConventions.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020-2024 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 io.spring.ge.conventions.build; + +import java.util.Arrays; + +import io.spring.javaformat.gradle.SpringJavaFormatPlugin; +import org.gradle.api.JavaVersion; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaPluginExtension; +import org.gradle.api.plugins.quality.CheckstyleExtension; +import org.gradle.api.plugins.quality.CheckstylePlugin; +import org.gradle.api.tasks.TaskContainer; +import org.gradle.api.tasks.compile.JavaCompile; +import org.gradle.api.tasks.testing.Test; + +public class JavaConventions { + + void apply(Project project) { + configureJavaCompilation(project); + configureSourceAndJavadocJars(project); + configureSpringJavaFormat(project); + configureCheckstyle(project); + configureTestTasks(project); + } + + private void configureJavaCompilation(Project project) { + JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class); + javaPluginExtension.setSourceCompatibility(JavaVersion.VERSION_1_8); + javaPluginExtension.setTargetCompatibility(JavaVersion.VERSION_1_8); + TaskContainer tasks = project.getTasks(); + tasks.withType(JavaCompile.class, + (javaCompile) -> javaCompile.getOptions() + .getCompilerArgs() + .addAll(Arrays.asList("-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes", + "-Xlint:varargs"))); + } + + private void configureSourceAndJavadocJars(Project project) { + JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class); + javaPluginExtension.withJavadocJar(); + javaPluginExtension.withSourcesJar(); + } + + private void configureSpringJavaFormat(Project project) { + project.getPlugins().apply(SpringJavaFormatPlugin.class); + } + + private void configureCheckstyle(Project project) { + String javaFormatVersion = SpringJavaFormatPlugin.class.getPackage().getImplementationVersion(); + project.getPlugins().apply(CheckstylePlugin.class); + project.getDependencies() + .add("checkstyle", "io.spring.javaformat:spring-javaformat-checkstyle:" + javaFormatVersion); + CheckstyleExtension checkstyleExtension = project.getExtensions().getByType(CheckstyleExtension.class); + checkstyleExtension.setToolVersion("9.3"); + checkstyleExtension.setConfig(project.getResources() + .getText() + .fromArchiveEntry( + project.getConfigurations() + .getByName("checkstyle") + .filter((file) -> file.getName().startsWith("spring-javaformat-checkstyle")), + "io/spring/javaformat/checkstyle/checkstyle.xml")); + } + + private void configureTestTasks(Project project) { + project.getTasks().withType(Test.class, (test) -> test.useJUnitPlatform()); + } + +} diff --git a/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/MavenPublishConventions.java b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/MavenPublishConventions.java new file mode 100644 index 0000000..a12ce67 --- /dev/null +++ b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/MavenPublishConventions.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020-2024 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 io.spring.ge.conventions.build; + +import java.util.Arrays; + +import org.gradle.api.Project; +import org.gradle.api.publish.PublicationContainer; +import org.gradle.api.publish.PublishingExtension; +import org.gradle.api.publish.maven.MavenPublication; +import org.gradle.api.publish.tasks.GenerateModuleMetadata; + +public class MavenPublishConventions { + + void apply(Project project) { + project.setGroup("io.spring.ge.conventions"); + configureDeploymentRepository(project); + configurePom(project); + disableModuleMetadataGeneration(project); + } + + private void configureDeploymentRepository(Project project) { + if (project.hasProperty("distributionRepository")) { + PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); + publishing.getRepositories().maven((maven) -> { + maven.setUrl(project.property("distributionRepository")); + maven.setName("deployment"); + }); + } + } + + private void configurePom(Project project) { + PublicationContainer publications = project.getExtensions() + .getByType(PublishingExtension.class) + .getPublications(); + publications.withType(MavenPublication.class).configureEach((mavenPublication) -> { + mavenPublication.pom((pom) -> { + pom.getName().set(project.getDescription()); + pom.getDescription().set(project.getDescription()); + pom.getUrl().set("https://github.com/spring-io/gradle-enterprise-conventions"); + pom.organization((organization) -> { + organization.getName().set("Pivotal Software, Inc."); + organization.getUrl().set("https://spring.io"); + }); + pom.licenses((licenses) -> licenses.license((license) -> { + license.getName().set("The Apache Software License, Version 2.0"); + license.getUrl().set("https://www.apache.org/licenses/LICENSE-2.0.txt"); + })); + pom.scm((scm) -> { + scm.getUrl().set("https://github.com/spring-io/gradle-enterprise-conventions"); + scm.getConnection().set("scm:git:https://github.com/spring-io/gradle-enterprise-conventions"); + }); + pom.developers((developers) -> { + developers.developer((developer) -> { + developer.getId().set("wilkinsona"); + developer.getName().set("Andy Wilkinson"); + developer.getEmail().set("awilkinson@pivotal.io"); + developer.getRoles().set(Arrays.asList("Project Lead")); + }); + }); + }); + }); + } + + private void disableModuleMetadataGeneration(Project project) { + project.getTasks() + .withType(GenerateModuleMetadata.class, + (generateModuleMetadata) -> generateModuleMetadata.setEnabled(false)); + } + +} diff --git a/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/package-info.java b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/package-info.java new file mode 100644 index 0000000..69e14c3 --- /dev/null +++ b/gradle/plugins/build-conventions/src/main/java/io/spring/ge/conventions/build/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2020-2024 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. + */ + +/** + * Conventions for the project's build. + */ +package io.spring.ge.conventions.build; diff --git a/settings.gradle b/settings.gradle index b200346..0cec21c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ pluginManagement { + includeBuild "gradle/plugins/build-conventions" repositories { mavenCentral() gradlePluginPortal()