Skip to content

Commit

Permalink
feat: use includes and excludes to specify files to scan
Browse files Browse the repository at this point in the history
This is to implement #12
  • Loading branch information
juliangp authored and bmarwell committed Jan 21, 2022
1 parent d02d749 commit dac102d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
14 changes: 13 additions & 1 deletion src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public class FileAnalyser
*/
private Collection sourceDirs;

/**
* The files to include, as a comma separated list of patterns.
*/
private String includes;

/**
* The files top exclude, as a comma separated list of patterns.
*/
private String excludes;

/**
* Log for debug output.
*/
Expand Down Expand Up @@ -124,6 +134,8 @@ public FileAnalyser( TagListReport report, List<TagClass> tagClasses )
locale = report.getLocale();
noCommentString = report.getBundle().getString( "report.taglist.nocomment" );
this.tagClasses = tagClasses;
this.includes = report.getIncludesCommaSeparated();
this.excludes = report.getExcludesCommaSeparated();
}

/**
Expand Down Expand Up @@ -171,7 +183,7 @@ private List findFilesToScan()
{
for ( Iterator iter = sourceDirs.iterator(); iter.hasNext(); )
{
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), "**/*.java", null ) );
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), includes, excludes ) );
}
}
catch ( IOException e )
Expand Down
74 changes: 60 additions & 14 deletions src/main/java/org/codehaus/mojo/taglist/TagListReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.codehaus.mojo.taglist.tags.InvalidTagException;
import org.codehaus.mojo.taglist.tags.TagClass;
import org.codehaus.mojo.taglist.tags.TagFactory;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.PathTool;
import org.codehaus.plexus.util.StringUtils;
Expand All @@ -72,6 +74,22 @@ public class TagListReport
/** Default locale used if the source file locale is null. */
private static final String DEFAULT_LOCALE = "en";

/**
* List of files to include. Specified as fileset patterns which are relative to the source directory.
*
* @since 3.0.0
*/
@Parameter( defaultValue = "**/*.java" )
private String[] includes;

/**
* List of files to exclude. Specified as fileset patterns which are relative to the source directory.
*
* @since 3.0.0
*/
@Parameter( defaultValue = "" )
private String[] excludes;

/**
* Specifies the directory where the xml output will be generated.
*
Expand Down Expand Up @@ -435,30 +453,33 @@ private List pruneSourceDirs( List sourceDirectories )
}

/**
* Checks whether the given directory contains Java files.
* Checks whether the given directory contains source files.
*
* @param dir the source directory.
* @return true if the folder or one of its subfolders contains at least 1 Java file.
* @return true if the folder or one of its subfolders contains at least 1 source file that matches includes/excludes.
*/
private boolean hasSources( File dir )
{
boolean found = false;
if ( dir.exists() && dir.isDirectory() )
{
File[] files = dir.listFiles();
for ( int i = 0; i < files.length && !found; i++ )
{
File currentFile = files[i];
if ( currentFile.isFile() && currentFile.getName().endsWith( ".java" ) )
{
try {
if ( ! FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() ) {
found = true;
}
else if ( currentFile.isDirectory() )
{
boolean hasSources = hasSources( currentFile );
if ( hasSources )
{
found = true;
} catch (IOException e) {
// should never get here
getLog().error("Error whilst trying to scan the directory " + dir.getAbsolutePath(), e);
}
File[] files = dir.listFiles();
if ( files != null ) {
for ( int i = 0; i < files.length && !found; i++ ) {
File currentFile = files[i];
if ( currentFile.isDirectory() ) {
boolean hasSources = hasSources( currentFile );
if ( hasSources ) {
found = true;
}
}
}
}
Expand All @@ -485,6 +506,7 @@ public List constructSourceDirs()
{
MavenProject reactorProject = (MavenProject) i.next();

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

/**
* Get the files to include, as a comma separated list of patterns.
*/
String getIncludesCommaSeparated()
{
if ( includes != null ) {
return String.join(",", includes);
} else {
return "";
}
}

/**
* Get the files to exclude, as a comma separated list of patterns.
*/
String getExcludesCommaSeparated()
{
if ( excludes != null ) {
return String.join(",", excludes);
} else {
return "";
}
}

/**
* Returns the Locale of the source files.
*
* @return The Locale of the source files.
Expand Down

0 comments on commit dac102d

Please # to comment.