Skip to content

Commit

Permalink
[MRELEASE-1029] update project.build.outputTimestamp property
Browse files Browse the repository at this point in the history
to get an up-to-date timestamp value for Reproducible Builds during
releases
  • Loading branch information
hboutemy committed Nov 23, 2019
1 parent fa7a561 commit cfcc8ca
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
*/

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
Expand Down Expand Up @@ -105,6 +109,13 @@ public void setModelETL( String modelETL )
this.modelETL = modelETL;
}

private long startTime = -1 * 1000;

public void setStartTime( long startTime )
{
this.startTime = startTime;
}

protected abstract String getPomSuffix();

@Override
Expand Down Expand Up @@ -166,6 +177,8 @@ private void transform( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment
List<MavenProject> reactorProjects, boolean simulate, ReleaseResult result )
throws ReleaseExecutionException, ReleaseFailureException
{
result.setStartTime( ( startTime >= 0 ) ? startTime : System.currentTimeMillis() );

for ( MavenProject project : reactorProjects )
{
logInfo( result, "Transforming '" + project.getName() + "'..." );
Expand Down Expand Up @@ -325,6 +338,40 @@ private void transformDocument( MavenProject project, Model modelTarget, Release
}

transformScm( project, modelTarget, releaseDescriptor, projectId, scmRepository, result );

if ( properties != null )
{
rewriteBuildOutputTimestampProperty( properties, result );
}
}

private void rewriteBuildOutputTimestampProperty( Properties properties, ReleaseResult result )
{
String buildOutputTimestamp = properties.getProperty( "project.build.outputTimestamp" );
if ( buildOutputTimestamp == null )
{
// no Reproducible Builds output timestamp defined
return;
}
if ( buildOutputTimestamp.length() <= 1 )
{
// value length == 1 means disable Reproducible Builds
return;
}

if ( StringUtils.isNumeric( buildOutputTimestamp ) )
{
// int representing seconds since the epoch, like SOURCE_DATE_EPOCH
buildOutputTimestamp = String.valueOf( result.getStartTime() / 1000 );
}
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( result.getStartTime() ) );
}
properties.setProperty( "project.build.outputTimestamp", buildOutputTimestamp );
}

private void rewriteVersion( Model modelTarget, ReleaseDescriptor releaseDescriptor, String projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void setUp()
if( phase instanceof AbstractRewritePomsPhase)
{
((AbstractRewritePomsPhase) phase).setModelETL( modelETL );
((AbstractRewritePomsPhase) phase).setStartTime( 0 );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/trunk</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1970-01-01T00:00:00Z</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/trunk</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1970-01-01T00:00:00Z</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/tags/release-label</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1970-01-01T00:00:00Z</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/tags/release-label</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1970-01-01T00:00:00Z</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/tags/release-label</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1970-01-01T00:00:00Z</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
<url>file://localhost/tmp/scm-repo/trunk</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>initial</project.build.outputTimestamp>
<javaVersion>7</javaVersion>
<maven.compiler.source>1.${javaVersion}</maven.compiler.source>
<maven.compiler.target>1.${javaVersion}</maven.compiler.target>
</properties>

<!--
a multi-line
comment
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<javaVersion>7</javaVersion>
<mavenVersion>3.0</mavenVersion>
<maven.site.path>maven-release-archives/maven-release-LATEST</maven.site.path>
<project.build.outputTimestamp>2019-11-23T17:28:05Z</project.build.outputTimestamp>
</properties>

<build>
Expand Down

0 comments on commit cfcc8ca

Please sign in to comment.