Skip to content

Migrated the svg library from Ant-based build.xml to Gradle build.gradle.kts #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 138 additions & 1 deletion java/libraries/svg/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,138 @@
ant.importBuild("build.xml")
import java.net.URI

plugins {
id("java-library")
}

repositories {
mavenCentral()
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

val coreJar = file("../../../core/library/core.jar")
val batikVersion = "1.19"

// The .zip file to be downloaded
val batikZip = "batik-bin-$batikVersion.zip"

// The .jar that we need from the download
val batikJar = file("library/batik.jar")

// URL for the version of Batik currently supported by this library
val batikUrl = "https://dlcdn.apache.org//xmlgraphics/batik/binaries/$batikZip"

// Storing a "local" copy in case the original link goes dead. When updating
// releases, please upload the new version to download.processing.org.
val batikBackupUrl = "https://download.processing.org/batik/$batikZip"


// Download Batik dependency if not present
// OK to ignore failed downloads if we at least have a version that's local
val downloadBatik by tasks.registering {
outputs.file(batikJar)

doLast {
if (batikJar.exists()) {
logger.lifecycle("Batik JAR already exists, skipping download.")
return@doLast
}

batikJar.parentFile.mkdirs()
val zipFile = file(batikZip)
val urlsToTry = listOf(batikUrl, batikBackupUrl)

val success = urlsToTry.any { url ->
try {
logger.lifecycle("Downloading Batik from $url")
URI(url).toURL().openStream().use { input ->
zipFile.outputStream().use { output ->
input.copyTo(output)
}
}
copy {
from(zipTree(zipFile)) {
include("batik-$batikVersion/lib/batik-all-$batikVersion.jar")
}
into(batikJar.parentFile)
eachFile {
path = batikJar.name
}
includeEmptyDirs = false
}

zipFile.delete()

if (batikJar.exists()) {
logger.lifecycle("Successfully extracted Batik JAR to ${batikJar.absolutePath}")
true
} else {
logger.error("Extraction failed")
false
}
} catch (e: Exception) {
logger.warn("Failed to download from $url: ${e.message}")
false
}
}

if (!success) {
throw GradleException("Failed to download Batik from all sources and no local copy found.")
}
}
}

tasks.register("checkCore") {
doFirst {
if (!coreJar.exists()) {
throw GradleException("Missing core.jar at $coreJar. Please build the core module first.")
}
}
}

sourceSets {
main {
java {
srcDirs("src")
}
}
}

dependencies {
implementation(project(":core"))
implementation(files(batikJar))
}

// Compile sources
tasks.named<JavaCompile>("compileJava") {
dependsOn(downloadBatik, "checkCore")
options.encoding = "UTF-8"
}

tasks.named<Jar>("jar") {
dependsOn("checkCore")
archiveBaseName.set("svg")
destinationDirectory.set(file("library"))
from(sourceSets.main.get().output)
}

tasks.register<Jar>("svgJar") {
dependsOn("checkCore", "classes")
archiveBaseName.set("svg")
destinationDirectory.set(file("library"))
from(sourceSets.main.get().output)
}

// Clean the build directories
tasks.named<Delete>("clean") {
delete("bin", "library/svg.jar")
}


tasks.register<Delete>("cleanLibs") {
delete(batikJar)
}
83 changes: 0 additions & 83 deletions java/libraries/svg/build.xml

This file was deleted.