From c2d640f46a85785d5f16ad12cdb9770b27571f74 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Fri, 19 Mar 2021 23:03:42 +0200 Subject: [PATCH] Update project.build.outputTimestamp property to support reproducible builds - closes #286 --- .../plugin/gitflow/AbstractGitFlowMojo.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java index 8de164d5..24f1ff86 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -15,10 +15,14 @@ */ package com.amashchenko.maven.plugin.gitflow; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.TimeZone; import java.util.regex.Pattern; import org.apache.maven.artifact.ArtifactUtils; @@ -49,6 +53,8 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo { private static final String VERSIONS_MAVEN_PLUGIN_SET_PROPERTY_GOAL = "org.codehaus.mojo:versions-maven-plugin:set-property"; /** Name of the tycho-versions-plugin set-version goal. */ private static final String TYCHO_VERSIONS_PLUGIN_SET_GOAL = "org.eclipse.tycho:tycho-versions-plugin:set-version"; + /** Name of the property needed to have reproducible builds. */ + private static final String REPRODUCIBLE_BUILDS_PROPERTY = "project.build.outputTimestamp"; /** System line separator. */ protected static final String LS = System.getProperty("line.separator"); @@ -245,6 +251,18 @@ protected String getCurrentProjectVersion() throws MojoFailureException { return reloadedProject.getVersion(); } + /** + * Gets current project {@link #REPRODUCIBLE_BUILDS_PROPERTY} property value + * from pom.xml file. + * + * @return Value of {@link #REPRODUCIBLE_BUILDS_PROPERTY} property. + * @throws MojoFailureException + */ + private String getCurrentProjectOutputTimestamp() throws MojoFailureException { + final MavenProject reloadedProject = reloadProject(mavenSession.getCurrentProject()); + return reloadedProject.getProperties().getProperty(REPRODUCIBLE_BUILDS_PROPERTY); + } + /** * Reloads project info from file * @@ -1037,6 +1055,24 @@ protected void mvnSetVersions(final String version) throws MojoFailureException, } if (runCommand) { executeMvnCommand(args.toArray(new String[0])); + + String timestamp = getCurrentProjectOutputTimestamp(); + if (timestamp != null && timestamp.length() > 1) { + if (StringUtils.isNumeric(timestamp)) { + // int representing seconds since the epoch + timestamp = String.valueOf(System.currentTimeMillis() / 1000l); + } else { + // ISO-8601 + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + timestamp = df.format(new Date()); + } + + getLog().info("Updating property '" + REPRODUCIBLE_BUILDS_PROPERTY + "' to '" + timestamp + "'."); + + executeMvnCommand(VERSIONS_MAVEN_PLUGIN_SET_PROPERTY_GOAL, "-DgenerateBackupPoms=false", + "-Dproperty=" + REPRODUCIBLE_BUILDS_PROPERTY, "-DnewVersion=" + timestamp); + } } } }