This is an automated email from the ASF dual-hosted git repository. hboutemy pushed a commit to branch MSHARED-837 in repository https://gitbox.apache.org/repos/asf/maven-archiver.git
commit 33cf3bfc5f51e37f4d06f618af66160e1fb14a70 Author: Hervé Boutemy <[email protected]> AuthorDate: Sat Oct 5 19:21:05 2019 +0200 MSHARED-837 add an API to configure outputTimestamp --- pom.xml | 4 +- .../org/apache/maven/archiver/MavenArchiver.java | 52 ++++++++++++++++++++++ .../apache/maven/archiver/MavenArchiverTest.java | 18 ++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 56386f9..f43b3d5 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-archiver</artifactId> - <version>4.1.0</version> + <version>4.2.0-SNAPSHOT</version> </dependency> <!-- ! plexus-archiver needs this, or else maven-artifact will @@ -102,7 +102,7 @@ <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> - <version>3.2.0</version> + <version>3.3.0</version> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> diff --git a/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/src/main/java/org/apache/maven/archiver/MavenArchiver.java index 521f5c2..82cd3a9 100644 --- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -42,8 +42,12 @@ import org.apache.maven.shared.utils.StringUtils; import javax.lang.model.SourceVersion; import java.io.File; import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Properties; @@ -758,4 +762,52 @@ public class MavenArchiver this.buildJdkSpecDefaultEntry = buildJdkSpecDefaultEntry; } + /** + * Parse output timestamp configured for Reproducible Builds' archive entries, formatted as ISO-8601 + * <code>yyyy-MM-dd'T'HH:mm:ssX</code>. + * + * @param outputTimestamp the value of <code>${project.build.outputTimestamp}</code> (may be <code>null</code>) + * @return the parsed timestamp, may be <code>null</code> if <code>null</code> input or input contains only 1 + * character + * @since 3.4.1 + */ + public Date parseOutputTimestamp( String outputTimestamp ) + { + if ( outputTimestamp == null || outputTimestamp.length() < 2 ) + { + // no timestamp configured (1 character configuration is useful to override a full value during pom + // inheritance) + return null; + } + + DateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" ); + try + { + return df.parse( outputTimestamp ); + } + catch ( ParseException pe ) + { + throw new IllegalArgumentException( "Invalid project.build.outputTimestamp value '" + outputTimestamp + "'", + pe ); + } + } + + /** + * Configure Reproducible Builds archive creation if a timestamp is provided. + * + * @param outputTimestamp the value of <code>${project.build.outputTimestamp}</code> (may be <code>null</code>) + * @return the parsed timestamp + * @since 3.4.1 + * @see #parseOutputTimestamp + * @see Archiver#configureReproducible + */ + public Date configureReproducible( String outputTimestamp ) + { + Date outputDate = parseOutputTimestamp( outputTimestamp ); + if ( outputDate != null ) + { + getArchiver().configureReproducible( outputDate ); + } + return outputDate; + } } diff --git a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java index 6e0e81e..609383a 100644 --- a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java +++ b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java @@ -1503,4 +1503,22 @@ public class MavenArchiverTest } } + + @Test + public void testParseOutputTimestamp() + { + MavenArchiver archiver = new MavenArchiver(); + + assertNull( archiver.parseOutputTimestamp( null ) ); + assertNull( archiver.parseOutputTimestamp( "" ) ); + assertNull( archiver.parseOutputTimestamp( "." ) ); + assertNull( archiver.parseOutputTimestamp( " " ) ); + assertNull( archiver.parseOutputTimestamp( "_" ) ); + + assertEquals( 1570300662000L, archiver.parseOutputTimestamp( "2019-10-05T18:37:42Z" ).getTime() ); + assertEquals( 1570300662000L, archiver.parseOutputTimestamp( "2019-10-05T20:37:42+02:00" ).getTime() ); + assertEquals( 1570300662000L, archiver.parseOutputTimestamp( "2019-10-05T16:37:42-02:00" ).getTime() ); + assertEquals( 1570300662000L, archiver.parseOutputTimestamp( "2019-10-05T20:37:42+0200" ).getTime() ); + assertEquals( 1570300662000L, archiver.parseOutputTimestamp( "2019-10-05T16:37:42-0200" ).getTime() ); + } }
