Skip to content

Commit 46c017d

Browse files
jorsolslawekjaranowski
authored andcommitted
[MJAR-275] - Fix outputTimestamp not applied to module-info; breaks reproducible builds
Signed-off-by: Jorge Solórzano <[email protected]>
1 parent c02be20 commit 46c017d

File tree

7 files changed

+227
-9
lines changed

7 files changed

+227
-9
lines changed

pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@
121121
<artifactId>maven-plugin-annotations</artifactId>
122122
<scope>provided</scope>
123123
</dependency>
124-
<dependency>
125-
<groupId>org.codehaus.plexus</groupId>
126-
<artifactId>plexus-archiver</artifactId>
127-
<version>4.3.0</version>
128-
</dependency>
129124

130125
<!-- Test -->
131126
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# NOTE: Requires Java 10+ to compile the module declaration for Java 9+,
19+
# this is due that compiling the module declaration generates a
20+
# module descriptor with the JDK version on it, making it unreproducible.
21+
invoker.java.version = 10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<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/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>mjar-275-reproducible-multi-release-modular-jar</artifactId>
24+
<name>mjar-275-reproducible-multi-release-modular-jar</name>
25+
<description>Verifies that the modular descriptor is reproducible (timestamp is set)</description>
26+
<packaging>jar</packaging>
27+
<version>1.0-SNAPSHOT</version>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<project.build.outputTimestamp>2022-06-26T13:25:58Z</project.build.outputTimestamp>
32+
</properties>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-jar-plugin</artifactId>
39+
<version>@project.version@</version>
40+
<configuration>
41+
<archive>
42+
<manifest>
43+
<mainClass>myproject.HelloWorld</mainClass>
44+
</manifest>
45+
</archive>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
50+
<pluginManagement>
51+
<plugins>
52+
<plugin>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<version>3.10.1</version>
55+
<configuration>
56+
<release>8</release>
57+
</configuration>
58+
<executions>
59+
<execution>
60+
<id>java9</id>
61+
<goals>
62+
<goal>compile</goal>
63+
</goals>
64+
<configuration>
65+
<release>9</release>
66+
<compileSourceRoots>
67+
<compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>
68+
</compileSourceRoots>
69+
<multiReleaseOutput>true</multiReleaseOutput>
70+
</configuration>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
</plugins>
75+
</pluginManagement>
76+
</build>
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package myproject;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* The classic Hello World App.
24+
*/
25+
public class HelloWorld
26+
{
27+
28+
/**
29+
* Main method.
30+
*
31+
* @param args Not used
32+
*/
33+
public static void main( String[] args )
34+
{
35+
System.out.println( "Hi!" );
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
module myproject {
21+
exports myproject;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.*;
21+
import java.lang.module.*;
22+
import java.nio.file.attribute.FileTime;
23+
import java.time.Instant;
24+
import java.util.*;
25+
import java.util.jar.*;
26+
27+
File target = new File( basedir, "target" )
28+
29+
assert target.exists()
30+
assert target.isDirectory()
31+
32+
File artifact = new File( target, "mjar-275-reproducible-multi-release-modular-jar-1.0-SNAPSHOT.jar" );
33+
34+
assert artifact.exists()
35+
assert artifact.isFile()
36+
37+
JarFile jar = new JarFile( artifact );
38+
39+
Attributes manifest = jar.getManifest().getMainAttributes();
40+
41+
assert "myproject.HelloWorld".equals( manifest.get( Attributes.Name.MAIN_CLASS ) )
42+
43+
InputStream moduleDescriptorInputStream = jar.getInputStream( jar.getEntry( "META-INF/versions/9/module-info.class" ) );
44+
ModuleDescriptor moduleDescriptor = ModuleDescriptor.read( moduleDescriptorInputStream );
45+
46+
assert "myproject.HelloWorld".equals( moduleDescriptor.mainClass().orElse( null ) )
47+
48+
// Normalize to UTC
49+
long normalizeUTC( String timestamp )
50+
{
51+
long millis = Instant.parse( timestamp ).toEpochMilli();
52+
Calendar cal = Calendar.getInstance();
53+
cal.setTimeInMillis( millis );
54+
return millis - ( cal.get( Calendar.ZONE_OFFSET ) + cal.get( Calendar.DST_OFFSET ) );
55+
}
56+
57+
// All entries should have the same timestamp
58+
FileTime expectedTimestamp = FileTime.fromMillis( normalizeUTC( "2022-06-26T13:25:58Z" ) );
59+
Enumeration<JarEntry> entries = jar.entries();
60+
while ( entries.hasMoreElements() )
61+
{
62+
assert expectedTimestamp.equals( entries.nextElement().getLastModifiedTime() )
63+
}
64+
65+
jar.close();

src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ public abstract class AbstractJarMojo
141141
private boolean skipIfEmpty;
142142

143143
/**
144-
* Timestamp for reproducible output archive entries, either formatted as ISO 8601
145-
* <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
146-
* <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
144+
* Timestamp for reproducible output archive entries, either formatted as ISO 8601 extended offset date-time
145+
* (e.g. in UTC such as '2011-12-03T10:15:30Z' or with an offset '2019-10-05T20:37:42+06:00'),
146+
* or as an int representing seconds since the epoch
147+
* (like <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
147148
*
148149
* @since 3.2.0
149150
*/
@@ -257,7 +258,7 @@ public File createArchive()
257258
archiver.setOutputFile( jarFile );
258259

259260
// configure for Reproducible Builds based on outputTimestamp value
260-
archiver.configureReproducible( outputTimestamp );
261+
archiver.configureReproducibleBuild( outputTimestamp );
261262

262263
archive.setForced( forceCreation );
263264

0 commit comments

Comments
 (0)