Skip to content

Commit dac102d

Browse files
juliangpbmarwell
authored andcommitted
feat: use includes and excludes to specify files to scan
This is to implement #12
1 parent d02d749 commit dac102d

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public class FileAnalyser
8383
*/
8484
private Collection sourceDirs;
8585

86+
/**
87+
* The files to include, as a comma separated list of patterns.
88+
*/
89+
private String includes;
90+
91+
/**
92+
* The files top exclude, as a comma separated list of patterns.
93+
*/
94+
private String excludes;
95+
8696
/**
8797
* Log for debug output.
8898
*/
@@ -124,6 +134,8 @@ public FileAnalyser( TagListReport report, List<TagClass> tagClasses )
124134
locale = report.getLocale();
125135
noCommentString = report.getBundle().getString( "report.taglist.nocomment" );
126136
this.tagClasses = tagClasses;
137+
this.includes = report.getIncludesCommaSeparated();
138+
this.excludes = report.getExcludesCommaSeparated();
127139
}
128140

129141
/**
@@ -171,7 +183,7 @@ private List findFilesToScan()
171183
{
172184
for ( Iterator iter = sourceDirs.iterator(); iter.hasNext(); )
173185
{
174-
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), "**/*.java", null ) );
186+
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), includes, excludes ) );
175187
}
176188
}
177189
catch ( IOException e )

src/main/java/org/codehaus/mojo/taglist/TagListReport.java

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.File;
2323
import java.io.FileOutputStream;
24+
import java.io.IOException;
2425
import java.io.OutputStreamWriter;
2526
import java.util.ArrayList;
2627
import java.util.Collection;
@@ -48,6 +49,7 @@
4849
import org.codehaus.mojo.taglist.tags.InvalidTagException;
4950
import org.codehaus.mojo.taglist.tags.TagClass;
5051
import org.codehaus.mojo.taglist.tags.TagFactory;
52+
import org.codehaus.plexus.util.FileUtils;
5153
import org.codehaus.plexus.util.IOUtil;
5254
import org.codehaus.plexus.util.PathTool;
5355
import org.codehaus.plexus.util.StringUtils;
@@ -72,6 +74,22 @@ public class TagListReport
7274
/** Default locale used if the source file locale is null. */
7375
private static final String DEFAULT_LOCALE = "en";
7476

77+
/**
78+
* List of files to include. Specified as fileset patterns which are relative to the source directory.
79+
*
80+
* @since 3.0.0
81+
*/
82+
@Parameter( defaultValue = "**/*.java" )
83+
private String[] includes;
84+
85+
/**
86+
* List of files to exclude. Specified as fileset patterns which are relative to the source directory.
87+
*
88+
* @since 3.0.0
89+
*/
90+
@Parameter( defaultValue = "" )
91+
private String[] excludes;
92+
7593
/**
7694
* Specifies the directory where the xml output will be generated.
7795
*
@@ -435,30 +453,33 @@ private List pruneSourceDirs( List sourceDirectories )
435453
}
436454

437455
/**
438-
* Checks whether the given directory contains Java files.
456+
* Checks whether the given directory contains source files.
439457
*
440458
* @param dir the source directory.
441-
* @return true if the folder or one of its subfolders contains at least 1 Java file.
459+
* @return true if the folder or one of its subfolders contains at least 1 source file that matches includes/excludes.
442460
*/
443461
private boolean hasSources( File dir )
444462
{
445463
boolean found = false;
446464
if ( dir.exists() && dir.isDirectory() )
447465
{
448-
File[] files = dir.listFiles();
449-
for ( int i = 0; i < files.length && !found; i++ )
450-
{
451-
File currentFile = files[i];
452-
if ( currentFile.isFile() && currentFile.getName().endsWith( ".java" ) )
453-
{
466+
try {
467+
if ( ! FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() ) {
454468
found = true;
455469
}
456-
else if ( currentFile.isDirectory() )
457-
{
458-
boolean hasSources = hasSources( currentFile );
459-
if ( hasSources )
460-
{
461-
found = true;
470+
} catch (IOException e) {
471+
// should never get here
472+
getLog().error("Error whilst trying to scan the directory " + dir.getAbsolutePath(), e);
473+
}
474+
File[] files = dir.listFiles();
475+
if ( files != null ) {
476+
for ( int i = 0; i < files.length && !found; i++ ) {
477+
File currentFile = files[i];
478+
if ( currentFile.isDirectory() ) {
479+
boolean hasSources = hasSources( currentFile );
480+
if ( hasSources ) {
481+
found = true;
482+
}
462483
}
463484
}
464485
}
@@ -485,6 +506,7 @@ public List constructSourceDirs()
485506
{
486507
MavenProject reactorProject = (MavenProject) i.next();
487508

509+
// TODO should this be more like ! "pom".equals(...)
488510
if ( "java".equals( reactorProject.getArtifact().getArtifactHandler().getLanguage() ) )
489511
{
490512
dirs.addAll( reactorProject.getCompileSourceRoots() );
@@ -501,6 +523,30 @@ public List constructSourceDirs()
501523
}
502524

503525
/**
526+
* Get the files to include, as a comma separated list of patterns.
527+
*/
528+
String getIncludesCommaSeparated()
529+
{
530+
if ( includes != null ) {
531+
return String.join(",", includes);
532+
} else {
533+
return "";
534+
}
535+
}
536+
537+
/**
538+
* Get the files to exclude, as a comma separated list of patterns.
539+
*/
540+
String getExcludesCommaSeparated()
541+
{
542+
if ( excludes != null ) {
543+
return String.join(",", excludes);
544+
} else {
545+
return "";
546+
}
547+
}
548+
549+
/**
504550
* Returns the Locale of the source files.
505551
*
506552
* @return The Locale of the source files.

0 commit comments

Comments
 (0)