54
54
import java .util .HashSet ;
55
55
import java .util .List ;
56
56
import java .util .Map ;
57
- import java .util .Objects ;
58
57
import java .util .Set ;
59
58
import java .util .TreeMap ;
60
59
import java .util .stream .Collectors ;
61
60
62
61
public abstract class OutputDependenciesToJson extends DefaultTask {
63
62
64
- // From http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java
65
- private static final char [] hexArray = "0123456789abcdef" .toCharArray ();
63
+ private static final char [] hexChars = "0123456789abcdef" .toCharArray ();
66
64
67
65
private static final Gson GSON = new GsonBuilder ().setPrettyPrinting ().create ();
68
66
69
67
/**
70
68
* A single dependency.
71
69
*/
72
- static final class DependencyDescriptor implements Comparable <DependencyDescriptor > {
73
-
74
- final String group ;
75
- final String module ;
76
- final String version ;
77
- final String md5 ;
78
-
79
- DependencyDescriptor (final String group , final String module , final String version , final String md5 ) {
80
- this .group = group ;
81
- this .module = module ;
82
- this .version = version ;
83
- this .md5 = md5 ;
84
- }
85
-
70
+ record DependencyDescriptor (String group , String module , String version , String sha512 ) implements Comparable <DependencyDescriptor > {
86
71
@ Override
87
72
public int compareTo (final DependencyDescriptor that ) {
88
73
final int group = this .group .compareTo (that .group );
@@ -97,35 +82,6 @@ public int compareTo(final DependencyDescriptor that) {
97
82
98
83
return this .version .compareTo (that .version );
99
84
}
100
-
101
- @ Override
102
- public boolean equals (final Object other ) {
103
- if (this == other ) {
104
- return true ;
105
- }
106
- if (other == null || this .getClass () != other .getClass ()) {
107
- return false ;
108
- }
109
- final DependencyDescriptor that = (DependencyDescriptor ) other ;
110
- return Objects .equals (this .group , that .group )
111
- && Objects .equals (this .module , that .module )
112
- && Objects .equals (this .version , that .version );
113
- }
114
-
115
- @ Override
116
- public int hashCode () {
117
- return Objects .hash (this .group , this .module , this .version );
118
- }
119
-
120
- @ Override
121
- public String toString () {
122
- return "DependencyDescriptor{" +
123
- "group='" + this .group + '\'' +
124
- ", module='" + this .module + '\'' +
125
- ", version='" + this .version + '\'' +
126
- ", md5='" + this .md5 + '\'' +
127
- '}' ;
128
- }
129
85
}
130
86
131
87
/**
@@ -134,14 +90,7 @@ public String toString() {
134
90
* <p>At runtime, transitive dependencies won't be traversed, so this needs to
135
91
* include direct + transitive depends.</p>
136
92
*/
137
- static final class DependencyManifest {
138
-
139
- final Map <String , List <DependencyDescriptor >> dependencies ;
140
-
141
- DependencyManifest (final Map <String , List <DependencyDescriptor >> dependencies ) {
142
- this .dependencies = dependencies ;
143
- }
144
- }
93
+ record DependencyManifest (Map <String , List <DependencyDescriptor >> dependencies ) {}
145
94
146
95
/**
147
96
* Configuration to gather dependency artifacts from.
@@ -225,41 +174,42 @@ private List<DependencyDescriptor> configToDescriptor(final Set<ResolvedArtifact
225
174
.map (dependency -> {
226
175
final ModuleComponentIdentifier id = (ModuleComponentIdentifier ) dependency .getId ().getComponentIdentifier ();
227
176
228
- // Get file input stream for reading the file content
229
- final String md5hash ;
177
+ final MessageDigest digest ;
178
+ try {
179
+ digest = MessageDigest .getInstance ("SHA-512" );
180
+ } catch (final NoSuchAlgorithmException e ) {
181
+ throw new GradleException ("Failed to find digest algorithm" , e );
182
+ }
183
+
230
184
try (final InputStream in = Files .newInputStream (dependency .getFile ().toPath ())) {
231
- final MessageDigest hasher = MessageDigest .getInstance ("MD5" );
232
185
final byte [] buf = new byte [4096 ];
233
186
int read ;
234
187
while ((read = in .read (buf )) != -1 ) {
235
- hasher .update (buf , 0 , read );
188
+ digest .update (buf , 0 , read );
236
189
}
237
-
238
- md5hash = OutputDependenciesToJson .toHexString (hasher .digest ());
239
- } catch (final IOException | NoSuchAlgorithmException ex ) {
240
- throw new GradleException ("Failed to create hash for " + dependency , ex );
190
+ } catch (final IOException e ) {
191
+ throw new GradleException ("Failed to digest file for " + dependency , e );
241
192
}
242
193
243
- // create descriptor
244
194
return new DependencyDescriptor (
245
195
id .getGroup (),
246
196
id .getModule (),
247
197
id .getVersion (),
248
- md5hash
198
+ OutputDependenciesToJson . toHexString ( digest . digest ())
249
199
);
250
200
})
251
201
.sorted (Comparator .naturalOrder ()) // sort dependencies for stable output
252
202
.collect (Collectors .toList ());
253
203
}
254
204
255
205
public static String toHexString (final byte [] bytes ) {
256
- final char [] hexChars = new char [bytes .length * 2 ];
257
- for ( int j = 0 ; j < bytes . length ; j ++) {
258
- final int v = bytes [ j ] & 0xFF ;
259
- hexChars [ j * 2 ] = OutputDependenciesToJson .hexArray [ v >>> 4 ];
260
- hexChars [ j * 2 + 1 ] = OutputDependenciesToJson .hexArray [ v & 0x0F ];
206
+ final char [] chars = new char [bytes .length << 1 ];
207
+ int i = 0 ;
208
+ for ( final byte b : bytes ) {
209
+ chars [ i ++ ] = OutputDependenciesToJson .hexChars [( b >> 4 ) & 15 ];
210
+ chars [ i ++ ] = OutputDependenciesToJson .hexChars [ b & 15 ];
261
211
}
262
- return new String (hexChars );
212
+ return new String (chars );
263
213
}
264
214
265
215
public static class ConfigurationHolder {
@@ -274,7 +224,7 @@ public Provider<Set<String>> getIds() {
274
224
return this .getArtifacts ().map (set -> set .stream ()
275
225
.map (art -> art .getId ().getComponentIdentifier ())
276
226
.filter (id -> id instanceof ModuleComponentIdentifier )
277
- .map (art -> art . getDisplayName () )
227
+ .map (ComponentIdentifier :: getDisplayName )
278
228
.collect (Collectors .toSet ()));
279
229
}
280
230
0 commit comments