From aa0ad16a1b258df26cb3973d7d267359e52060ec Mon Sep 17 00:00:00 2001 From: Matt <4009945+MattBDev@users.noreply.github.com> Date: Fri, 14 May 2021 17:42:37 -0400 Subject: [PATCH] Update to Gradle 7 (#1078) --- buildSrc/build.gradle.kts | 3 +- buildSrc/src/main/kotlin/CommonConfig.kt | 4 +- buildSrc/src/main/kotlin/LibsConfig.kt | 136 ++++++++++++++++----- buildSrc/src/main/kotlin/PlatformConfig.kt | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- worldedit-bukkit/build.gradle.kts | 2 +- worldedit-core/doctools/build.gradle.kts | 4 +- 7 files changed, 113 insertions(+), 42 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 71c5142f7..3dd5403a2 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -6,7 +6,6 @@ plugins { } repositories { - jcenter() gradlePluginPortal() maven { name = "EngineHub" @@ -23,5 +22,5 @@ val properties = Properties().also { props -> dependencies { implementation(gradleApi()) implementation("org.ajoberstar.grgit:grgit-gradle:4.1.0") - implementation("com.github.jengelman.gradle.plugins:shadow:6.1.0") + implementation("gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.0.0") } diff --git a/buildSrc/src/main/kotlin/CommonConfig.kt b/buildSrc/src/main/kotlin/CommonConfig.kt index 4b3562904..6036a29c0 100644 --- a/buildSrc/src/main/kotlin/CommonConfig.kt +++ b/buildSrc/src/main/kotlin/CommonConfig.kt @@ -1,6 +1,7 @@ import org.gradle.api.Project import org.gradle.api.plugins.JavaPluginExtension import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JvmVendorSpec import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.repositories @@ -50,7 +51,8 @@ fun Project.applyCommonConfiguration() { plugins.withId("java") { the().toolchain { - languageVersion.set(JavaLanguageVersion.of(8)) + languageVersion.set(JavaLanguageVersion.of(11)) + vendor.set(JvmVendorSpec.ADOPTOPENJDK) } } diff --git a/buildSrc/src/main/kotlin/LibsConfig.kt b/buildSrc/src/main/kotlin/LibsConfig.kt index eac7e6746..a13d012a5 100644 --- a/buildSrc/src/main/kotlin/LibsConfig.kt +++ b/buildSrc/src/main/kotlin/LibsConfig.kt @@ -1,16 +1,27 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.ExternalModuleDependency import org.gradle.api.artifacts.ModuleDependency -import org.gradle.api.internal.HasConvention -import org.gradle.api.plugins.MavenRepositoryHandlerConvention -import org.gradle.api.tasks.Upload +import org.gradle.api.attributes.Bundling +import org.gradle.api.attributes.Category +import org.gradle.api.attributes.DocsType +import org.gradle.api.attributes.LibraryElements +import org.gradle.api.attributes.Usage +import org.gradle.api.attributes.java.TargetJvmVersion +import org.gradle.api.component.AdhocComponentWithVariants +import org.gradle.api.component.SoftwareComponentFactory +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication import org.gradle.api.tasks.bundling.Jar import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.getPlugin import org.gradle.kotlin.dsl.invoke +import org.gradle.kotlin.dsl.named import org.gradle.kotlin.dsl.register +import javax.inject.Inject fun Project.applyLibrariesConfiguration() { applyCommonConfiguration() @@ -20,14 +31,13 @@ fun Project.applyLibrariesConfiguration() { configurations { create("shade") - getByName("archives").extendsFrom(getByName("default")) } group = "${rootProject.group}.worldedit-libs" val relocations = mapOf( - "net.kyori.text" to "com.sk89q.worldedit.util.formatting.text", - "net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori" + "net.kyori.text" to "com.sk89q.worldedit.util.formatting.text", + "net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori", ) tasks.register("jar") { @@ -47,22 +57,22 @@ fun Project.applyLibrariesConfiguration() { } val altConfigFiles = { artifactType: String -> val deps = configurations["shade"].incoming.dependencies - .filterIsInstance() - .map { it.copy() } - .map { dependency -> - dependency.artifact { - name = dependency.name - type = artifactType - extension = "jar" - classifier = artifactType - } - dependency + .filterIsInstance() + .map { it.copy() } + .map { dependency -> + dependency.artifact { + name = dependency.name + type = artifactType + extension = "jar" + classifier = artifactType } + dependency + } files(configurations.detachedConfiguration(*deps.toTypedArray()) - .resolvedConfiguration.lenientConfiguration.artifacts - .filter { it.classifier == artifactType } - .map { zipTree(it.file) }) + .resolvedConfiguration.lenientConfiguration.artifacts + .filter { it.classifier == artifactType } + .map { zipTree(it.file) }) } tasks.register("sourcesJar") { from({ @@ -85,25 +95,85 @@ fun Project.applyLibrariesConfiguration() { dependsOn("jar", "sourcesJar") } - artifacts { - val jar = tasks.named("jar") - add("default", jar) { - builtBy(jar) + project.apply() + + val libsComponent = project.components["libs"] as AdhocComponentWithVariants + + val apiElements = project.configurations.register("apiElements") { + isVisible = false + description = "API elements for libs" + isCanBeResolved = false + isCanBeConsumed = true + attributes { + attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_API)) + attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.LIBRARY)) + attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling.SHADOWED)) + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements.JAR)) + attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11) } - val sourcesJar = tasks.named("sourcesJar") - add("archives", sourcesJar) { - builtBy(sourcesJar) + outgoing.artifact(tasks.named("jar")) + } + + val runtimeElements = project.configurations.register("runtimeElements") { + isVisible = false + description = "Runtime elements for libs" + isCanBeResolved = false + isCanBeConsumed = true + attributes { + attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME)) + attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.LIBRARY)) + attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling.SHADOWED)) + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements.JAR)) + attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11) + } + outgoing.artifact(tasks.named("jar")) + } + + val sourcesElements = project.configurations.register("sourcesElements") { + isVisible = false + description = "Source elements for libs" + isCanBeResolved = false + isCanBeConsumed = true + attributes { + attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_RUNTIME)) + attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.DOCUMENTATION)) + attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling.SHADOWED)) + attribute(DocsType.DOCS_TYPE_ATTRIBUTE, project.objects.named(DocsType.SOURCES)) + } + outgoing.artifact(tasks.named("sourcesJar")) + } + + libsComponent.addVariantsFromConfiguration(apiElements.get()) { + mapToMavenScope("compile") + } + + libsComponent.addVariantsFromConfiguration(runtimeElements.get()) { + mapToMavenScope("runtime") + } + + libsComponent.addVariantsFromConfiguration(sourcesElements.get()) { + mapToMavenScope("runtime") + } + + configure { + publications { + register("maven") { + from(libsComponent) + } } } - tasks.register("install") { - configuration = configurations["archives"] - (repositories as HasConvention).convention.getPlugin().mavenInstaller { - pom.version = project.version.toString() - pom.artifactId = project.name - } - } +} +// A horrible hack because `softwareComponentFactory` has to be gotten via plugin +// gradle why +internal open class LibsConfigPluginHack @Inject constructor( + private val softwareComponentFactory: SoftwareComponentFactory +) : Plugin { + override fun apply(project: Project) { + val libsComponents = softwareComponentFactory.adhoc("libs") + project.components.add(libsComponents) + } } fun Project.constrainDependenciesToLibsCore() { diff --git a/buildSrc/src/main/kotlin/PlatformConfig.kt b/buildSrc/src/main/kotlin/PlatformConfig.kt index 3f360013c..0e8b0f558 100644 --- a/buildSrc/src/main/kotlin/PlatformConfig.kt +++ b/buildSrc/src/main/kotlin/PlatformConfig.kt @@ -30,7 +30,7 @@ fun Project.applyPlatformAndCoreConfiguration() { } else { ext["internalVersion"] = "$version" } - + tasks .withType() .matching { it.name == "compileJava" || it.name == "compileTestJava" } @@ -60,7 +60,7 @@ fun Project.applyPlatformAndCoreConfiguration() { "testImplementation"("org.mockito:mockito-core:${Versions.MOCKITO}") "testImplementation"("org.mockito:mockito-junit-jupiter:${Versions.MOCKITO}") "testImplementation"("net.bytebuddy:byte-buddy:1.11.0") - "testRuntime"("org.junit.jupiter:junit-jupiter-engine:${Versions.JUNIT}") + "testRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine:${Versions.JUNIT}") } // Java 8 turns on doclint which we fail diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 549d84424..29e413457 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/worldedit-bukkit/build.gradle.kts b/worldedit-bukkit/build.gradle.kts index 87c1ec37e..eacc93ceb 100644 --- a/worldedit-bukkit/build.gradle.kts +++ b/worldedit-bukkit/build.gradle.kts @@ -61,7 +61,7 @@ dependencies { compileOnly("com.github.MilkBowl:VaultAPI:1.7") { isTransitive = false } api(project(":worldedit-core")) api(project(":worldedit-libs:bukkit")) - compile(":worldedit-adapters:") + implementation(":worldedit-adapters:") // Paper-patched NMS jars compileOnly("com.destroystokyo.paperv1_15_r1:paperv1_15_r1:1_15_r1") compileOnly("com.destroystokyo.paperv1_16_r1:paperv1_16_r1:1_16_r1") diff --git a/worldedit-core/doctools/build.gradle.kts b/worldedit-core/doctools/build.gradle.kts index 7f667b4bc..cdb07e08f 100644 --- a/worldedit-core/doctools/build.gradle.kts +++ b/worldedit-core/doctools/build.gradle.kts @@ -1,14 +1,14 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.4.21" + kotlin("jvm") version "1.5.0-RC" application } applyCommonConfiguration() tasks.withType { - kotlinOptions.jvmTarget = "1.8" + kotlinOptions.jvmTarget = "11" } application.mainClass.set("com.sk89q.worldedit.internal.util.DocumentationPrinter")