From 87d54f1103def7f32db30459a35a437522704fc7 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 14 Apr 2021 05:16:48 -0700 Subject: [PATCH] Clean up build scripts using new Gradle features (#2436) * Make use of `TYPESAFE_PROJECT_ACCESSORS` and `VERSION_CATALOGS` Gradle feature previews to clean up build scripts * Bump setup-java action to v2, specify AdoptOpenJDK distribution --- .github/workflows/gradle.yml | 3 +- adventure/build.gradle.kts | 14 ++--- api/build.gradle.kts | 20 +++---- build.gradle.kts | 40 +++++++------- buildSrc/src/main/kotlin/Versions.kt | 24 --------- buildSrc/src/main/kotlin/extensions.kt | 10 ++++ bukkit-legacy/build.gradle.kts | 4 +- bukkit/build.gradle.kts | 8 +-- bungee/build.gradle.kts | 6 +-- common/build.gradle.kts | 4 +- fabric/build.gradle.kts | 2 +- gradle/libs.versions.toml | 58 +++++++++++++++++++++ java-compat/build.gradle.kts | 8 +-- java-compat/java-compat-16/build.gradle.kts | 11 ++-- java-compat/java-compat-8/build.gradle.kts | 4 +- java-compat/java-compat-9/build.gradle.kts | 7 +-- settings.gradle.kts | 3 ++ sponge-legacy/build.gradle.kts | 4 +- sponge/build.gradle.kts | 6 +-- universal/build.gradle.kts | 24 +++++---- velocity/build.gradle.kts | 6 +-- 21 files changed, 154 insertions(+), 112 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/Versions.kt create mode 100644 gradle/libs.versions.toml diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 62f401fb5..648ab1c49 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -13,8 +13,9 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up JDK 11 - uses: actions/setup-java@v1 + uses: actions/setup-java@v2 with: + distribution: 'adopt' java-version: 11 - name: Build with Gradle run: ./gradlew build diff --git a/adventure/build.gradle.kts b/adventure/build.gradle.kts index 25507aca1..cdb8fd142 100644 --- a/adventure/build.gradle.kts +++ b/adventure/build.gradle.kts @@ -1,9 +1,11 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar -import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin + +plugins { + id("com.github.johnrengelman.shadow") +} // Shade and relocate adventure in an extra module, so that common/the rest can directly depend on a // relocated adventure without breaking native platform's adventure usage with project wide relocation -apply() tasks { withType { relocate("net.kyori", "us.myles.viaversion.libs.kyori") @@ -14,16 +16,10 @@ tasks { } dependencies { - api("net.kyori", "adventure-api", Versions.adventure) { + api(libs.bundles.adventure) { exclude("org.checkerframework") - } - api("net.kyori", "adventure-text-serializer-gson", Versions.adventure) { exclude("net.kyori", "adventure-api") exclude("net.kyori", "adventure-bom") exclude("com.google.code.gson", "gson") } - api("net.kyori", "adventure-text-serializer-legacy", Versions.adventure) { - exclude("net.kyori", "adventure-api") - exclude("net.kyori", "adventure-bom") - } } \ No newline at end of file diff --git a/api/build.gradle.kts b/api/build.gradle.kts index aa1dc325c..561832205 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("net.kyori.blossom") version "1.2.0" + id("net.kyori.blossom") } blossom { @@ -8,13 +8,15 @@ blossom { } dependencies { - api(project(":adventure", "shadow")) - api("it.unimi.dsi", "fastutil", Versions.fastUtil) - api("com.viaversion", "opennbt", Versions.openNBT) - api("com.google.code.gson", "gson", Versions.gson) + api(projects.adventure) { + targetConfiguration = "shadow" + } + api(libs.fastutil) + api(libs.openNBT) + api(libs.gson) - compileOnlyApi("org.yaml", "snakeyaml", Versions.snakeYaml) - compileOnlyApi("io.netty", "netty-all", Versions.netty) - compileOnlyApi("com.google.guava", "guava", Versions.guava) - compileOnlyApi("org.checkerframework", "checker-qual", Versions.checkerQual) + compileOnlyApi(libs.snakeYaml) + compileOnlyApi(libs.netty) + compileOnlyApi(libs.guava) + compileOnlyApi(libs.checkerQual) } diff --git a/build.gradle.kts b/build.gradle.kts index 9d15bdfe9..8571668f2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin plugins { `java-library` `maven-publish` + id("net.kyori.blossom") version "1.2.0" apply false } allprojects { @@ -31,21 +32,6 @@ subprojects { } } - val platforms = listOf( - "bukkit", - "bungee", - "fabric", - "sponge", - "velocity" - ).map { "viaversion-$it" } - if (platforms.contains(project.name)) { - configureShadowJar() - } else if (project.name == "viaversion-api") { - configureShadowJarAPI() - } else if (project.name == "viaversion") { - apply() - } - repositories { maven("https://repo.viaversion.com") maven("https://papermc.io/repo/repository/maven-public/") @@ -53,20 +39,18 @@ subprojects { maven("https://nexus.velocitypowered.com/repository/velocity-artifacts-snapshots/") maven("https://repo.spongepowered.org/repository/maven-public/") maven("https://libraries.minecraft.net") - maven("https://repo.maven.apache.org/maven2/") + mavenCentral() } dependencies { // Note: If manually starting tests doesn't work for you in IJ, change 'Gradle -> Run Tests Using' to 'IntelliJ IDEA' - testImplementation("io.netty", "netty-all", Versions.netty) - testImplementation("com.google.guava", "guava", Versions.guava) - testImplementation("org.junit.jupiter", "junit-jupiter-api", Versions.jUnit) - testImplementation("org.junit.jupiter", "junit-jupiter-engine", Versions.jUnit) + testImplementation(rootProject.libs.netty) + testImplementation(rootProject.libs.guava) + testImplementation(rootProject.libs.bundles.junit) } + configureJavaTarget(8) java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 withSourcesJar() withJavadocJar() } @@ -96,6 +80,18 @@ subprojects { } } +sequenceOf( + projects.viaversionBukkit, + projects.viaversionBungee, + projects.viaversionFabric, + projects.viaversionSponge, + projects.viaversionVelocity +).map { it.dependencyProject }.forEach { project -> + project.configureShadowJar() +} + +projects.viaversionApi.dependencyProject.configureShadowJarAPI() + tasks { // root project has no useful artifacts withType { diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt deleted file mode 100644 index 2b46ae456..000000000 --- a/buildSrc/src/main/kotlin/Versions.kt +++ /dev/null @@ -1,24 +0,0 @@ -object Versions { - // Common compiled - const val adventure = "4.7.0" - const val gson = "2.8.6" - const val fastUtil = "8.3.1" - const val openNBT = "2.0-SNAPSHOT" - const val javassist = "3.27.0-GA" - - // Common provided - const val netty = "4.0.20.Final" - const val guava = "17.0" - const val snakeYaml = "1.18" - - const val jUnit = "5.6.3" - const val checkerQual = "3.12.0" - - // Platforms - const val paper = "1.16.5-R0.1-SNAPSHOT" - const val legacyBukkit = "1.8.8-R0.1-SNAPSHOT" - const val bungee = "1.16-R0.5-SNAPSHOT" - const val sponge = "5.0.0" - const val legacySponge = "4.0.0" - const val velocity = "1.1.0-SNAPSHOT" -} diff --git a/buildSrc/src/main/kotlin/extensions.kt b/buildSrc/src/main/kotlin/extensions.kt index 719a7b65e..66807bc62 100644 --- a/buildSrc/src/main/kotlin/extensions.kt +++ b/buildSrc/src/main/kotlin/extensions.kt @@ -1,8 +1,11 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import org.gradle.api.JavaVersion import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginConvention import org.gradle.jvm.tasks.Jar import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.invoke import org.gradle.kotlin.dsl.withType import java.io.ByteArrayOutputStream @@ -88,3 +91,10 @@ fun Project.latestCommitHash(): String { } return byteOut.toString(Charsets.UTF_8.name()).trim() } + +fun Project.configureJavaTarget(version: Int) { + configure { + sourceCompatibility = JavaVersion.toVersion(version) + targetCompatibility = JavaVersion.toVersion(version) + } +} diff --git a/bukkit-legacy/build.gradle.kts b/bukkit-legacy/build.gradle.kts index 02ec02b02..c69bdfe8f 100644 --- a/bukkit-legacy/build.gradle.kts +++ b/bukkit-legacy/build.gradle.kts @@ -1,6 +1,6 @@ dependencies { - implementation(project(":viaversion-common")) - compileOnly("org.bukkit", "bukkit", Versions.legacyBukkit) { + implementation(projects.viaversionCommon) + compileOnly(libs.legacyBukkit) { exclude("junit", "junit") exclude("com.google.code.gson", "gson") exclude("javax.persistence", "persistence-api") diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index b23b4cad2..3affbc8e2 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -1,8 +1,8 @@ dependencies { - implementation(project(":viaversion-bukkit-legacy")) - implementation(project(":viaversion-common")) - implementation("org.javassist", "javassist", Versions.javassist) - compileOnly("com.destroystokyo.paper", "paper-api", Versions.paper) { + implementation(projects.viaversionBukkitLegacy) + implementation(projects.viaversionCommon) + implementation(libs.javassist) + compileOnly(libs.paper) { exclude("junit", "junit") exclude("com.google.code.gson", "gson") exclude("javax.persistence", "persistence-api") diff --git a/bungee/build.gradle.kts b/bungee/build.gradle.kts index 150642d56..12469ce8c 100644 --- a/bungee/build.gradle.kts +++ b/bungee/build.gradle.kts @@ -1,7 +1,7 @@ dependencies { - implementation(project(":viaversion-common")) - implementation(project(":java-compat")) - compileOnly("net.md-5", "bungeecord-api", Versions.bungee) + implementation(projects.viaversionCommon) + implementation(projects.javaCompat) + compileOnly(libs.bungee) } configure { diff --git a/common/build.gradle.kts b/common/build.gradle.kts index ef334d609..8d6ef9165 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("net.kyori.blossom") version "1.2.0" + id("net.kyori.blossom") } blossom { @@ -8,5 +8,5 @@ blossom { } dependencies { - api(project(":viaversion-api")) + api(projects.viaversionApi) } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 416fc0244..0dd230f83 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -1,3 +1,3 @@ dependencies { - implementation(project(":viaversion-common")) + implementation(projects.viaversionCommon) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 000000000..d47f401f9 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,58 @@ +metadata.format.version = "1.0" + +[versions] + +adventure = "4.7.0" +gson = "2.8.6" +fastutil = "8.3.1" +openNBT = "2.0-SNAPSHOT" +javassist = "3.27.0-GA" + +# Common provided +netty = "4.0.20.Final" +guava = "17.0" +snakeYaml = "1.18" + +junit = "5.6.3" +checkerQual = "3.12.0" + +# Platforms +paper = "1.16.5-R0.1-SNAPSHOT" +legacyBukkit = "1.8.8-R0.1-SNAPSHOT" +bungee = "1.16-R0.5-SNAPSHOT" +sponge = "5.0.0" +legacySponge = "4.0.0" +velocity = "1.1.0-SNAPSHOT" + + +[libraries] + +adventure-api = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" } +adventure-text-serializer-gson = { group = "net.kyori", name = "adventure-text-serializer-gson", version.ref = "adventure" } +adventure-text-serializer-legacy = { group = "net.kyori", name = "adventure-text-serializer-legacy", version.ref = "adventure" } + +gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" } +fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" } +openNBT = { group = "com.viaversion", name = "opennbt", version.ref = "openNBT" } +javassist = { group = "org.javassist", name = "javassist", version.ref = "javassist" } + +netty = { group = "io.netty", name = "netty-all", version.ref = "netty" } +guava = { group = "com.google.guava", name = "guava", version.ref = "guava" } +snakeYaml = { group = "org.yaml", name = "snakeyaml", version.ref = "snakeYaml" } + +jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "junit" } +jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junit" } +checkerQual = { group = "org.checkerframework", name = "checker-qual", version.ref = "checkerQual" } + +paper = { group = "com.destroystokyo.paper", name = "paper-api", version.ref = "paper" } +legacyBukkit = { group = "org.bukkit", name = "bukkit", version.ref = "legacyBukkit" } +bungee = { group = "net.md-5", name = "bungeecord-api", version.ref = "bungee" } +sponge = { group = "org.spongepowered", name = "spongeapi", version.ref = "sponge" } +legacySponge = { group = "org.spongepowered", name = "spongeapi", version.ref = "legacySponge" } +velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity" } + + +[bundles] + +adventure = ["adventure-api", "adventure-text-serializer-gson", "adventure-text-serializer-legacy"] +junit = ["jupiter-api", "jupiter-engine"] diff --git a/java-compat/build.gradle.kts b/java-compat/build.gradle.kts index ed4e522b5..da0e6981f 100644 --- a/java-compat/build.gradle.kts +++ b/java-compat/build.gradle.kts @@ -1,8 +1,8 @@ dependencies { - api(project(":java-compat:java-compat-common")) - api(project(":java-compat:java-compat-8")) - api(project(":java-compat:java-compat-9")) - api(project(":java-compat:java-compat-16")) + api(projects.javaCompat.javaCompatCommon) + api(projects.javaCompat.javaCompat8) + api(projects.javaCompat.javaCompat9) + api(projects.javaCompat.javaCompat16) } configure { diff --git a/java-compat/java-compat-16/build.gradle.kts b/java-compat/java-compat-16/build.gradle.kts index cbea07999..eb1d78ded 100644 --- a/java-compat/java-compat-16/build.gradle.kts +++ b/java-compat/java-compat-16/build.gradle.kts @@ -1,10 +1,7 @@ dependencies { - api(project(":java-compat:java-compat-common")) + api(projects.javaCompat.javaCompatCommon) } -configure { - // This is for Java 16, but the minimum required for this - // is actually just Java 9! - sourceCompatibility = JavaVersion.VERSION_1_9 - targetCompatibility = JavaVersion.VERSION_1_9 -} +// This is for Java 16, but the minimum required for this +// is actually just Java 9! +configureJavaTarget(9) diff --git a/java-compat/java-compat-8/build.gradle.kts b/java-compat/java-compat-8/build.gradle.kts index 6b8e88798..5480dcb03 100644 --- a/java-compat/java-compat-8/build.gradle.kts +++ b/java-compat/java-compat-8/build.gradle.kts @@ -1,3 +1,5 @@ dependencies { - api(project(":java-compat:java-compat-common")) + api(projects.javaCompat.javaCompatCommon) } + +configureJavaTarget(8) diff --git a/java-compat/java-compat-9/build.gradle.kts b/java-compat/java-compat-9/build.gradle.kts index 01c8a8b7a..9966cb2dc 100644 --- a/java-compat/java-compat-9/build.gradle.kts +++ b/java-compat/java-compat-9/build.gradle.kts @@ -1,8 +1,5 @@ dependencies { - api(project(":java-compat:java-compat-common")) + api(projects.javaCompat.javaCompatCommon) } -configure { - sourceCompatibility = JavaVersion.VERSION_1_9 - targetCompatibility = JavaVersion.VERSION_1_9 -} +configureJavaTarget(9) diff --git a/settings.gradle.kts b/settings.gradle.kts index f95032f6c..a29fef5a6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,5 +1,8 @@ rootProject.name = "viaversion-parent" +enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") +enableFeaturePreview("VERSION_CATALOGS") + include("adventure") include("java-compat", "java-compat:java-compat-common", "java-compat:java-compat-8", "java-compat:java-compat-9", "java-compat:java-compat-16") diff --git a/sponge-legacy/build.gradle.kts b/sponge-legacy/build.gradle.kts index 14f58dd81..54e6acef4 100644 --- a/sponge-legacy/build.gradle.kts +++ b/sponge-legacy/build.gradle.kts @@ -1,4 +1,4 @@ dependencies { - implementation(project(":viaversion-common")) - compileOnly("org.spongepowered", "spongeapi", Versions.legacySponge) + implementation(projects.viaversionCommon) + compileOnly(libs.legacySponge) } diff --git a/sponge/build.gradle.kts b/sponge/build.gradle.kts index 9af9dbfd2..23fa2aad6 100644 --- a/sponge/build.gradle.kts +++ b/sponge/build.gradle.kts @@ -1,5 +1,5 @@ dependencies { - implementation(project(":viaversion-sponge-legacy")) - implementation(project(":viaversion-common")) - compileOnly("org.spongepowered","spongeapi", Versions.sponge) + implementation(projects.viaversionSpongeLegacy) + implementation(projects.viaversionCommon) + compileOnly(libs.sponge) } diff --git a/universal/build.gradle.kts b/universal/build.gradle.kts index 58e15484e..911bb8f25 100644 --- a/universal/build.gradle.kts +++ b/universal/build.gradle.kts @@ -1,22 +1,26 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +plugins { + id("com.github.johnrengelman.shadow") +} + tasks { withType { archiveClassifier.set("") archiveFileName.set("ViaVersion-${project.version}.jar") destinationDirectory.set(rootProject.projectDir.resolve("build/libs")) duplicatesStrategy = DuplicatesStrategy.EXCLUDE - arrayOf( - "bukkit", - "bungee", - "fabric", - "sponge", - "velocity" - ).forEach { - val subProject = rootProject.project(":viaversion-$it") - val shadowJarTask = subProject.tasks.getByName("shadowJar") - from(zipTree(shadowJarTask.outputs.files.singleFile)) + sequenceOf( + rootProject.projects.viaversionBukkit, + rootProject.projects.viaversionBungee, + rootProject.projects.viaversionFabric, + rootProject.projects.viaversionSponge, + rootProject.projects.viaversionVelocity, + ).map { it.dependencyProject }.forEach { subproject -> + val shadowJarTask = subproject.tasks.getByName("shadowJar", ShadowJar::class) dependsOn(shadowJarTask) + dependsOn(subproject.tasks.withType()) + from(zipTree(shadowJarTask.archiveFile)) } } build { diff --git a/velocity/build.gradle.kts b/velocity/build.gradle.kts index a36d9c65f..7c6781be4 100644 --- a/velocity/build.gradle.kts +++ b/velocity/build.gradle.kts @@ -1,5 +1,5 @@ dependencies { - implementation(project(":viaversion-common")) - compileOnly("com.velocitypowered", "velocity-api", Versions.velocity) - annotationProcessor("com.velocitypowered", "velocity-api", Versions.velocity) + implementation(projects.viaversionCommon) + compileOnly(libs.velocity) + annotationProcessor(libs.velocity) }