2929import java .util .List ;
3030import java .util .Locale ;
3131import java .util .ResourceBundle ;
32+ import java .util .concurrent .atomic .AtomicReference ;
3233
3334import org .apache .maven .doxia .siterenderer .Renderer ;
3435import org .apache .maven .model .ReportPlugin ;
3839import org .apache .maven .project .MavenProject ;
3940import org .apache .maven .reporting .AbstractMavenReport ;
4041import org .apache .maven .reporting .MavenReportException ;
42+ import org .apache .maven .shared .utils .io .FileUtils ;
4143import org .codehaus .mojo .taglist .beans .FileReport ;
4244import org .codehaus .mojo .taglist .beans .TagReport ;
4345import org .codehaus .mojo .taglist .output .TagListXMLComment ;
4951import org .codehaus .mojo .taglist .tags .InvalidTagException ;
5052import org .codehaus .mojo .taglist .tags .TagClass ;
5153import org .codehaus .mojo .taglist .tags .TagFactory ;
52- import org .codehaus .plexus .util .FileUtils ;
5354import org .codehaus .plexus .util .IOUtil ;
5455import org .codehaus .plexus .util .PathTool ;
5556import org .codehaus .plexus .util .StringUtils ;
@@ -192,6 +193,8 @@ public class TagListReport
192193
193194 private String [] tags ;
194195
196+ private AtomicReference <List > sourceDirs = new AtomicReference <>();
197+
195198 /**
196199 * {@inheritDoc}
197200 *
@@ -424,7 +427,7 @@ private String getRelativePath( File location )
424427 */
425428 public boolean canGenerateReport ()
426429 {
427- boolean canGenerate = !constructSourceDirs ().isEmpty ();
430+ boolean canGenerate = !getSourceDirs ().isEmpty ();
428431 if ( aggregate && !getProject ().isExecutionRoot () )
429432 {
430433 canGenerate = false ;
@@ -434,11 +437,11 @@ public boolean canGenerateReport()
434437
435438 /**
436439 * Removes empty dirs from the list.
437- *
440+ *
438441 * @param sourceDirectories the original list of directories.
439442 * @return a new list containing only non empty dirs.
440443 */
441- private List pruneSourceDirs ( List sourceDirectories )
444+ private List pruneSourceDirs ( List sourceDirectories ) throws IOException
442445 {
443446 List pruned = new ArrayList ( sourceDirectories .size () );
444447 for ( Iterator i = sourceDirectories .iterator (); i .hasNext (); )
@@ -454,45 +457,46 @@ private List pruneSourceDirs( List sourceDirectories )
454457
455458 /**
456459 * Checks whether the given directory contains source files.
457- *
460+ *
458461 * @param dir the source directory.
459- * @return true if the folder or one of its subfolders contains at least 1 source file that matches includes/excludes.
462+ * @return true if the folder or one of its subfolders contains at least 1 source file that matches
463+ * includes/excludes.
460464 */
461- private boolean hasSources ( File dir )
465+ private boolean hasSources ( File dir ) throws IOException
462466 {
463- boolean found = false ;
464467 if ( dir .exists () && dir .isDirectory () )
465468 {
466- try {
467- if ( ! FileUtils .getFiles ( dir , getIncludesCommaSeparated (), getExcludesCommaSeparated () ).isEmpty () ) {
468- found = true ;
469- }
470- } catch (IOException e ) {
471- // should never get here
472- getLog ().error ("Error whilst trying to scan the directory " + dir .getAbsolutePath (), e );
469+ if ( !FileUtils .getFiles ( dir , getIncludesCommaSeparated (), getExcludesCommaSeparated () ).isEmpty () )
470+ {
471+ return true ;
473472 }
473+
474474 File [] files = dir .listFiles ();
475- if ( files != null ) {
476- for ( int i = 0 ; i < files .length && !found ; i ++ ) {
475+ if ( files != null )
476+ {
477+ for ( int i = 0 ; i < files .length ; i ++ )
478+ {
477479 File currentFile = files [i ];
478- if ( currentFile .isDirectory () ) {
480+ if ( currentFile .isDirectory () )
481+ {
479482 boolean hasSources = hasSources ( currentFile );
480- if ( hasSources ) {
481- found = true ;
483+ if ( hasSources )
484+ {
485+ return true ;
482486 }
483487 }
484488 }
485489 }
486490 }
487- return found ;
491+ return false ;
488492 }
489493
490494 /**
491495 * Construct the list of source directories to analyze.
492- *
496+ *
493497 * @return the list of dirs.
494498 */
495- public List constructSourceDirs ()
499+ private List constructSourceDirs ()
496500 {
497501 List dirs = new ArrayList ( getProject ().getCompileSourceRoots () );
498502 if ( !skipTestSources )
@@ -506,7 +510,6 @@ public List constructSourceDirs()
506510 {
507511 MavenProject reactorProject = (MavenProject ) i .next ();
508512
509- // TODO should this be more like ! "pom".equals(...)
510513 if ( "java" .equals ( reactorProject .getArtifact ().getArtifactHandler ().getLanguage () ) )
511514 {
512515 dirs .addAll ( reactorProject .getCompileSourceRoots () );
@@ -518,18 +521,46 @@ public List constructSourceDirs()
518521 }
519522 }
520523
521- dirs = pruneSourceDirs ( dirs );
524+ /*
525+ * This try-catch is needed due to a missing declared exception in the
526+ * 'canGenerateReport()' method. For this reason, neither the 'canGenerateReport()'
527+ * nor the 'constructSourceDirs()' can throw exceptions.
528+ * The exception itself is caused by a declaration from the FileUtils, but never used
529+ * there. The FileUtils.getFiles() should be replaced by an NIO filter at some point.
530+ */
531+ try
532+ {
533+ dirs = pruneSourceDirs ( dirs );
534+ }
535+ catch ( IOException javaIoIOException )
536+ {
537+ getLog ().warn ( "Unable to prune source dirs." , javaIoIOException );
538+ }
539+
522540 return dirs ;
523541 }
524542
543+ protected List getSourceDirs ()
544+ {
545+ if ( sourceDirs .get () == null )
546+ {
547+ sourceDirs .compareAndSet ( null , constructSourceDirs () );
548+ }
549+
550+ return sourceDirs .get ();
551+ }
552+
525553 /**
526554 * Get the files to include, as a comma separated list of patterns.
527555 */
528556 String getIncludesCommaSeparated ()
529557 {
530- if ( includes != null ) {
531- return String .join ("," , includes );
532- } else {
558+ if ( includes != null )
559+ {
560+ return String .join ( "," , includes );
561+ }
562+ else
563+ {
533564 return "" ;
534565 }
535566 }
0 commit comments