26
26
import java .util .Collection ;
27
27
import java .util .HashSet ;
28
28
import java .util .List ;
29
+ import java .util .Optional ;
29
30
import java .util .ResourceBundle ;
30
31
31
32
import org .apache .maven .artifact .Artifact ;
@@ -279,65 +280,70 @@ public final void execute() throws MojoExecutionException {
279
280
jarSigner .setToolchain (toolchain );
280
281
}
281
282
282
- int processed = 0 ;
283
+ List <File > archives = findJarfiles ();
284
+ processArchives (archives );
285
+ getLog ().info (getMessage ("processed" , archives .size ()));
286
+ }
283
287
288
+ /**
289
+ * Finds all jar files, by looking at the Maven project and user configuration.
290
+ *
291
+ * @return a List of File objects
292
+ * @throws MojoExecutionException if it was not possible to build a list of jar files
293
+ */
294
+ private List <File > findJarfiles () throws MojoExecutionException {
284
295
if (this .archive != null ) {
285
- processArchive (this .archive );
286
- processed ++;
287
- } else {
288
- if (processMainArtifact ) {
289
- processed += processArtifact (this .project .getArtifact ()) ? 1 : 0 ;
290
- }
296
+ // Only process this, but nothing more
297
+ return Arrays .asList (this .archive );
298
+ }
291
299
292
- if (processAttachedArtifacts ) {
293
- Collection <String > includes = new HashSet <>();
294
- if (includeClassifiers != null ) {
295
- includes .addAll (Arrays .asList (includeClassifiers ));
296
- }
300
+ List <File > archives = new ArrayList <>();
301
+ if (processMainArtifact ) {
302
+ getFileFromArtifact (this .project .getArtifact ()).ifPresent (archives ::add );
303
+ }
297
304
298
- Collection <String > excludes = new HashSet <>();
299
- if (excludeClassifiers != null ) {
300
- excludes .addAll (Arrays .asList (excludeClassifiers ));
301
- }
305
+ if (processAttachedArtifacts ) {
306
+ Collection <String > includes = new HashSet <>();
307
+ if (includeClassifiers != null ) {
308
+ includes .addAll (Arrays .asList (includeClassifiers ));
309
+ }
302
310
303
- for ( Artifact artifact : this . project . getAttachedArtifacts ()) {
304
- if (! includes . isEmpty () && ! includes . contains ( artifact . getClassifier ()) ) {
305
- continue ;
306
- }
311
+ Collection < String > excludes = new HashSet <>();
312
+ if (excludeClassifiers != null ) {
313
+ excludes . addAll ( Arrays . asList ( excludeClassifiers )) ;
314
+ }
307
315
308
- if (excludes .contains (artifact .getClassifier ())) {
309
- continue ;
310
- }
316
+ for (Artifact artifact : this .project .getAttachedArtifacts ()) {
317
+ if (!includes .isEmpty () && !includes .contains (artifact .getClassifier ())) {
318
+ continue ;
319
+ }
311
320
312
- processed += processArtifact (artifact ) ? 1 : 0 ;
321
+ if (excludes .contains (artifact .getClassifier ())) {
322
+ continue ;
313
323
}
324
+
325
+ getFileFromArtifact (artifact ).ifPresent (archives ::add );
326
+ }
327
+ } else {
328
+ if (verbose ) {
329
+ getLog ().info (getMessage ("ignoringAttachments" ));
314
330
} else {
315
- if (verbose ) {
316
- getLog ().info (getMessage ("ignoringAttachments" ));
317
- } else {
318
- getLog ().debug (getMessage ("ignoringAttachments" ));
319
- }
331
+ getLog ().debug (getMessage ("ignoringAttachments" ));
320
332
}
333
+ }
321
334
322
- if (archiveDirectory != null ) {
323
- String includeList = (includes != null ) ? StringUtils .join (includes , "," ) : null ;
324
- String excludeList = (excludes != null ) ? StringUtils .join (excludes , "," ) : null ;
325
-
326
- List <File > jarFiles ;
327
- try {
328
- jarFiles = FileUtils .getFiles (archiveDirectory , includeList , excludeList );
329
- } catch (IOException e ) {
330
- throw new MojoExecutionException ("Failed to scan archive directory for JARs: " + e .getMessage (), e );
331
- }
335
+ if (archiveDirectory != null ) {
336
+ String includeList = (includes != null ) ? StringUtils .join (includes , "," ) : null ;
337
+ String excludeList = (excludes != null ) ? StringUtils .join (excludes , "," ) : null ;
332
338
333
- for ( File jarFile : jarFiles ) {
334
- processArchive ( jarFile );
335
- processed ++;
336
- }
339
+ try {
340
+ archives . addAll ( FileUtils . getFiles ( archiveDirectory , includeList , excludeList ) );
341
+ } catch ( IOException e ) {
342
+ throw new MojoExecutionException ( "Failed to scan archive directory for JARs: " + e . getMessage (), e );
337
343
}
338
344
}
339
345
340
- getLog (). info ( getMessage ( "processed" , processed )) ;
346
+ return archives ;
341
347
}
342
348
343
349
/**
@@ -358,7 +364,7 @@ public final void execute() throws MojoExecutionException {
358
364
*
359
365
* @param commandLine The {@code Commandline} to get a string representation of.
360
366
* @return The string representation of {@code commandLine}.
361
- * @throws NullPointerException if {@code commandLine} is {@code null}.
367
+ * @throws NullPointerException if {@code commandLine} is {@code null}
362
368
*/
363
369
protected String getCommandlineInfo (final Commandline commandLine ) {
364
370
if (commandLine == null ) {
@@ -384,45 +390,39 @@ public String getStorepass() {
384
390
* @param artifact The artifact to check, may be <code>null</code>.
385
391
* @return <code>true</code> if the artifact looks like a ZIP file, <code>false</code> otherwise.
386
392
*/
387
- private boolean isZipFile (final Artifact artifact ) {
393
+ private static boolean isZipFile (final Artifact artifact ) {
388
394
return artifact != null && artifact .getFile () != null && JarSignerUtil .isZipFile (artifact .getFile ());
389
395
}
390
396
391
397
/**
392
- * Processes a given artifact .
398
+ * Examines an Artifact and extract the File object pointing to the Artifact jar file .
393
399
*
394
- * @param artifact The artifact to process.
395
- * @return <code>true</code> if the artifact is a JAR and was processed, <code>false</code> otherwise.
396
- * @throws NullPointerException if {@code artifact} is {@code null}.
397
- * @throws MojoExecutionException if processing {@code artifact} fails.
400
+ * @param artifact the artifact to examine
401
+ * @return An Optional containing the File, or Optional.empty() if the File is not a jar file.
402
+ * @throws NullPointerException if {@code artifact} is {@code null}
398
403
*/
399
- private boolean processArtifact (final Artifact artifact ) throws MojoExecutionException {
404
+ private Optional < File > getFileFromArtifact (final Artifact artifact ) {
400
405
if (artifact == null ) {
401
406
throw new NullPointerException ("artifact" );
402
407
}
403
408
404
- boolean processed = false ;
405
-
406
409
if (isZipFile (artifact )) {
407
- processArchive (artifact .getFile ());
408
-
409
- processed = true ;
410
- } else {
411
- if (this .verbose ) {
412
- getLog ().info (getMessage ("unsupported" , artifact ));
413
- } else if (getLog ().isDebugEnabled ()) {
414
- getLog ().debug (getMessage ("unsupported" , artifact ));
415
- }
410
+ return Optional .of (artifact .getFile ());
416
411
}
417
412
418
- return processed ;
413
+ if (this .verbose ) {
414
+ getLog ().info (getMessage ("unsupported" , artifact ));
415
+ } else if (getLog ().isDebugEnabled ()) {
416
+ getLog ().debug (getMessage ("unsupported" , artifact ));
417
+ }
418
+ return Optional .empty ();
419
419
}
420
420
421
421
/**
422
422
* Pre-processes a given archive.
423
423
*
424
424
* @param archive The archive to process, must not be <code>null</code>.
425
- * @throws MojoExecutionException If pre-processing failed.
425
+ * @throws MojoExecutionException if pre-processing failed
426
426
*/
427
427
protected void preProcessArchive (final File archive ) throws MojoExecutionException {
428
428
// Default implementation does nothing
@@ -437,14 +437,26 @@ protected void validateParameters() throws MojoExecutionException {
437
437
// Default implementation does nothing
438
438
}
439
439
440
+ /**
441
+ * Process (sign/verify) a list of archives.
442
+ *
443
+ * @param archives list of jar files to process
444
+ * @throws MojoExecutionException if an error occurs during the processing of archives
445
+ */
446
+ protected void processArchives (List <File > archives ) throws MojoExecutionException {
447
+ for (File file : archives ) {
448
+ processArchive (file );
449
+ }
450
+ }
451
+
440
452
/**
441
453
* Processes a given archive.
442
454
*
443
455
* @param archive The archive to process.
444
- * @throws NullPointerException if {@code archive} is {@code null}.
445
- * @throws MojoExecutionException if processing {@code archive} fails.
456
+ * @throws NullPointerException if {@code archive} is {@code null}
457
+ * @throws MojoExecutionException if processing {@code archive} fails
446
458
*/
447
- private void processArchive (final File archive ) throws MojoExecutionException {
459
+ protected final void processArchive (final File archive ) throws MojoExecutionException {
448
460
if (archive == null ) {
449
461
throw new NullPointerException ("archive" );
450
462
}
0 commit comments