Skip to content

Commit

Permalink
#453 update Reproducible Builds outputTimestamp when setting version
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Jan 6, 2022
1 parent b827092 commit 04afc2c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ target
.project
*.iml
.idea
/.factorypath
1 change: 1 addition & 0 deletions src/it/it-set-019-outputTimestamp/invoker.properties
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:set -DnewVersion=2.0
14 changes: 14 additions & 0 deletions src/it/it-set-019-outputTimestamp/pom.xml
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-set-019</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>set</name>

<properties>
<project.build.outputTimestamp>10</project.build.outputTimestamp>
</properties>
</project>
3 changes: 3 additions & 0 deletions src/it/it-set-019-outputTimestamp/verify.groovy
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text;

assert pom =~ /<project.build.outputTimestamp>\d\d\d\d+<.project.build.outputTimestamp>/
39 changes: 39 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/SetMojo.java
Expand Up @@ -21,14 +21,18 @@

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -485,6 +489,9 @@ protected synchronized void update( ModifiedPomXMLEventReader pom )
for ( VersionChange versionChange : sourceChanges )
{
changer.apply( versionChange );

// also update project.build.outputTimestamp
updateBuildOutputTimestamp( pom, model );
}
}
catch ( IOException e )
Expand All @@ -494,4 +501,36 @@ protected synchronized void update( ModifiedPomXMLEventReader pom )
log.clearContext();
}

private void updateBuildOutputTimestamp( ModifiedPomXMLEventReader pom, Model model )
throws XMLStreamException
{
String buildOutputTimestamp = model.getProperties().getProperty( "project.build.outputTimestamp" );

if ( buildOutputTimestamp == null || StringUtils.isEmpty( buildOutputTimestamp ) )
{
// no Reproducible Builds output timestamp defined
return;
}

if ( StringUtils.isNumeric( buildOutputTimestamp ) )
{
// int representing seconds since the epoch, like SOURCE_DATE_EPOCH
buildOutputTimestamp = String.valueOf( System.currentTimeMillis() / 1000 );
}
else if ( buildOutputTimestamp.length() <= 1 )
{
// value length == 1 means disable Reproducible Builds
return;
}
else
{
// ISO-8601
DateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" );
df.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
buildOutputTimestamp = df.format( new Date() );
}

PomHelper.setPropertyVersion( pom, null, "project.build.outputTimestamp", buildOutputTimestamp );
}

}

0 comments on commit 04afc2c

Please sign in to comment.