Skip to content

Commit

Permalink
Update project.build.outputTimestamp property to support reproducible…
Browse files Browse the repository at this point in the history
… builds - closes #286
  • Loading branch information
aleksandr-m committed Mar 19, 2021
1 parent fab9768 commit c2d640f
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit c2d640f

Please sign in to comment.