From e2d9e44be3d76b33d9d217ae1b78547a00eaa921 Mon Sep 17 00:00:00 2001 From: jmp Date: Mon, 24 May 2021 21:58:34 -0700 Subject: [PATCH] Build script improvements --- .github/workflows/gradle.yml | 3 + adventure/build.gradle.kts | 12 +- api/build.gradle.kts | 1 + {buildSrc => build-logic}/build.gradle.kts | 3 +- build-logic/src/main/kotlin/extensions.kt | 44 +++++++ .../kotlin/via.base-conventions.gradle.kts | 42 +++++++ .../main/kotlin/via.build-logic.gradle.kts | 0 .../via.platform-conventions.gradle.kts | 10 ++ .../kotlin/via.shadow-conventions.gradle.kts | 55 +++++++++ .../via.standard-conventions.gradle.kts | 5 + build.gradle.kts | 112 ++++-------------- buildSrc/src/main/kotlin/extensions.kt | 100 ---------------- gradle.properties | 6 +- .../java-compat-unsafe/build.gradle.kts | 2 +- settings.gradle.kts | 29 ++++- universal/build.gradle.kts | 12 +- 16 files changed, 234 insertions(+), 202 deletions(-) rename {buildSrc => build-logic}/build.gradle.kts (65%) create mode 100644 build-logic/src/main/kotlin/extensions.kt create mode 100644 build-logic/src/main/kotlin/via.base-conventions.gradle.kts create mode 100644 build-logic/src/main/kotlin/via.build-logic.gradle.kts create mode 100644 build-logic/src/main/kotlin/via.platform-conventions.gradle.kts create mode 100644 build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts create mode 100644 build-logic/src/main/kotlin/via.standard-conventions.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/extensions.kt diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 648ab1c49..28d3082a2 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -7,11 +7,14 @@ on: [push, pull_request] jobs: build: + # Only run on PRs if the source branch is on a different repo. We do not need run everything twice. + if: ${{ github.event_name != 'pull_request' || github.repository != github.event.pull_request.head.repo.full_name }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: gradle/wrapper-validation-action@v1 - name: Set up JDK 11 uses: actions/setup-java@v2 with: diff --git a/adventure/build.gradle.kts b/adventure/build.gradle.kts index 3e2a74c1c..c2847bb39 100644 --- a/adventure/build.gradle.kts +++ b/adventure/build.gradle.kts @@ -1,5 +1,3 @@ -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar - plugins { id("com.github.johnrengelman.shadow") } @@ -7,11 +5,11 @@ plugins { // 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 tasks { - withType { + shadowJar { relocate("net.kyori", "com.viaversion.viaversion.libs.kyori") } - getByName("build") { - dependsOn(withType()) + build { + dependsOn(shadowJar) } } @@ -22,4 +20,6 @@ dependencies { exclude("net.kyori", "adventure-bom") exclude("com.google.code.gson", "gson") } -} \ No newline at end of file +} + +publishShadowJar() diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 561832205..e6537c1fd 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("net.kyori.blossom") + id("via.shadow-conventions") } blossom { diff --git a/buildSrc/build.gradle.kts b/build-logic/build.gradle.kts similarity index 65% rename from buildSrc/build.gradle.kts rename to build-logic/build.gradle.kts index c9e19892a..c0d998653 100644 --- a/buildSrc/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -7,5 +7,6 @@ repositories { } dependencies { + // version must be manually kept in sync with the one in root project settings.gradle.kts implementation("gradle.plugin.com.github.jengelman.gradle.plugins", "shadow", "7.0.0") -} \ No newline at end of file +} diff --git a/build-logic/src/main/kotlin/extensions.kt b/build-logic/src/main/kotlin/extensions.kt new file mode 100644 index 000000000..c55b057bb --- /dev/null +++ b/build-logic/src/main/kotlin/extensions.kt @@ -0,0 +1,44 @@ +import org.gradle.api.JavaVersion +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.named +import java.io.ByteArrayOutputStream + +fun Project.publishShadowJar() { + configurePublication { + artifact(tasks["shadowJar"]) + artifact(tasks["sourcesJar"]) + } +} + +fun Project.publishJavaComponents() { + configurePublication { + from(components["java"]) + } +} + +private fun Project.configurePublication(configurer: MavenPublication.() -> Unit) { + extensions.configure { + publications.named("mavenJava") { + apply(configurer) + } + } +} + +fun Project.latestCommitHash(): String { + val byteOut = ByteArrayOutputStream() + exec { + commandLine = listOf("git", "rev-parse", "--short", "HEAD") + standardOutput = byteOut + } + return byteOut.toString(Charsets.UTF_8.name()).trim() +} + +fun JavaPluginExtension.javaTarget(version: Int) { + sourceCompatibility = JavaVersion.toVersion(version) + targetCompatibility = JavaVersion.toVersion(version) +} diff --git a/build-logic/src/main/kotlin/via.base-conventions.gradle.kts b/build-logic/src/main/kotlin/via.base-conventions.gradle.kts new file mode 100644 index 000000000..1ccf460fd --- /dev/null +++ b/build-logic/src/main/kotlin/via.base-conventions.gradle.kts @@ -0,0 +1,42 @@ +plugins { + `java-library` + `maven-publish` +} + +tasks { + // Variable replacements + processResources { + filesMatching(listOf("plugin.yml", "mcmod.info", "fabric.mod.json", "bungee.yml")) { + expand("version" to project.version, "description" to project.description) + } + } + javadoc { + options.encoding = Charsets.UTF_8.name() + } + compileJava { + options.encoding = Charsets.UTF_8.name() + options.compilerArgs.addAll(listOf("-nowarn", "-Xlint:-unchecked", "-Xlint:-deprecation")) + } +} + +java { + javaTarget(8) + withSourcesJar() + withJavadocJar() // todo: do we need javadoc enabled for every module? +} + +publishing { + publications.create("mavenJava") { + groupId = rootProject.group as String + artifactId = project.name + version = rootProject.version as String + } + repositories.maven { + name = "Via" + url = uri("https://repo.viaversion.com/") + credentials(PasswordCredentials::class) + authentication { + create("basic") + } + } +} diff --git a/build-logic/src/main/kotlin/via.build-logic.gradle.kts b/build-logic/src/main/kotlin/via.build-logic.gradle.kts new file mode 100644 index 000000000..e69de29bb diff --git a/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts b/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts new file mode 100644 index 000000000..98c5515a7 --- /dev/null +++ b/build-logic/src/main/kotlin/via.platform-conventions.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("via.shadow-conventions") +} + +tasks { + shadowJar { + archiveFileName.set("ViaVersion-${project.name.substringAfter("viaversion-").capitalize()}-${project.version}.jar") + destinationDirectory.set(rootProject.layout.buildDirectory.dir("libs")) + } +} diff --git a/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts b/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts new file mode 100644 index 000000000..aee71dfca --- /dev/null +++ b/build-logic/src/main/kotlin/via.shadow-conventions.gradle.kts @@ -0,0 +1,55 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.gradle.jvm.tasks.Jar +import org.gradle.kotlin.dsl.named + +plugins { + id("via.base-conventions") + id("com.github.johnrengelman.shadow") +} + +tasks { + named("jar") { + archiveClassifier.set("unshaded") + from(project.rootProject.file("LICENSE")) + } + val shadowJar = named("shadowJar") { + archiveClassifier.set("") + configureRelocations() + configureExcludes() + } + named("build") { + dependsOn(shadowJar) + } +} + +publishShadowJar() + +fun ShadowJar.configureRelocations() { + relocate("javassist", "com.viaversion.viaversion.libs.javassist") + relocate("com.google.gson", "com.viaversion.viaversion.libs.gson") + relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt") + relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil") +} + +fun ShadowJar.configureExcludes() { + // FastUtil - we only want object and int maps + // Object types + exclude("it/unimi/dsi/fastutil/*/*Reference*") + exclude("it/unimi/dsi/fastutil/*/*Boolean*") + exclude("it/unimi/dsi/fastutil/*/*Byte*") + exclude("it/unimi/dsi/fastutil/*/*Short*") + exclude("it/unimi/dsi/fastutil/*/*Float*") + exclude("it/unimi/dsi/fastutil/*/*Double*") + exclude("it/unimi/dsi/fastutil/*/*Long*") + exclude("it/unimi/dsi/fastutil/*/*Char*") + // Map types + exclude("it/unimi/dsi/fastutil/*/*Custom*") + exclude("it/unimi/dsi/fastutil/*/*Tree*") + exclude("it/unimi/dsi/fastutil/*/*Heap*") + exclude("it/unimi/dsi/fastutil/*/*Queue*") + // Crossing fingers + exclude("it/unimi/dsi/fastutil/*/*Big*") + exclude("it/unimi/dsi/fastutil/*/*Synchronized*") + exclude("it/unimi/dsi/fastutil/*/*Unmodifiable*") + exclude("it/unimi/dsi/fastutil/io/*") +} diff --git a/build-logic/src/main/kotlin/via.standard-conventions.gradle.kts b/build-logic/src/main/kotlin/via.standard-conventions.gradle.kts new file mode 100644 index 000000000..519de9f7a --- /dev/null +++ b/build-logic/src/main/kotlin/via.standard-conventions.gradle.kts @@ -0,0 +1,5 @@ +plugins { + id("via.base-conventions") +} + +publishJavaComponents() diff --git a/build.gradle.kts b/build.gradle.kts index f1add7eeb..93750893e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,104 +1,40 @@ -import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME plugins { - `java-library` - `maven-publish` - id("net.kyori.blossom") version "1.2.0" apply false + id("via.build-logic") } allprojects { group = "com.viaversion" - version = "4.0.0-21w20a-SNAPSHOT" + version = property("projectVersion") as String // from gradle.properties description = "Allow newer clients to join older server versions." } +val platforms = setOf( + projects.viaversionBukkit, + projects.viaversionBungee, + projects.viaversionFabric, + projects.viaversionSponge, + projects.viaversionVelocity +).map { it.dependencyProject } + +val special = setOf( + projects.viaversion, + projects.viaversionApi, + projects.adventure +).map { it.dependencyProject } + subprojects { - apply() - apply() - - tasks { - // Variable replacements - processResources { - filesMatching(listOf("plugin.yml", "mcmod.info", "fabric.mod.json", "bungee.yml")) { - expand("version" to project.version, "description" to project.description) - } - } - withType { - options.encoding = Charsets.UTF_8.name() - } - withType { - options.encoding = Charsets.UTF_8.name() - options.compilerArgs.addAll(listOf("-nowarn", "-Xlint:-unchecked", "-Xlint:-deprecation")) - } - } - - repositories { - maven("https://repo.viaversion.com") - maven("https://papermc.io/repo/repository/maven-public/") - maven("https://oss.sonatype.org/content/repositories/snapshots/") - maven("https://nexus.velocitypowered.com/repository/velocity-artifacts-snapshots/") - maven("https://repo.spongepowered.org/repository/maven-public/") - maven("https://libraries.minecraft.net") - mavenCentral() + when (this) { + in platforms -> plugins.apply("via.platform-conventions") + in special -> plugins.apply("via.base-conventions") + else -> plugins.apply("via.standard-conventions") } dependencies { // Note: If manually starting tests doesn't work for you in IJ, change 'Gradle -> Run Tests Using' to 'IntelliJ IDEA' - testImplementation(rootProject.libs.netty) - testImplementation(rootProject.libs.guava) - testImplementation(rootProject.libs.bundles.junit) - } - - configureJavaTarget(8) - java { - withSourcesJar() - withJavadocJar() - } -} - -// Configure shadow tasks before the publishing task -sequenceOf( - projects.viaversionBukkit, - projects.viaversionBungee, - projects.viaversionFabric, - projects.viaversionSponge, - projects.viaversionVelocity -).map { it.dependencyProject }.forEach { project -> - project.configureShadowJar() -} - -projects.viaversionApi.dependencyProject.configureShadowJarAPI() -projects.viaversion.dependencyProject.apply() - -subprojects { - publishing { - publications { - create("mavenJava") { - groupId = rootProject.group as String - artifactId = project.name - version = rootProject.version as String - - if (plugins.hasPlugin(ShadowPlugin::class.java)) { - artifact(tasks["shadowJar"]) - } else { - from(components["java"]) - } - } - } - repositories.maven { - name = "Via" - url = uri("https://repo.viaversion.com/") - credentials(PasswordCredentials::class) - authentication { - create("basic") - } - } - } -} - -tasks { - // root project has no useful artifacts - withType { - onlyIf { false } + TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.netty) + TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.guava) + TEST_IMPLEMENTATION_CONFIGURATION_NAME(rootProject.libs.bundles.junit) } } diff --git a/buildSrc/src/main/kotlin/extensions.kt b/buildSrc/src/main/kotlin/extensions.kt deleted file mode 100644 index 20ec7f9f9..000000000 --- a/buildSrc/src/main/kotlin/extensions.kt +++ /dev/null @@ -1,100 +0,0 @@ -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 - -fun Project.configureShadowJarAPI() { - apply() - tasks { - withType { - archiveClassifier.set("") - configureRelocations() - configureExcludes() - } - getByName("build") { - dependsOn(withType()) - } - withType { - if (name == "jar") { - archiveClassifier.set("unshaded") - } - } - } -} - -fun Project.configureShadowJar() { - apply() - tasks { - withType { - archiveClassifier.set("") - archiveFileName.set("ViaVersion-${project.name.substringAfter("viaversion-").capitalize()}-${project.version}.jar") - destinationDirectory.set(rootProject.projectDir.resolve("build/libs")) - configureRelocations() - configureExcludes() - } - getByName("build") { - dependsOn(withType()) - } - withType { - if (name == "jar") { - archiveClassifier.set("unshaded") - } - from(project.parent!!.file("LICENSE")) { - into("") - } - } - } -} - -private fun ShadowJar.configureRelocations() { - relocate("javassist", "com.viaversion.viaversion.libs.javassist") - relocate("com.google.gson", "com.viaversion.viaversion.libs.gson") - relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt") - relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil") -} - -private fun ShadowJar.configureExcludes() { - // FastUtil - we only want object and int maps - // Object types - exclude("it/unimi/dsi/fastutil/*/*Reference*") - exclude("it/unimi/dsi/fastutil/*/*Boolean*") - exclude("it/unimi/dsi/fastutil/*/*Byte*") - exclude("it/unimi/dsi/fastutil/*/*Short*") - exclude("it/unimi/dsi/fastutil/*/*Float*") - exclude("it/unimi/dsi/fastutil/*/*Double*") - exclude("it/unimi/dsi/fastutil/*/*Long*") - exclude("it/unimi/dsi/fastutil/*/*Char*") - // Map types - exclude("it/unimi/dsi/fastutil/*/*Custom*") - exclude("it/unimi/dsi/fastutil/*/*Tree*") - exclude("it/unimi/dsi/fastutil/*/*Heap*") - exclude("it/unimi/dsi/fastutil/*/*Queue*") - // Crossing fingers - exclude("it/unimi/dsi/fastutil/*/*Big*") - exclude("it/unimi/dsi/fastutil/*/*Synchronized*") - exclude("it/unimi/dsi/fastutil/*/*Unmodifiable*") - exclude("it/unimi/dsi/fastutil/io/*") -} - -fun Project.latestCommitHash(): String { - val byteOut = ByteArrayOutputStream() - exec { - commandLine = listOf("git", "rev-parse", "--short", "HEAD") - standardOutput = byteOut - } - 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/gradle.properties b/gradle.properties index f30cf6d13..3c97edf8b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,7 @@ +# Project properties - we put these here so they can be modified without causing a recompile of the build scripts +projectVersion=4.0.0-21w20a-SNAPSHOT + +# Gradle properties org.gradle.daemon=true org.gradle.configureondemand=true -org.gradle.parallel=true \ No newline at end of file +org.gradle.parallel=true diff --git a/java-compat/java-compat-unsafe/build.gradle.kts b/java-compat/java-compat-unsafe/build.gradle.kts index 6b8e88798..19c32d3eb 100644 --- a/java-compat/java-compat-unsafe/build.gradle.kts +++ b/java-compat/java-compat-unsafe/build.gradle.kts @@ -1,3 +1,3 @@ dependencies { - api(project(":java-compat:java-compat-common")) + api(projects.javaCompat.javaCompatCommon) } diff --git a/settings.gradle.kts b/settings.gradle.kts index db3f54f08..995a746ed 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,8 +1,33 @@ -rootProject.name = "viaversion-parent" - enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") enableFeaturePreview("VERSION_CATALOGS") +dependencyResolutionManagement { + // configures repositories for all projects + repositories { + maven("https://repo.viaversion.com") + maven("https://papermc.io/repo/repository/maven-public/") + maven("https://oss.sonatype.org/content/repositories/snapshots/") + maven("https://nexus.velocitypowered.com/repository/velocity-artifacts-snapshots/") + maven("https://repo.spongepowered.org/repository/maven-public/") + maven("https://libraries.minecraft.net") + mavenCentral() + } + // only use these repos + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) +} + +pluginManagement { + // default plugin versions + plugins { + id("net.kyori.blossom") version "1.2.0" + id("com.github.johnrengelman.shadow") version "7.0.0" + } +} + +rootProject.name = "viaversion-parent" + +includeBuild("build-logic") + include("adventure") include("java-compat", "java-compat:java-compat-common", "java-compat:java-compat-unsafe") diff --git a/universal/build.gradle.kts b/universal/build.gradle.kts index dcc0411bb..b88d476fd 100644 --- a/universal/build.gradle.kts +++ b/universal/build.gradle.kts @@ -1,7 +1,11 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +plugins { + id("com.github.johnrengelman.shadow") +} + tasks { - withType { + shadowJar { archiveClassifier.set("") archiveFileName.set("ViaVersion-${project.version}.jar") destinationDirectory.set(rootProject.projectDir.resolve("build/libs")) @@ -13,13 +17,15 @@ tasks { rootProject.projects.viaversionSponge, rootProject.projects.viaversionVelocity, ).map { it.dependencyProject }.forEach { subproject -> - val shadowJarTask = subproject.tasks.getByName("shadowJar", ShadowJar::class) + val shadowJarTask = subproject.tasks.named("shadowJar").forUseAtConfigurationTime().get() dependsOn(shadowJarTask) dependsOn(subproject.tasks.withType()) from(zipTree(shadowJarTask.archiveFile)) } } build { - dependsOn(withType()) + dependsOn(shadowJar) } } + +publishShadowJar()