From fcf249166cd715728448984532874f8bb5815054 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sat, 16 Jul 2022 17:10:26 +0100 Subject: [PATCH] Add GH actions Maven publishing, improve buildscript --- .github/workflows/release.yml | 33 ++++++++++++++ .github/workflows/snapshot.yml | 29 ++++++++++++ build.gradle.kts | 82 +++++++++++++++++++++++----------- 3 files changed, 117 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/snapshot.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..64046ea --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +name: Java Gradle Release + +on: + push: + tags: + - 'v\d+.\d+.\d+' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up JDK 8 + uses: actions/setup-java@v2 + with: + java-version: '8' + distribution: 'temurin' + cache: gradle + - name: Publish with Gradle + run: ./gradlew publish + env: + GITHUB_TOKEN: ${{ Secrets.GITHUB_TOKEN }} + BUNNYCDN_TOKEN: ${{ Secrets.BUNNYCDN_TOKEN }} + RELEASE: true + - name: Cleanup Gradle Cache + # Remove some files from the Gradle cache, so they aren't cached by GitHub Actions. + # Restoring these files from a GitHub Actions cache might cause problems for future builds. + run: | + rm -f ~/.gradle/caches/modules-2/modules-2.lock + rm -f ~/.gradle/caches/modules-2/gc.properties \ No newline at end of file diff --git a/.github/workflows/snapshot.yml b/.github/workflows/snapshot.yml new file mode 100644 index 0000000..7b9e044 --- /dev/null +++ b/.github/workflows/snapshot.yml @@ -0,0 +1,29 @@ +name: Java Gradle Snapshot + +on: ["push", "pull_request"] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up JDK 8 + uses: actions/setup-java@v2 + with: + java-version: '8' + distribution: 'temurin' + cache: gradle + - name: Publish with Gradle + run: ./gradlew publish + env: + GITHUB_TOKEN: ${{ Secrets.GITHUB_TOKEN }} + BUNNYCDN_TOKEN: ${{ Secrets.BUNNYCDN_TOKEN }} + - name: Cleanup Gradle Cache + # Remove some files from the Gradle cache, so they aren't cached by GitHub Actions. + # Restoring these files from a GitHub Actions cache might cause problems for future builds. + run: | + rm -f ~/.gradle/caches/modules-2/modules-2.lock + rm -f ~/.gradle/caches/modules-2/gc.properties \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index ca8dda1..232f78d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,6 +22,15 @@ repositories { } val r8 by configurations.creating +val shrinkJarOutput by configurations.creating { + isCanBeResolved = false + isCanBeConsumed = true + + attributes { + attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME)) + attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling::class.java, Bundling.EMBEDDED)) + } +} dependencies { implementation("commons-cli:commons-cli:1.5.0") @@ -35,7 +44,7 @@ dependencies { } application { - mainClassName = "link.infra.packwiz.installer.RequiresBootstrap" + mainClass.set("link.infra.packwiz.installer.RequiresBootstrap") } val gitVersion: groovy.lang.Closure<*> by extra @@ -70,9 +79,11 @@ tasks.shadowJar { exclude("META-INF/NOTICE.txt") } -tasks.register("shrinkJar") { +val shrinkJar by tasks.registering(JavaExec::class) { val rules = file("src/main/proguard.txt") - val r8File = tasks.shadowJar.get().archiveFile.get().asFile.run { resolveSibling(name.removeSuffix(".jar") + "-shrink.jar") } + val r8File = base.libsDirectory.file(provider { + base.archivesName.get() + "-" + project.version + "-all-shrink.jar" + }) dependsOn(configurations.named("runtimeClasspath")) inputs.files(tasks.shadowJar, rules) outputs.file(r8File) @@ -82,7 +93,7 @@ tasks.register("shrinkJar") { args = mutableListOf( "--release", "--classfile", - "--output", r8File.toString(), + "--output", r8File.get().toString(), "--pg-conf", rules.toString(), "--lib", System.getProperty("java.home"), *(if (System.getProperty("java.version").startsWith("1.")) { @@ -93,48 +104,61 @@ tasks.register("shrinkJar") { ) } +artifacts { + add("shrinkJarOutput", shrinkJar) { + classifier = "dist" + } +} + // Used for vscode launch.json -tasks.register("copyJar") { - from(tasks.named("shrinkJar")) +val copyJar by tasks.registering(Copy::class) { + from(shrinkJar) rename("packwiz-installer-(.*)\\.jar", "packwiz-installer.jar") - into("build/libs/") - outputs.file("build/libs/packwiz-installer.jar") + into(layout.buildDirectory.dir("dist")) + outputs.file(layout.buildDirectory.dir("dist").map { file("packwiz-installer.jar") }) } -tasks.assemble { - dependsOn("shrinkJar") - dependsOn("copyJar") +tasks.build { + dependsOn(copyJar) } -if (project.hasProperty("github.token")) { - githubRelease { - owner("comp500") - repo("packwiz-installer") - tagName("${project.version}") - releaseName("Release ${project.version}") - draft(true) - token(findProperty("github.token") as String? ?: "") - releaseAssets(tasks.jar.get().destinationDirectory.file("packwiz-installer.jar").get()) - } +githubRelease { + owner("comp500") + repo("packwiz-installer") + tagName("${project.version}") + releaseName("Release ${project.version}") + draft(true) + token(findProperty("github.token") as String?) + releaseAssets(copyJar) +} - tasks.githubRelease { - dependsOn(tasks.build) - } +tasks.githubRelease { + dependsOn(copyJar) + enabled = project.hasProperty("github.token") && project.findProperty("release") == "true" } tasks.compileKotlin { kotlinOptions { jvmTarget = "1.8" - freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy") + freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-opt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy") } } tasks.compileTestKotlin { kotlinOptions { jvmTarget = "1.8" - freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-Xopt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy") + freeCompilerArgs = listOf("-Xjvm-default=all", "-Xallow-result-return-type", "-opt-in=kotlin.io.path.ExperimentalPathApi", "-Xlambdas=indy") } } +val javaComponent = components["java"] as AdhocComponentWithVariants +javaComponent.addVariantsFromConfiguration(shrinkJarOutput) { + mapToMavenScope("runtime") + mapToOptional() +} +javaComponent.withVariantsFromConfiguration(configurations["shadowRuntimeElements"]) { + skip() +} + if (project.hasProperty("bunnycdn.token")) { publishing { publications { @@ -147,7 +171,11 @@ if (project.hasProperty("bunnycdn.token")) { } repositories { maven { - url = uri("https://storage.bunnycdn.com/comp-maven") + url = if (project.findProperty("release") == "true") { + uri("https://storage.bunnycdn.com/comp-maven/repository/release") + } else { + uri("https://storage.bunnycdn.com/comp-maven/repository/snapshot") + } credentials(HttpHeaderCredentials::class) { name = "AccessKey" value = findProperty("bunnycdn.token") as String?