|
18 | 18 |
|
19 | 19 | package pl.project13.maven.git; |
20 | 20 |
|
| 21 | +import com.google.common.annotations.VisibleForTesting; |
21 | 22 | import java.io.File; |
22 | 23 | import java.nio.charset.Charset; |
23 | 24 | import java.nio.charset.StandardCharsets; |
24 | | -import java.text.DateFormat; |
25 | | -import java.text.ParseException; |
26 | 25 | import java.text.SimpleDateFormat; |
| 26 | +import java.time.Instant; |
27 | 27 | import java.util.Collections; |
28 | 28 | import java.util.Date; |
29 | 29 | import java.util.List; |
|
44 | 44 | import org.apache.maven.plugins.annotations.Parameter; |
45 | 45 | import org.apache.maven.project.MavenProject; |
46 | 46 | import org.apache.maven.settings.Settings; |
| 47 | +import org.joda.time.DateTime; |
47 | 48 | import org.sonatype.plexus.build.incremental.BuildContext; |
48 | 49 | import pl.project13.core.CommitIdGenerationMode; |
49 | 50 | import pl.project13.core.CommitIdPropertiesOutputFormat; |
@@ -497,20 +498,28 @@ public class GitCommitIdMojo extends AbstractMojo { |
497 | 498 | * represents dates or times exported by this plugin (e.g. {@code git.commit.time}, {@code |
498 | 499 | * git.build.time}). It should be a valid {@link SimpleDateFormat} string. |
499 | 500 | * |
500 | | - * <p>The current dateFormat is set to match maven's default {@code yyyy-MM-dd'T'HH:mm:ssZ}. |
501 | | - * Please note that in previous versions (2.2.0 - 2.2.2) the default dateFormat was set to: {@code |
502 | | - * dd.MM.yyyy '@' HH:mm:ss z}. However the {@code RFC 822 time zone} seems to give a more reliable |
503 | | - * option in parsing the date and it's being used in maven as default. |
| 501 | + * <p>The current dateFormat will be formatted as ISO 8601 |
| 502 | + * {@code yyyy-MM-dd'T'HH:mm:ssXXX} and therefore can be used as input to maven's |
| 503 | + * <a href="https://maven.apache.org/guides/mini/guide-reproducible-builds.html"> |
| 504 | + * reproducible build</a> feature. |
| 505 | + * |
| 506 | + * Please note that in previous versions |
| 507 | + * (2.2.2 - 7.0.1) the default format was set to {@code yyyy-MM-dd'T'HH:mm:ssZ} |
| 508 | + * which produces a {@code RFC 822 time zone}. While such format gives reliable |
| 509 | + * options in parsing the date, it does not comply with the requirements of |
| 510 | + * the reproducible build feature. |
| 511 | + * (2.2.0 - 2.2.2) the default dateFormat was set to: {@code |
| 512 | + * dd.MM.yyyy '@' HH:mm:ss z}. |
504 | 513 | * |
505 | 514 | * <p>Example: |
506 | 515 | * |
507 | 516 | * <pre>{@code |
508 | | - * <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat> |
| 517 | + * <dateFormat>yyyy-MM-dd'T'HH:mm:ssXXX</dateFormat> |
509 | 518 | * }</pre> |
510 | 519 | * |
511 | 520 | * @since 2.2.0 |
512 | 521 | */ |
513 | | - @Parameter(defaultValue = "yyyy-MM-dd'T'HH:mm:ssZ") |
| 522 | + @Parameter(defaultValue = "yyyy-MM-dd'T'HH:mm:ssXXX") |
514 | 523 | String dateFormat; |
515 | 524 |
|
516 | 525 | /** |
@@ -1454,32 +1463,26 @@ private Properties getContextProperties(MavenProject project) { |
1454 | 1463 | * href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>. |
1455 | 1464 | * |
1456 | 1465 | * <p>Inspired by |
1457 | | - * https://github.com/apache/maven-archiver/blob/a3103d99396cd8d3440b907ef932a33563225265/src/main/java/org/apache/maven/archiver/MavenArchiver.java#L765 |
| 1466 | + * https://github.com/apache/maven-archiver/blob/7acb1db4a9754beacde3f21a69e5523ee901abd5/src/main/java/org/apache/maven/archiver/MavenArchiver.java#L755 |
1458 | 1467 | * |
1459 | 1468 | * @param outputTimestamp the value of <code>${project.build.outputTimestamp}</code> (may be |
1460 | 1469 | * <code>null</code>) |
1461 | 1470 | * @return the parsed timestamp, may be <code>null</code> if <code>null</code> input or input |
1462 | 1471 | * contains only 1 character |
1463 | 1472 | */ |
1464 | | - private Date parseOutputTimestamp(String outputTimestamp) throws GitCommitIdExecutionException { |
| 1473 | + @VisibleForTesting |
| 1474 | + protected static Date parseOutputTimestamp(String outputTimestamp) { |
1465 | 1475 | if (outputTimestamp != null |
1466 | 1476 | && !outputTimestamp.trim().isEmpty() |
1467 | 1477 | && outputTimestamp.chars().allMatch(Character::isDigit)) { |
1468 | | - return new Date(Long.parseLong(outputTimestamp) * 1000); |
| 1478 | + return Date.from(Instant.ofEpochSecond(Long.parseLong(outputTimestamp))); |
1469 | 1479 | } |
1470 | 1480 |
|
1471 | 1481 | if ((outputTimestamp == null) || (outputTimestamp.length() < 2)) { |
1472 | 1482 | // no timestamp configured |
1473 | 1483 | return null; |
1474 | 1484 | } |
1475 | | - |
1476 | | - DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); |
1477 | | - try { |
1478 | | - return df.parse(outputTimestamp); |
1479 | | - } catch (ParseException pe) { |
1480 | | - throw new GitCommitIdExecutionException( |
1481 | | - "Invalid 'project.build.outputTimestamp' value '" + outputTimestamp + "'", pe); |
1482 | | - } |
| 1485 | + return new DateTime(outputTimestamp).toDate(); |
1483 | 1486 | } |
1484 | 1487 |
|
1485 | 1488 | private void publishPropertiesInto(Properties propertiesToPublish, Properties propertiesTarget) { |
|
0 commit comments