This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git

commit 3d6ce4e39b74906a86e8862724baac0aa56e2df8
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Oct 27 22:19:32 2026 +0000

    [#679] Take the NOTICE copyright year from project.build.outputTimestamp
    
    ApacheNoticeResourceTransformer ended its copyright line with the current
    year, so the shaded jar changed with the year it was built in. It now uses
    the year of project.build.outputTimestamp when that is set, and a new
    copyrightYear setting takes precedence over both.
---
 .../apache-notice-output-timestamp/pom.xml         | 74 ++++++++++++++++++++++
 .../src/main/resources/META-INF/NOTICE             |  1 +
 .../apache-notice-output-timestamp/verify.groovy   | 28 ++++++++
 .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 47 ++++++++++++++
 .../resource/ApacheNoticeResourceTransformer.java  | 20 +++++-
 .../markdown/examples/resource-transformers.md.vm  |  2 +
 .../shade/mojo/OutputTimestampYearTest.java        | 49 ++++++++++++++
 .../ApacheNoticeResourceTransformerTest.java       | 22 +++++++
 8 files changed, 242 insertions(+), 1 deletion(-)

diff --git a/src/it/projects/apache-notice-output-timestamp/pom.xml 
b/src/it/projects/apache-notice-output-timestamp/pom.xml
new file mode 100644
index 00000000..f6cf606a
--- /dev/null
+++ b/src/it/projects/apache-notice-output-timestamp/pom.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.shade.anot</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <name>apache-notice-output-timestamp</name>
+  <description>
+    Test that a generated NOTICE takes its copyright year from 
project.build.outputTimestamp.
+  </description>
+
+  <properties>
+    <slf4j.version>1.7.36</slf4j.version>
+    
<project.build.outputTimestamp>2019-06-01T00:00:00Z</project.build.outputTimestamp>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <version>${slf4j.version}</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>attach-shade</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <shadedArtifactAttached>true</shadedArtifactAttached>
+              <transformers>
+                <transformer 
implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
+                  <projectName>Shade IT</projectName>
+                </transformer>
+              </transformers>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git 
a/src/it/projects/apache-notice-output-timestamp/src/main/resources/META-INF/NOTICE
 
b/src/it/projects/apache-notice-output-timestamp/src/main/resources/META-INF/NOTICE
new file mode 100644
index 00000000..c3ac3f0f
--- /dev/null
+++ 
b/src/it/projects/apache-notice-output-timestamp/src/main/resources/META-INF/NOTICE
@@ -0,0 +1 @@
+Dependency notice
diff --git a/src/it/projects/apache-notice-output-timestamp/verify.groovy 
b/src/it/projects/apache-notice-output-timestamp/verify.groovy
new file mode 100644
index 00000000..3dc34a64
--- /dev/null
+++ b/src/it/projects/apache-notice-output-timestamp/verify.groovy
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.jar.JarFile
+
+def jar = new JarFile(new File(basedir, 'target/test-1.0-shaded.jar'))
+try {
+    def notice = jar.getInputStream(jar.getEntry('META-INF/NOTICE')).text
+    assert notice.contains('Copyright 2006-2019 The Apache Software 
Foundation') : notice
+} finally {
+    jar.close()
+}
diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java 
b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
index eb000c1e..d4c5cd8d 100644
--- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
+++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
@@ -26,6 +26,10 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Writer;
 import java.nio.file.Files;
+import java.time.Instant;
+import java.time.OffsetDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeParseException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -444,6 +448,16 @@ public class ShadeMojo extends AbstractMojo {
     @Parameter
     private List<String> extraArtifacts;
 
+    /**
+     * Timestamp for reproducible output, in ISO 8601 or as seconds since the 
epoch. Its year ends the copyright line of
+     * a NOTICE generated by {@code ApacheNoticeResourceTransformer} that sets 
no {@code copyrightYear}, instead of the
+     * current year.
+     *
+     * @since 3.6.3
+     */
+    @Parameter(defaultValue = "${project.build.outputTimestamp}")
+    private String outputTimestamp;
+
     @Inject
     private MavenProjectHelper projectHelper;
 
@@ -984,9 +998,42 @@ public class ShadeMojo extends AbstractMojo {
                 ((ApacheNoticeResourceTransformer) 
transformer).setProjectNameIfUnset(project.getName());
             }
         }
+        String year = outputTimestampYear(outputTimestamp);
+        if (year == null && outputTimestamp != null && 
outputTimestamp.length() > 1) {
+            getLog().warn("Cannot read a year from outputTimestamp " + 
outputTimestamp
+                    + "; a generated NOTICE uses the current year");
+        }
+        for (ResourceTransformer transformer : transformers) {
+            if (year != null && transformer instanceof 
ApacheNoticeResourceTransformer) {
+                ((ApacheNoticeResourceTransformer) 
transformer).setDefaultCopyrightYear(year);
+            }
+        }
         return Arrays.asList(transformers);
     }
 
+    /**
+     * Reads the UTC year of a {@code project.build.outputTimestamp} value, in 
the formats maven-archiver accepts.
+     *
+     * @return the year, or {@code null} when the value is unset, at most one 
character long (which means "no
+     *         timestamp"), or cannot be parsed
+     */
+    static String outputTimestampYear(String outputTimestamp) {
+        if (outputTimestamp == null || outputTimestamp.length() < 2) {
+            return null;
+        }
+        Instant instant;
+        try {
+            if (outputTimestamp.chars().allMatch(Character::isDigit)) {
+                instant = 
Instant.ofEpochSecond(Long.parseLong(outputTimestamp));
+            } else {
+                instant = OffsetDateTime.parse(outputTimestamp).toInstant();
+            }
+        } catch (DateTimeParseException | NumberFormatException e) {
+            return null;
+        }
+        return String.valueOf(instant.atZone(ZoneOffset.UTC).getYear());
+    }
+
     private List<Filter> getFilters(List<Artifact> artifactCollection, 
ArtifactSelector artifactSelector)
             throws MojoExecutionException {
         List<Filter> filters = new ArrayList<>();
diff --git 
a/src/main/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformer.java
 
b/src/main/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformer.java
index d47b072f..da846122 100644
--- 
a/src/main/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformer.java
+++ 
b/src/main/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformer.java
@@ -64,6 +64,12 @@ public class ApacheNoticeResourceTransformer extends 
AbstractCompatibilityTransf
 
     String inceptionYear = "2006";
 
+    /**
+     * The year that ends the copyright line. Defaults to the year of {@code 
project.build.outputTimestamp} when
+     * that is set, and otherwise to the current year.
+     */
+    String copyrightYear;
+
     String copyright;
 
     /**
@@ -92,6 +98,18 @@ public class ApacheNoticeResourceTransformer extends 
AbstractCompatibilityTransf
         }
     }
 
+    /**
+     * Sets the year that ends the copyright line unless the configuration 
sets {@code copyrightYear}.
+     *
+     * @param year the year of the build's reproducible timestamp
+     * @since 3.6.3
+     */
+    public void setDefaultCopyrightYear(String year) {
+        if (copyrightYear == null) {
+            copyrightYear = year;
+        }
+    }
+
     @Override
     public boolean canTransformResource(String resource) {
         return NOTICE_PATH.equalsIgnoreCase(resource)
@@ -103,7 +121,7 @@ public class ApacheNoticeResourceTransformer extends 
AbstractCompatibilityTransf
     public void processResource(String resource, InputStream is, 
List<Relocator> relocators, long time)
             throws IOException {
         if (entries.isEmpty()) {
-            String year = new SimpleDateFormat("yyyy").format(new Date());
+            String year = copyrightYear != null ? copyrightYear : new 
SimpleDateFormat("yyyy").format(new Date());
             if (!inceptionYear.equals(year)) {
                 year = inceptionYear + "-" + year;
             }
diff --git a/src/site/markdown/examples/resource-transformers.md.vm 
b/src/site/markdown/examples/resource-transformers.md.vm
index 2092c0f9..01b456d0 100644
--- a/src/site/markdown/examples/resource-transformers.md.vm
+++ b/src/site/markdown/examples/resource-transformers.md.vm
@@ -447,6 +447,8 @@ Some licenses (including the [ Apache License, Version 
2](https://www.apache.org
 The generated notice uses the Maven project name by default. This can be 
overridden with the
 `projectName` transformer parameter.
 
+The copyright line of the generated `NOTICE` runs from `inceptionYear` to 
`copyrightYear`. When `copyrightYear` is not configured, it is the year of 
`project.build.outputTimestamp` if that is set, so that the `NOTICE` is 
reproducible, and the current year otherwise.
+
 For example, to simply merge in dependent notices:
 
 ```xml
diff --git 
a/src/test/java/org/apache/maven/plugins/shade/mojo/OutputTimestampYearTest.java
 
b/src/test/java/org/apache/maven/plugins/shade/mojo/OutputTimestampYearTest.java
new file mode 100644
index 00000000..ea26ce73
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/shade/mojo/OutputTimestampYearTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.shade.mojo;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Test for {@link ShadeMojo#outputTimestampYear(String)}.
+ */
+class OutputTimestampYearTest {
+
+    @Test
+    void readsIso8601() {
+        assertEquals("2019", 
ShadeMojo.outputTimestampYear("2019-06-01T00:00:00Z"));
+        assertEquals("2020", 
ShadeMojo.outputTimestampYear("2019-12-31T23:30:00-02:00"), "the year in UTC");
+    }
+
+    @Test
+    void readsSecondsSinceTheEpoch() {
+        assertEquals("2024", ShadeMojo.outputTimestampYear("1704067200"));
+    }
+
+    @Test
+    void unsetOrDisabledOrInvalidGivesNoYear() {
+        assertNull(ShadeMojo.outputTimestampYear(null));
+        assertNull(ShadeMojo.outputTimestampYear(""));
+        assertNull(ShadeMojo.outputTimestampYear("1"));
+        assertNull(ShadeMojo.outputTimestampYear("not a timestamp"));
+    }
+}
diff --git 
a/src/test/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformerTest.java
 
b/src/test/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformerTest.java
index bedf58ba..2e23eb2a 100644
--- 
a/src/test/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformerTest.java
+++ 
b/src/test/java/org/apache/maven/plugins/shade/resource/ApacheNoticeResourceTransformerTest.java
@@ -120,6 +120,28 @@ public class ApacheNoticeResourceTransformerTest {
         processAndFailOnNullPointer("\n");
     }
 
+    @Test
+    public void testCopyrightYearEndsTheCopyrightLine() throws IOException {
+        transformer.copyrightYear = "2019";
+        transformer.processResource(
+                NOTICE_RESOURCE, new ByteArrayInputStream(new byte[0]), 
Collections.<Relocator>emptyList(), 0);
+
+        assertTrue(
+                String.join("", transformer.entries).contains("Copyright 
2006-2019 The Apache Software Foundation"),
+                String.join("", transformer.entries));
+    }
+
+    @Test
+    public void testDefaultCopyrightYearDoesNotOverrideTheConfiguredOne() {
+        transformer.copyrightYear = "2020";
+        transformer.setDefaultCopyrightYear("2019");
+        assertEquals("2020", transformer.copyrightYear);
+
+        ApacheNoticeResourceTransformer unconfigured = new 
ApacheNoticeResourceTransformer();
+        unconfigured.setDefaultCopyrightYear("2019");
+        assertEquals("2019", unconfigured.copyrightYear);
+    }
+
     private void processAndFailOnNullPointer(final String noticeText) throws 
IOException {
         try {
             final ByteArrayInputStream noticeInputStream = new 
ByteArrayInputStream(noticeText.getBytes());

Reply via email to