Skip to content

Commit

Permalink
Replace xerces with javax.xml for Gradle 8.4 compatibility (fixes #814)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Oct 8, 2023
1 parent e31a34b commit 0326a7d
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
repositories {
// Use 'gradle install' to install latest
mavenLocal()
mavenCentral()
gradlePluginPortal()
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion examples/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
repositories {
// Use 'gradle install' to install latest
mavenLocal()
mavenCentral()
gradlePluginPortal()
}

dependencies {
Expand Down
1 change: 0 additions & 1 deletion gradle-versions-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ dependencies {
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.8.20'
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.12.0'
implementation 'com.thoughtworks.xstream:xstream:1.4.20'

testRuntimeOnly files(createClasspathManifest)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.github.benmanes.gradle.versions.reporter

import com.github.benmanes.gradle.versions.reporter.result.DependenciesGroup
import com.github.benmanes.gradle.versions.reporter.result.Dependency
import com.github.benmanes.gradle.versions.reporter.result.DependencyLatest
import com.github.benmanes.gradle.versions.reporter.result.DependencyOutdated
import com.github.benmanes.gradle.versions.reporter.result.DependencyUnresolved
import com.github.benmanes.gradle.versions.reporter.result.Result
import com.github.benmanes.gradle.versions.reporter.result.VersionAvailable
import com.thoughtworks.xstream.XStream
import com.github.benmanes.gradle.versions.updates.gradle.GradleUpdateResult
import org.gradle.api.Project
import org.w3c.dom.Document
import org.w3c.dom.Element
import java.io.OutputStream
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult

/**
* A XML reporter for the dependency updates results.
Expand All @@ -19,20 +22,165 @@ class XmlReporter(
override val revision: String,
override val gradleReleaseChannel: String,
) : AbstractReporter(project, revision, gradleReleaseChannel) {

override fun write(printStream: OutputStream, result: Result) {
val xStream = XStream().apply {
aliasSystemAttribute(null, "class") // Removes attributes={class=sorted-set}
alias("response", Result::class.java)
alias("available", VersionAvailable::class.java)
alias("exceededDependency", DependencyLatest::class.java)
alias("outdatedDependency", DependencyOutdated::class.java)
alias("unresolvedDependency", DependencyUnresolved::class.java)
alias("dependency", Dependency::class.java)
alias("group", DependenciesGroup::class.java)
val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val document = documentBuilder.newDocument()
document.setXmlStandalone(true)

val response = document.createElement("response")
document.appendChild(response)
appendTextChild(document, response, "count", result.count)

writeCurrentSection(result, document, response)
writeOutdatedSection(result, document, response)
writeExceededSection(result, document, response)
writeUndeclaredSection(result, document, response)
writeUnresolvedSection(result, document, response)
writeGradle(result, document, response)

val transformerFactory = TransformerFactory.newInstance()
transformerFactory.setAttribute("indent-number", 2)

val transformer = transformerFactory.newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes")

val source = DOMSource(document)
val resultStream = StreamResult(printStream)
transformer.transform(source, resultStream)
}

private fun writeCurrentSection(result: Result, document: Document, response: Element) {
val current = document.createElement("current")
response.appendChild(current)
appendTextChild(document, current, "count", result.current.count)

val dependencies = document.createElement("dependencies")
current.appendChild(dependencies)
for (dependency in result.current.dependencies) {
writeDependency(document, dependencies, "dependency", dependency)
}
}

private fun writeOutdatedSection(result: Result, document: Document, response: Element) {
val outdated = document.createElement("outdated")
response.appendChild(outdated)
appendTextChild(document, outdated, "count", result.outdated.count)

val dependencies = document.createElement("dependencies")
outdated.appendChild(dependencies)
for (dependency in result.outdated.dependencies) {
var element = writeDependency(document, dependencies, "outdatedDependency", dependency)
writeVersionAvailable(document, element, dependency.available)
}
}

private fun writeVersionAvailable(
document: Document,
dependencyElement: Element,
version: VersionAvailable
) {
val available = document.createElement("available")
dependencyElement.appendChild(available)

printStream.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
printStream.println(xStream.toXML(result).trimMargin())
appendTextChild(document, available, "release", version.release)
appendTextChild(document, available, "milestone", version.milestone)
appendTextChild(document, available, "integration", version.integration)
}

private fun writeExceededSection(result: Result, document: Document, response: Element) {
val exceeded = document.createElement("exceeded")
response.appendChild(exceeded)
appendTextChild(document, exceeded, "count", result.exceeded.count)

val dependencies = document.createElement("dependencies")
exceeded.appendChild(dependencies)
for (dependency in result.exceeded.dependencies) {
var element = writeDependency(document, dependencies, "exceededDependency", dependency)
appendTextChild(document, element, "latest", dependency.latest)
}
}

private fun writeUndeclaredSection(result: Result, document: Document, response: Element) {
val undeclared = document.createElement("undeclared")
response.appendChild(undeclared)
appendTextChild(document, undeclared, "count", result.undeclared.count)

val dependencies = document.createElement("dependencies")
undeclared.appendChild(dependencies)
for (dependency in result.undeclared.dependencies) {
writeDependency(document, dependencies, "dependency", dependency)
}
}

private fun writeUnresolvedSection(result: Result, document: Document, response: Element) {
val unresolved = document.createElement("unresolved")
response.appendChild(unresolved)
appendTextChild(document, unresolved, "count", result.unresolved.count)

val dependencies = document.createElement("dependencies")
unresolved.appendChild(dependencies)
for (dependency in result.unresolved.dependencies) {
val element = writeDependency(document, dependencies, "unresolvedDependency", dependency)
appendTextChild(document, element, "reason", dependency.reason)
}
}

private fun writeGradle(result: Result, document: Document, response: Element) {
val gradle = document.createElement("gradle")
response.appendChild(gradle)
appendTextChild(document, gradle, "enabled", result.gradle.enabled)

writeGradleUpdate(document, gradle, "running", result.gradle.running)
writeGradleUpdate(document, gradle, "current", result.gradle.current)
writeGradleUpdate(document, gradle, "releaseCandidate", result.gradle.releaseCandidate)
writeGradleUpdate(document, gradle, "nightly", result.gradle.nightly)
}

private fun writeGradleUpdate(
document: Document,
element: Element,
name: String,
update: GradleUpdateResult
) {
val channel = document.createElement(name)
element.appendChild(channel)

appendTextChild(document, channel, "version", update.version)
appendTextChild(document, channel, "isUpdateAvailable", update.isUpdateAvailable)
appendTextChild(document, channel, "isFailure", update.isFailure)
appendTextChild(document, channel, "reason", update.reason)
}

private fun writeDependency(
document: Document,
element: Element,
name: String,
dependency: Dependency
): Element {
val dependencyElement = document.createElement(name)
element.appendChild(dependencyElement)

appendTextChild(document, dependencyElement, "group", dependency.group)
appendTextChild(document, dependencyElement, "name", dependency.name)
appendTextChild(document, dependencyElement, "version", dependency.version)
appendTextChild(document, dependencyElement, "projectUrl", dependency.projectUrl)
appendTextChild(document, dependencyElement, "userReason", dependency.userReason)
return dependencyElement
}

private fun appendTextChild(
document: Document,
parent: Element,
name: String,
textContent: Any?
) {
if (textContent != null) {
val element = document.createElement(name)
element.textContent = textContent.toString()
parent.appendChild(element)
}
}

override fun getFileExtension(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ final class DependencyUpdatesSpec extends Specification {
}

when:
def reporter = evaluate(project, 'milestone', 'plain', 'build', null, null, true, "", CURRENT.id)
def reporter = evaluate(project, 'milestone', 'plain', 'build', null, null, true, null, CURRENT.id)
reporter.write()

then:
Expand Down Expand Up @@ -619,7 +619,8 @@ final class DependencyUpdatesSpec extends Specification {
addRepositoryTo(project)

when:
def reporter = evaluate(project, 'milestone', null, 'build', null,null, false, "", RELEASE_CANDIDATE.id, {config -> config.name.equals("upgradesFound")})
def reporter = evaluate(project, 'milestone', null, 'build', null, null, false,
"", RELEASE_CANDIDATE.id, {config -> config.name.equals("upgradesFound")})
reporter.write()

then:
Expand All @@ -644,9 +645,15 @@ final class DependencyUpdatesSpec extends Specification {
}

private static def evaluate(project, revision = 'milestone', outputFormatter = null,
outputDir = 'build', resolutionStrategy = null, reportfileName = null,
checkForGradleUpdate = true, gradleVersionsApiBaseUrl = "", gradleReleaseChannel = RELEASE_CANDIDATE.id,
configurationFilter = { true }) {
outputDir = 'build', resolutionStrategy = null, reportfileName = null,
checkForGradleUpdate = true, gradleVersionsApiBaseUrl = null,
gradleReleaseChannel = RELEASE_CANDIDATE.id, configurationFilter = { true }) {
if (reportfileName == null) {
reportfileName = "report"
}
if (gradleVersionsApiBaseUrl == null) {
gradleVersionsApiBaseUrl = "https://services.gradle.org/versions/"
}
new DependencyUpdates(project, resolutionStrategy, revision, buildOutputFormatter(outputFormatter), outputDir,
reportfileName, checkForGradleUpdate, gradleVersionsApiBaseUrl, gradleReleaseChannel, false, false, configurationFilter).run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ final class DifferentGradleVersionsSpec extends Specification {
'7.3.3',
'7.4.2',
'7.5.1',
'8.0',
'8.1',
'8.2',
'8.3',
'8.4',
]
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.github.ben-manes
VERSION_NAME=0.48.0
VERSION_NAME=0.49.0

POM_INCEPTION_YEAR=2012
POM_PACKAGING=jar
Expand Down

0 comments on commit 0326a7d

Please sign in to comment.