From 3d20c3dd2da7c6a648da9cb4c704e3bd92bd0aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20St=C3=A4ding?= Date: Mon, 27 Mar 2023 00:00:24 +0200 Subject: [PATCH] Migrate buildSrc plugins to build-logic --- api/build.gradle.kts | 1 + build-logic/build.gradle.kts | 19 +++++ {buildSrc => build-logic}/settings.gradle.kts | 4 ++ build-logic/src/main/kotlin/LibsAccessor.kt | 6 ++ .../kotlin/velocity-checkstyle.gradle.kts | 10 +++ .../kotlin/velocity-init-manifest.gradle.kts | 29 ++++++++ .../main/kotlin/velocity-publish.gradle.kts | 33 +++++++++ .../main/kotlin/velocity-spotless.gradle.kts | 15 ++++ build.gradle.kts | 17 ++--- buildSrc/build.gradle.kts | 37 ---------- .../script/SetManifestImplVersionPlugin.kt | 54 -------------- .../script/VelocityCheckstylePlugin.kt | 38 ---------- .../script/VelocityPublishPlugin.kt | 72 ------------------- .../script/VelocitySpotlessPlugin.kt | 46 ------------ gradle/libs.versions.toml | 1 + native/build.gradle.kts | 2 +- proxy/build.gradle.kts | 2 +- settings.gradle.kts | 22 +++++- 18 files changed, 145 insertions(+), 263 deletions(-) create mode 100644 build-logic/build.gradle.kts rename {buildSrc => build-logic}/settings.gradle.kts (79%) create mode 100644 build-logic/src/main/kotlin/LibsAccessor.kt create mode 100644 build-logic/src/main/kotlin/velocity-checkstyle.gradle.kts create mode 100644 build-logic/src/main/kotlin/velocity-init-manifest.gradle.kts create mode 100644 build-logic/src/main/kotlin/velocity-publish.gradle.kts create mode 100644 build-logic/src/main/kotlin/velocity-spotless.gradle.kts delete mode 100644 buildSrc/build.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/com/velocitypowered/script/SetManifestImplVersionPlugin.kt delete mode 100644 buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityCheckstylePlugin.kt delete mode 100644 buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityPublishPlugin.kt delete mode 100644 buildSrc/src/main/kotlin/com/velocitypowered/script/VelocitySpotlessPlugin.kt diff --git a/api/build.gradle.kts b/api/build.gradle.kts index c5a96101e..2a1de7af8 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -1,6 +1,7 @@ plugins { `java-library` `maven-publish` + id("velocity-publish") } java { diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts new file mode 100644 index 000000000..7bbe0fb3f --- /dev/null +++ b/build-logic/build.gradle.kts @@ -0,0 +1,19 @@ +@Suppress("DSL_SCOPE_VIOLATION") // fixed in Gradle 8.1 +plugins { + `kotlin-dsl` + alias(libs.plugins.spotless) +} + +dependencies { + // this is OK as long as the same version catalog is used in the main build and build-logic + // see https://github.com/gradle/gradle/issues/15383#issuecomment-779893192 + implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) + + implementation("com.diffplug.spotless:spotless-plugin-gradle:${libs.plugins.spotless.get().version}") +} + +spotless { + kotlin { + licenseHeaderFile(rootProject.file("../HEADER.txt")) + } +} diff --git a/buildSrc/settings.gradle.kts b/build-logic/settings.gradle.kts similarity index 79% rename from buildSrc/settings.gradle.kts rename to build-logic/settings.gradle.kts index 0ecd59401..853b9f8c4 100644 --- a/buildSrc/settings.gradle.kts +++ b/build-logic/settings.gradle.kts @@ -1,3 +1,5 @@ +@file:Suppress("UnstableApiUsage") + dependencyResolutionManagement { repositories { mavenCentral() @@ -9,3 +11,5 @@ dependencyResolutionManagement { } } } + +rootProject.name = "build-logic" diff --git a/build-logic/src/main/kotlin/LibsAccessor.kt b/build-logic/src/main/kotlin/LibsAccessor.kt new file mode 100644 index 000000000..1214d4912 --- /dev/null +++ b/build-logic/src/main/kotlin/LibsAccessor.kt @@ -0,0 +1,6 @@ +import org.gradle.accessors.dm.LibrariesForLibs +import org.gradle.api.Project +import org.gradle.kotlin.dsl.getByType + +val Project.libs: LibrariesForLibs + get() = rootProject.extensions.getByType() diff --git a/build-logic/src/main/kotlin/velocity-checkstyle.gradle.kts b/build-logic/src/main/kotlin/velocity-checkstyle.gradle.kts new file mode 100644 index 000000000..4ac506002 --- /dev/null +++ b/build-logic/src/main/kotlin/velocity-checkstyle.gradle.kts @@ -0,0 +1,10 @@ +plugins { + checkstyle +} + +extensions.configure { + configFile = rootProject.file("config/checkstyle/checkstyle.xml") + maxErrors = 0 + maxWarnings = 0 + toolVersion = libs.checkstyle.get().version.toString() +} diff --git a/build-logic/src/main/kotlin/velocity-init-manifest.gradle.kts b/build-logic/src/main/kotlin/velocity-init-manifest.gradle.kts new file mode 100644 index 000000000..1901430f5 --- /dev/null +++ b/build-logic/src/main/kotlin/velocity-init-manifest.gradle.kts @@ -0,0 +1,29 @@ +import org.gradle.jvm.tasks.Jar +import org.gradle.kotlin.dsl.withType +import java.io.ByteArrayOutputStream + +val currentShortRevision = ByteArrayOutputStream().use { + exec { + executable = "git" + args = listOf("rev-parse", "HEAD") + standardOutput = it + } + it.toString().trim().substring(0, 8) +} + +tasks.withType { + manifest { + val buildNumber = System.getenv("BUILD_NUMBER") + val velocityHumanVersion: String = + if (project.version.toString().endsWith("-SNAPSHOT")) { + if (buildNumber == null) { + "${project.version} (git-$currentShortRevision)" + } else { + "${project.version} (git-$currentShortRevision-b$buildNumber)" + } + } else { + archiveVersion.get() + } + attributes["Implementation-Version"] = velocityHumanVersion + } +} diff --git a/build-logic/src/main/kotlin/velocity-publish.gradle.kts b/build-logic/src/main/kotlin/velocity-publish.gradle.kts new file mode 100644 index 000000000..49419a700 --- /dev/null +++ b/build-logic/src/main/kotlin/velocity-publish.gradle.kts @@ -0,0 +1,33 @@ +plugins { + java + `maven-publish` +} + +extensions.configure { + repositories { + maven { + credentials(PasswordCredentials::class.java) + + name = "paper" + val base = "https://papermc.io/repo/repository/maven" + val releasesRepoUrl = "$base-releases/" + val snapshotsRepoUrl = "$base-snapshots/" + setUrl(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl) + } + } + publications { + create("maven") { + from(components["java"]) + pom { + name.set("Velocity") + description.set("The modern, next-generation Minecraft server proxy") + url.set("https://www.velocitypowered.com") + scm { + url.set("https://github.com/PaperMC/Velocity") + connection.set("scm:git:https://github.com/PaperMC/Velocity.git") + developerConnection.set("scm:git:https://github.com/PaperMC/Velocity.git") + } + } + } + } +} diff --git a/build-logic/src/main/kotlin/velocity-spotless.gradle.kts b/build-logic/src/main/kotlin/velocity-spotless.gradle.kts new file mode 100644 index 000000000..cbd3f3d46 --- /dev/null +++ b/build-logic/src/main/kotlin/velocity-spotless.gradle.kts @@ -0,0 +1,15 @@ +import com.diffplug.gradle.spotless.SpotlessExtension +import com.diffplug.gradle.spotless.SpotlessPlugin + +apply() + +extensions.configure { + java { + if (project.name == "velocity-api") { + licenseHeaderFile(file("HEADER.txt")) + } else { + licenseHeaderFile(rootProject.file("HEADER.txt")) + } + removeUnusedImports() + } +} diff --git a/build.gradle.kts b/build.gradle.kts index f1a64c850..3ec6b160c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,17 +1,14 @@ -import com.velocitypowered.script.VelocityCheckstylePlugin -import com.velocitypowered.script.VelocityPublishPlugin -import com.velocitypowered.script.VelocitySpotlessPlugin - plugins { `java-library` + id("velocity-checkstyle") apply false + id("velocity-spotless") apply false } subprojects { apply() - apply() - apply() - apply() + apply(plugin = "velocity-checkstyle") + apply(plugin = "velocity-spotless") java { toolchain { @@ -19,12 +16,6 @@ subprojects { } } - repositories { - mavenCentral() - maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // adventure - maven("https://repo.papermc.io/repository/maven-public/") - } - dependencies { testImplementation(rootProject.libs.junit) } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts deleted file mode 100644 index e65c26037..000000000 --- a/buildSrc/build.gradle.kts +++ /dev/null @@ -1,37 +0,0 @@ -plugins { - `kotlin-dsl` - checkstyle - alias(libs.plugins.indra.publishing) - alias(libs.plugins.spotless) -} - -dependencies { - implementation("com.diffplug.spotless:spotless-plugin-gradle:${libs.plugins.spotless.get().version}") -} - -gradlePlugin { - plugins { - register("set-manifest-impl-version") { - id = "set-manifest-impl-version" - implementationClass = "com.velocitypowered.script.SetManifestImplVersionPlugin" - } - register("velocity-checkstyle") { - id = "velocity-checkstyle" - implementationClass = "com.velocitypowered.script.VelocityCheckstylePlugin" - } - register("velocity-spotless") { - id = "velocity-spotless" - implementationClass = "com.velocitypowered.script.VelocitySpotlessPlugin" - } - register("velocity-publish") { - id = "velocity-publish" - implementationClass = "com.velocitypowered.script.VelocityPublishPlugin" - } - } -} - -spotless { - kotlin { - licenseHeaderFile(project.rootProject.file("../HEADER.txt")) - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/com/velocitypowered/script/SetManifestImplVersionPlugin.kt b/buildSrc/src/main/kotlin/com/velocitypowered/script/SetManifestImplVersionPlugin.kt deleted file mode 100644 index 5be27e608..000000000 --- a/buildSrc/src/main/kotlin/com/velocitypowered/script/SetManifestImplVersionPlugin.kt +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2023 Velocity Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.velocitypowered.script - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.jvm.tasks.Jar -import org.gradle.kotlin.dsl.withType -import java.io.ByteArrayOutputStream - -class SetManifestImplVersionPlugin : Plugin { - override fun apply(target: Project) = target.afterEvaluate { configure() } - private fun Project.configure() { - val currentShortRevision = ByteArrayOutputStream().use { - exec { - executable = "git" - args = listOf("rev-parse", "HEAD") - standardOutput = it - } - it.toString().trim().substring(0, 8) - } - tasks.withType { - manifest { - val buildNumber = System.getenv("BUILD_NUMBER") - var velocityHumanVersion: String - if (project.version.toString().endsWith("-SNAPSHOT")) { - if (buildNumber != null) { - velocityHumanVersion = "${project.version} (git-$currentShortRevision-b$buildNumber)" - } else { - velocityHumanVersion = "${project.version} (git-$currentShortRevision)" - } - } else { - velocityHumanVersion = archiveVersion.get() - } - attributes["Implementation-Version"] = velocityHumanVersion - } - } - } -} diff --git a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityCheckstylePlugin.kt b/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityCheckstylePlugin.kt deleted file mode 100644 index 73d5e7bcd..000000000 --- a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityCheckstylePlugin.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2023 Velocity Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.velocitypowered.script - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.plugins.quality.CheckstyleExtension -import org.gradle.api.plugins.quality.CheckstylePlugin -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.configure - -class VelocityCheckstylePlugin : Plugin { - override fun apply(target: Project) = target.configure() - private fun Project.configure() { - apply() - extensions.configure { - configFile = project.rootProject.file("config/checkstyle/checkstyle.xml") - maxErrors = 0 - maxWarnings = 0 - toolVersion = "10.6.0" - } - } -} diff --git a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityPublishPlugin.kt b/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityPublishPlugin.kt deleted file mode 100644 index 8aa18b048..000000000 --- a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocityPublishPlugin.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2023 Velocity Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.velocitypowered.script - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.artifacts.repositories.PasswordCredentials -import org.gradle.api.plugins.JavaBasePlugin -import org.gradle.api.plugins.JavaPluginExtension -import org.gradle.api.publish.PublishingExtension -import org.gradle.api.publish.maven.MavenPublication -import org.gradle.api.publish.maven.plugins.MavenPublishPlugin -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.get -import org.gradle.kotlin.dsl.getByType - -class VelocityPublishPlugin : Plugin { - override fun apply(target: Project) = target.afterEvaluate { - if (target.name != "velocity-proxy") { - configure() - } - } - private fun Project.configure() { - apply() - apply() - extensions.configure { - repositories { - maven { - credentials(PasswordCredentials::class.java) - - name = "paper" - val base = "https://papermc.io/repo/repository/maven" - val releasesRepoUrl = "$base-releases/" - val snapshotsRepoUrl = "$base-snapshots/" - setUrl(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl) - } - } - publications { - create("maven") { - from(components["java"]) - pom { - name.set("Velocity") - description.set("The modern, next-generation Minecraft server proxy") - url.set("https://www.velocitypowered.com") - scm { - url.set("https://github.com/PaperMC/Velocity") - connection.set("scm:git:https://github.com/PaperMC/Velocity.git") - developerConnection.set("scm:git:https://github.com/PaperMC/Velocity.git") - } - } - } - } - } - } -} diff --git a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocitySpotlessPlugin.kt b/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocitySpotlessPlugin.kt deleted file mode 100644 index 1641f2c8a..000000000 --- a/buildSrc/src/main/kotlin/com/velocitypowered/script/VelocitySpotlessPlugin.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2023 Velocity Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.velocitypowered.script - -import com.diffplug.gradle.spotless.SpotlessExtension -import com.diffplug.gradle.spotless.SpotlessPlugin -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.configure -import java.io.File - -class VelocitySpotlessPlugin : Plugin { - override fun apply(target: Project) = target.configure() - - private fun Project.configure() { - apply() - - extensions.configure { - java { - if (project.name == "velocity-api") { - licenseHeaderFile(project.file("HEADER.txt")) - } else { - licenseHeaderFile(project.rootProject.file("HEADER.txt")) - } - - removeUnusedImports() - } - } - } -} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1497d4667..8b1f9a817 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,6 +18,7 @@ brigadier = "com.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT" bstats = "org.bstats:bstats-base:3.0.1" caffeine = "com.github.ben-manes.caffeine:caffeine:3.1.5" checker-qual = "org.checkerframework:checker-qual:3.28.0" +checkstyle = "com.puppycrawl.tools:checkstyle:10.9.3" completablefutures = "com.spotify:completable-futures:0.3.5" configurate-hocon = { module = "org.spongepowered:configurate-hocon", version.ref = "configurate" } configurate-yaml = { module = "org.spongepowered:configurate-yaml", version.ref = "configurate" } diff --git a/native/build.gradle.kts b/native/build.gradle.kts index 165cb30f9..5ec8673cc 100644 --- a/native/build.gradle.kts +++ b/native/build.gradle.kts @@ -1,6 +1,6 @@ plugins { `java-library` - `maven-publish` + id("velocity-publish") } dependencies { diff --git a/proxy/build.gradle.kts b/proxy/build.gradle.kts index 643d5eb78..322888a27 100644 --- a/proxy/build.gradle.kts +++ b/proxy/build.gradle.kts @@ -2,7 +2,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCach plugins { application - `set-manifest-impl-version` + id("velocity-init-manifest") alias(libs.plugins.shadow) } diff --git a/settings.gradle.kts b/settings.gradle.kts index b8d700802..d928550b9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,23 @@ +@file:Suppress("UnstableApiUsage") + +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + mavenCentral() + maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // adventure + maven("https://repo.papermc.io/repository/maven-public/") + } +} + + +pluginManagement { + includeBuild("build-logic") + repositories { + mavenCentral() + gradlePluginPortal() + } +} + plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0" } @@ -6,8 +26,8 @@ rootProject.name = "velocity" sequenceOf( "api", - "proxy", "native", + "proxy", ).forEach { val project = ":velocity-$it" include(project)