idempotent license report (#11810)

Co-authored-by: Lauri Tulmin <ltulmin@splunk.com>
This commit is contained in:
SylvainJuge 2024-08-06 12:44:01 +02:00 committed by GitHub
parent 593db2348b
commit a9721df4b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

@ -88,8 +88,7 @@ jobs:
run: |
# add any untracked folders that may have been added by generateLicenseReport
git add licenses
# there's always going to one line difference due to the timestamp included in the report
if [[ $(git diff --cached --shortstat licenses) == " 1 file changed, 1 insertion(+), 1 deletion(-)" ]]
if [[ $(git diff --cached --shortstat licenses) == "" ]]
then
echo "Licenses are up-to-date."
else

View File

@ -2,7 +2,9 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jk1.license.filter.LicenseBundleNormalizer
import com.github.jk1.license.render.InventoryMarkdownReportRenderer
import org.spdx.sbom.gradle.SpdxSbomTask
import java.nio.file.Files
import java.util.UUID
import java.util.regex.Pattern
plugins {
id("com.github.jk1.dependency-license-report")
@ -240,11 +242,29 @@ tasks {
delete(rootProject.file("licenses"))
}
val removeLicenseDate by registering {
// removing the license report date makes it idempotent
doLast {
val filePath = rootDir.toPath().resolve("licenses").resolve("licenses.md")
if (Files.exists(filePath)) {
val datePattern = Pattern.compile("^_[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} .*_$")
val lines = Files.readAllLines(filePath)
// 4th line contains the timestamp of when the license report was generated, replace it with
// an empty line
if (lines.size > 3 && datePattern.matcher(lines[3]).matches()) {
lines[3] = ""
Files.write(filePath, lines)
}
}
}
}
val generateLicenseReportEnabled =
gradle.startParameter.taskNames.any { it.equals("generateLicenseReport") }
named("generateLicenseReport").configure {
dependsOn(cleanLicenses)
finalizedBy(":spotlessApply")
finalizedBy(removeLicenseDate)
// disable licence report generation unless this task is explicitly run
// the files produced by this task are used by other tasks without declaring them as dependency
// which gradle considers an error

2
licenses/licenses.md generated
View File

@ -1,7 +1,7 @@
# javaagent
## Dependency License Report
_2024-07-19 10:32:38 CEST_
## Apache License, Version 2.0
**1** **Group:** `com.blogspot.mydailyjava` **Name:** `weak-lock-free` **Version:** `0.18`