12
12
import java .nio .file .Files ;
13
13
import java .nio .file .Path ;
14
14
import java .nio .file .Paths ;
15
- import java .security .MessageDigest ;
16
15
import java .util .Arrays ;
17
16
import java .util .List ;
18
17
import java .util .Objects ;
25
24
import com .google .gson .GsonBuilder ;
26
25
import com .google .gson .reflect .TypeToken ;
27
26
27
+ import fr .jmini .utils .mvnutils .Algorithm ;
28
+ import fr .jmini .utils .mvnutils .Maven ;
29
+ import fr .jmini .utils .mvnutils .MavenArtifact ;
30
+
28
31
public class ECentralTask {
29
32
30
33
private static final String GROUP_ID = "fr.jmini.ecentral" ;
@@ -43,7 +46,6 @@ public ECentralTask(Input input) {
43
46
44
47
public void run () {
45
48
try {
46
- Files .createDirectories (getMavenBomFile ().getParent ());
47
49
Files .createDirectories (getDataFolder ());
48
50
runInternal ();
49
51
} catch (IOException e ) {
@@ -272,7 +274,7 @@ private void createMavenArtifacts() throws IOException {
272
274
}
273
275
274
276
private static boolean checkArtifactInMavenCentral (MavenArtifact artifact ) {
275
- String centralUrl = computeMavenCentralUrl (artifact );
277
+ String centralUrl = Maven . jarMavenCentralUrl (artifact );
276
278
try {
277
279
HttpURLConnection connection = (HttpURLConnection ) new URL (centralUrl ).openConnection ();
278
280
connection .setRequestMethod ("HEAD" );
@@ -283,49 +285,10 @@ private static boolean checkArtifactInMavenCentral(MavenArtifact artifact) {
283
285
}
284
286
}
285
287
286
- static String computeMavenCentralUrl (MavenArtifact artifact ) {
287
- return computeMavenCentralUrl (artifact , ".jar" );
288
- }
289
-
290
- static String computeMavenCentralUrl (MavenArtifact artifact , String extension ) {
291
- // See https://github.com/eclipse/aether-core/blob/aether-0.9.1.v20140329/aether-util/src/main/java/org/eclipse/aether/util/repository/layout/MavenDefaultLayout.java#L42
292
-
293
- StringBuilder sb = new StringBuilder ();
294
- sb .append ("https://repo1.maven.org/maven2/" );
295
- sb .append (subPathInMavenRepo (artifact , extension ));
296
- return sb .toString ();
297
- }
298
-
299
- private static String subPathInMavenRepo (MavenArtifact artifact , String extension ) {
300
- StringBuilder sb = new StringBuilder ();
301
- sb .append (artifact .getGroupId ()
302
- .replace ('.' , '/' ));
303
- sb .append ('/' );
304
- sb .append (artifact .getArtifactId ());
305
- sb .append ('/' );
306
- sb .append (artifact .getVersion ());
307
- sb .append ('/' );
308
- sb .append (artifact .getArtifactId ());
309
- sb .append ('-' );
310
- sb .append (artifact .getVersion ());
311
- sb .append (extension );
312
- return sb .toString ();
313
- }
314
-
315
288
private void createMavenBomFile () throws IOException {
316
289
List <MavenArtifact > entries = parseArtifactsFile (getMavenArtifactsFile ());
317
290
String content = createMavenBomContent (entries );
318
- Files .writeString (getMavenBomFile (), content , StandardCharsets .UTF_8 );
319
-
320
- writeHash (content , Algorithm .MD_5 );
321
- writeHash (content , Algorithm .SHA_1 );
322
- writeHash (content , Algorithm .SHA_256 );
323
- writeHash (content , Algorithm .SHA_512 );
324
- }
325
-
326
- private void writeHash (String content , Algorithm algorithm ) throws IOException {
327
- String hash = calculateHash (content , algorithm );
328
- Files .writeString (getMavenBomFile (".pom" + algorithm .getExtension ()), hash , StandardCharsets .UTF_8 );
291
+ Maven .writeFileToRepositoryWithArmoredFiles (Paths .get ("repo" ), getBomArtifact (), ".pom" , content , Algorithm .MD_5 , Algorithm .SHA_1 , Algorithm .SHA_256 , Algorithm .SHA_512 );
329
292
}
330
293
331
294
private String createMavenBomContent (List <MavenArtifact > entries ) {
@@ -376,25 +339,6 @@ private String createMavenBomContent(List<MavenArtifact> entries) {
376
339
return sb .toString ();
377
340
}
378
341
379
- static String calculateHash (String content , Algorithm algorithm ) {
380
- MessageDigest digest = algorithm .getMessageDigest ();
381
- byte [] encodedhash = digest .digest (
382
- content .getBytes (StandardCharsets .UTF_8 ));
383
- return bytesToHex (encodedhash );
384
- }
385
-
386
- private static String bytesToHex (byte [] hash ) {
387
- StringBuilder hexString = new StringBuilder (2 * hash .length );
388
- for (int i = 0 ; i < hash .length ; i ++) {
389
- String hex = Integer .toHexString (0xff & hash [i ]);
390
- if (hex .length () == 1 ) {
391
- hexString .append ('0' );
392
- }
393
- hexString .append (hex );
394
- }
395
- return hexString .toString ();
396
- }
397
-
398
342
private void writeArtifactsToFile (Path file , List <MavenArtifact > artifacts ) throws IOException {
399
343
Gson gson = new GsonBuilder ()
400
344
.setPrettyPrinting ()
@@ -425,13 +369,7 @@ private Path getMavenArtifactsFile() {
425
369
return getDataFolder ().resolve ("maven-artifacts.json" );
426
370
}
427
371
428
- private Path getMavenBomFile () {
429
- return getMavenBomFile (".pom" );
430
- }
431
-
432
- private Path getMavenBomFile (String extension ) {
433
- MavenArtifact artifact = new MavenArtifact (GROUP_ID , ARTIFACT_ID , input .getReleaseVersion ());
434
- return Paths .get ("repo" )
435
- .resolve (subPathInMavenRepo (artifact , extension ));
372
+ private MavenArtifact getBomArtifact () {
373
+ return new MavenArtifact (GROUP_ID , ARTIFACT_ID , input .getReleaseVersion ());
436
374
}
437
375
}
0 commit comments