Skip to content

Instantly share code, notes, and snippets.

@callumvanzyl
Created August 7, 2022 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callumvanzyl/48f42638c6f914bd8d6031a90f63fcda to your computer and use it in GitHub Desktop.
Save callumvanzyl/48f42638c6f914bd8d6031a90f63fcda to your computer and use it in GitHub Desktop.
A Gradle plugin which creates tasks to download platform-specific JRE distributions from Adoptium
package com.project.build
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File
import java.io.FileOutputStream
import java.net.URL
import java.nio.channels.Channels
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
enum class Platform(val friendlyName: String) {
LINUX_X32("Linux32"),
LINUX_X64("Linux64"),
WINDOWS_X32("Windows32"),
WINDOWS_X64("Windows64"),
MAC("Mac")
}
class FetchPackagingDependencies: Plugin<Project> {
private fun downloadFile(url: URL, outputFileName: String) {
url.openStream().use {
Channels.newChannel(it).use { rbc ->
FileOutputStream(outputFileName).use { fos ->
fos.channel.transferFrom(rbc, 0, Long.MAX_VALUE)
}
}
}
}
override fun apply(target: Project) {
for (platform in Platform.values()) {
target.tasks.create("fetchPackagingDependencies$platform") {
doFirst {
val jreFileName = jreFileNameForPlatform(platform)
val dependenciesFolder = Paths.get(target.projectDir.path, "build", "dependencies", platform.name)
Files.createDirectories(dependenciesFolder)
val jrePath = dependenciesFolder.resolve(jreFileName)
if (!Files.exists(jrePath)) {
downloadFile(URL(jreBaseUrl + jreFileName), jrePath.toString())
}
}
}
}
}
companion object {
private const val jreBaseUrl = "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.4%2B8/"
private const val jrePrefix = "OpenJDK17U-jre_"
private const val jrePostfix = "_hotspot_17.0.4_8"
private fun jreFileNameForPlatform(platform: Platform) = when(platform) {
Platform.LINUX_X32 -> "${jrePrefix}x32_linux${jrePostfix}.tar.gz"
Platform.LINUX_X64 -> "${jrePrefix}x64_linux${jrePostfix}.tar.gz"
Platform.WINDOWS_X32 -> "${jrePrefix}x32_windows${jrePostfix}.zip"
Platform.WINDOWS_X64 -> "${jrePrefix}x64_windows${jrePostfix}.zip"
Platform.MAC -> "${jrePrefix}x64_mac${jrePostfix}.tar.gz"
}
fun getJrePath(projectDir: File, platform: Platform): Path {
val jreFileName = jreFileNameForPlatform(platform)
return Paths.get(projectDir.path, "build", "dependencies", platform.name, jreFileName)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment