Skip to content

Commit

Permalink
build: Configure reproducible timestamp settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Mar 9, 2024
1 parent 6051fe9 commit 1d3889c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
31 changes: 31 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2022-2024 The Jarviz authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id 'java-library'
id 'groovy'
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
compileOnly gradleApi()
implementation 'org.kordamp.gradle:buildinfo-gradle-plugin:0.46.10'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.kordamp.gradle.archive

import net.nemerosa.versioning.VersioningExtension
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar
import org.kordamp.gradle.plugin.buildinfo.internal.ExtGitInfoService

import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributeView
import java.nio.file.attribute.FileTime
import java.time.Instant
import java.time.ZonedDateTime
import java.util.jar.JarFile
import java.util.stream.Stream

import static org.kordamp.gradle.util.PluginUtils.resolveConfig

class ReproducibleTimestamp extends DefaultTask {
@Input
Jar target

@TaskAction
void updateTimestamp() {
if (resolveConfig(project.rootProject).buildInfo.useCommitTimestamp) {
VersioningExtension versioning = project.rootProject.extensions.findByType(VersioningExtension)
ZonedDateTime timestamp = ExtGitInfoService.getCommitTimestamp(project.rootProject, versioning)

JarFile jarFile = new JarFile(target.archiveFile.get().asFile)
try (FileSystem zipfs = FileSystems.newFileSystem(Path.of(jarFile.name),
this.getClass().getClassLoader())) {
FileTime lastModifiedTime = FileTime.from(Instant.from(timestamp))
try (Stream<Path> stream = Files.walk(zipfs.getPath('/'))) {
stream.forEach { path ->
try {
Files.getFileAttributeView(path, BasicFileAttributeView)
.setTimes(lastModifiedTime, lastModifiedTime, lastModifiedTime)
} catch (Exception e) {
// ignore
}
}
}
}
}
}
}
41 changes: 30 additions & 11 deletions plugins/jarviz-tool-provider/jarviz-tool-provider.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,46 @@ shadowJar {
exclude 'plugin.xml'
}

task relocateShadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation) {
target = tasks.shadowJar
prefix = 'jarviz.shadow'
}

tasks.shadowJar.dependsOn tasks.relocateShadowJar
tasks.shadowJar.dependsOn(tasks.register('relocateShadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation) { t->
t.target = tasks.shadowJar
t.prefix = 'jarviz.shadow'
})

jar.finalizedBy(shadowJar)
addMainModuleInfo.mustRunAfter(shadowJar)

def reproducibleTimestamp = tasks.register('reproducibleTimestamp', org.kordamp.gradle.archive.ReproducibleTimestamp) {t ->
t.target = tasks.shadowJar
t.dependsOn(tasks.addMainModuleInfo)
t.mustRunAfter(tasks.addMainModuleInfo)
}
shadowJar.finalizedBy(reproducibleTimestamp)
assemble.dependsOn(reproducibleTimestamp)

tasks.withType(JavaCompile) { JavaCompile c ->
c.sourceCompatibility = JavaVersion.VERSION_11
c.targetCompatibility = JavaVersion.VERSION_11
}

task versionFile {
inputs.property('version', project.version)
outputs.file(project.layout.buildDirectory.file('VERSION'))
doLast {
classes.dependsOn(tasks.register('version') {t ->
t.inputs.property('version', project.version)
t.outputs.file(project.layout.buildDirectory.file('VERSION'))
t.doLast {
project.layout.buildDirectory.file('VERSION').get().asFile.text = project.version
}
})

['startScripts', 'distTar', 'distZip',
'startShadowScripts', 'shadowDistTar', 'shadowDistZip'].each { n ->
tasks.named(n) { t -> t.enabled = false }
}

classes.dependsOn(versionFile)
afterEvaluate {
def shadowJar = project.tasks.findByName('shadowJar')
def publishTask = project.tasks.findByName('publishMainPublicationToMavenLocal')
if (publishTask && shadowJar) publishTask.dependsOn(shadowJar)
publishTask = tasks.findByName('publishMainPublicationToLocalSnapshotRepository')
if (publishTask && shadowJar) publishTask.dependsOn(shadowJar)
publishTask = tasks.findByName('publishMainPublicationToLocalReleaseRepository')
if (publishTask && shadowJar) publishTask.dependsOn(shadowJar)
}

0 comments on commit 1d3889c

Please sign in to comment.