Created
November 27, 2024 14:36
-
-
Save chkpnt/eb64f00b759dbc237d6c8072c14ac4ff to your computer and use it in GitHub Desktop.
JasperReports BOM from POM in Gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// root project | |
// of course the filename is build.gradle.kts, but GH-gists requires unique filenames | |
plugins { | |
id("eu.rehost.jasperreports") version "0.14" | |
java | |
} | |
val jasperreportsVersion by extra("6.5.1") | |
jasper { | |
source.set(layout.projectDirectory.dir("src/main/reports")) | |
output.set(layout.buildDirectory.dir("compiledReports")) | |
} | |
dependencies { | |
compileReports(platform(project(":jasperreports-bom"))) | |
compileReports("net.sf.jasperreports:jasperreports") | |
compileReports("net.sf.jasperreports:jasperreports-fonts") | |
compileReports("net.sf.jasperreports:jasperreports-metadata") | |
// jasperreports-functions isn't mentioned in the jasperreports-pom, | |
compileReports("net.sf.jasperreports:jasperreports-functions:$jasperreportsVersion") | |
compileReports("org.springframework:spring-core") | |
compileReports("org.springframework:spring-beans") | |
// Groovy doesn't work with the JasperReports-Gradle-Plugin | |
//compileReports("org.codehaus.groovy:groovy-all") | |
} | |
tasks.jar.configure { | |
from(tasks.compileJasper.map { it.outputDir }) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// subproject jasperreports-bom | |
import org.gradle.api.artifacts.dsl.DependencyConstraintHandler | |
import javax.xml.parsers.DocumentBuilderFactory | |
plugins { | |
`java-platform` | |
} | |
val jasperreportsVersion: String by rootProject.extra | |
val pomConfiguration by configurations.creating { | |
isVisible = false | |
isTransitive = false | |
attributes { | |
attribute(Attribute.of("artifactType", String::class.java), "pom") | |
} | |
} | |
dependencies { | |
pomConfiguration("net.sf.jasperreports:jasperreports:$jasperreportsVersion@pom") | |
val pomFile = pomConfiguration.resolve().singleOrNull() | |
if (pomFile == null) { | |
throw GradleException("POM file not found for net.sf.jasperreports:jasperreports:$jasperreportsVersion") | |
} | |
val docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder() | |
val doc = docBuilder.parse(pomFile) | |
doc.documentElement.normalize() | |
constraints { | |
api("net.sf.jasperreports:jasperreports:$jasperreportsVersion") | |
val dependencies = doc.getElementsByTagName("dependency") | |
for (i in 0 until dependencies.length) { | |
val dependencyNode = dependencies.item(i) | |
if (dependencyNode is org.w3c.dom.Element) { | |
addDependencyConstraint(this, dependencyNode) | |
} | |
} | |
} | |
} | |
fun addDependencyConstraint(constraints: DependencyConstraintHandler, dep: org.w3c.dom.Element) { | |
val groupId = dep.getElementsByTagName("groupId").item(0)?.textContent | |
val artifactId = dep.getElementsByTagName("artifactId").item(0)?.textContent | |
val version = dep.getElementsByTagName("version").item(0)?.textContent | |
if (groupId != null && artifactId != null && version != null) { | |
constraints.api("$groupId:$artifactId") { | |
version { | |
strictly(version) | |
} | |
because("Converted from JasperReports POM") | |
} | |
} | |
} | |
tasks.register("printJasperReportsBOM") { | |
doLast { | |
println("Constraints converted from JasperReports POM:") | |
configurations["apiElements"].allDependencyConstraints | |
.sortedWith(compareBy({it.group}, {it.name})) | |
.forEach { | |
println("- ${it.group}:${it.name}:${it.version}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment