55import fr .inria .gforge .spoon .util .LogWrapper ;
66import org .apache .maven .artifact .DependencyResolutionRequiredException ;
77import org .apache .maven .project .MavenProject ;
8+ import org .codehaus .plexus .util .DirectoryScanner ;
89
910import java .io .File ;
1011import java .util .ArrayList ;
1516abstract class AbstractSpoonConfigurationBuilder
1617 implements SpoonConfigurationBuilder {
1718
19+ private static final String [] EMPTY_STRING_ARRAY = {};
20+ private static final String [] DEFAULT_INCLUDES = {"**/**" };
21+
1822 protected final List <String > parameters = new LinkedList <String >();
1923 protected final SpoonMojoGenerate spoon ;
2024 protected final ReportBuilder reportBuilder ;
25+ private DirectoryScanner directoryScanner = null ;
2126
2227 protected AbstractSpoonConfigurationBuilder (SpoonMojoGenerate spoon ,
2328 ReportBuilder reportBuilder ) {
@@ -30,43 +35,101 @@ protected AbstractSpoonConfigurationBuilder(SpoonMojoGenerate spoon,
3035 parameters .add ("--level" );
3136 parameters .add ("INFO" );
3237 }
33-
3438 }
3539
3640 @ Override
3741 public SpoonConfigurationBuilder addInputFolder () throws SpoonMavenPluginException {
38- final List <File > srcDir = new ArrayList <>();
42+ final List <File > inputs = new ArrayList <>();
3943 if (spoon .isIncludeSrcDirectories ()) {
40- srcDir .add (new File (spoon .getProject ().getBuild ().getSourceDirectory ()));
41- if (!spoon .getSkipGeneratedSources ()) {
42- for (String s : spoon .getProject ().getCompileSourceRoots ()) {
43- srcDir .add (new File (s ));
44+ final File sourceDirectory = new File (spoon .getProject ().getBuild ().getSourceDirectory ());
45+ if (sourceDirectory .exists ()) {
46+ for (final File input : filterDirectory (sourceDirectory )) {
47+ inputs .add (input );
48+ }
49+ }
50+
51+ if (!spoon .getSkipGeneratedSources ()) {
52+ for (final String compileSourceRootStr : spoon .getProject ().getCompileSourceRoots ()) {
53+ final File compileSourceRoot = new File (compileSourceRootStr );
54+ if (compileSourceRoot .exists ()) {
55+ for (final File input : filterDirectory (compileSourceRoot )) {
56+ inputs .add (input );
57+ }
58+ }
4459 }
4560 }
4661 }
47- if (spoon .isIncludeTestDirectories ()) {
48- srcDir .add (new File (spoon .getProject ().getBuild ().getTestSourceDirectory ()));
49- if (!spoon .getSkipGeneratedSources ()) {
50- for (String s : spoon .getProject ().getTestCompileSourceRoots ()) {
51- srcDir .add (new File (s ));
62+
63+ if (spoon .isIncludeTestDirectories ()) {
64+ final File testSourceDirectory = new File (spoon .getProject ().getBuild ().getTestSourceDirectory ());
65+ if (testSourceDirectory .exists ()) {
66+ for (final File input : filterDirectory (testSourceDirectory )) {
67+ inputs .add (input );
68+ }
69+ }
70+
71+ if (!spoon .getSkipGeneratedSources ()) {
72+ for (final String testCompileSourceRootStr : spoon .getProject ().getTestCompileSourceRoots ()) {
73+ final File testCompileSourceRoot = new File (testCompileSourceRootStr );
74+ if (testCompileSourceRoot .exists ()) {
75+ for (final File input : filterDirectory (testCompileSourceRoot )) {
76+ inputs .add (input );
77+ }
78+ }
5279 }
5380 }
5481 }
5582
56-
57- srcDir .removeIf (file -> !file .exists ());
58-
59- if (srcDir .isEmpty ()) {
60- throw new SpoonMavenPluginException (String .format ("No source directory for %s project." , spoon .getProject ().getName ()));
83+ if (inputs .isEmpty ()) {
84+ throw new SpoonMavenPluginException (String .format ("No input sources for %s project." , spoon .getProject ().getName ()));
6185 }
62- String inputs = srcDir .stream ().map (File ::getAbsolutePath ).collect (Collectors .joining (File .pathSeparator ));
86+
87+ final String inputsString = inputs .stream ().map (File ::getAbsolutePath ).collect (Collectors .joining (File .pathSeparator ));
6388
6489 parameters .add ("-i" );
65- parameters .add (inputs );
66- reportBuilder .setInput (inputs );
90+ parameters .add (inputsString );
91+ reportBuilder .setInput (inputsString );
6792 return this ;
6893 }
6994
95+ private File [] filterDirectory (final File dir ) {
96+ if (spoon .getIncludes ().isEmpty () && spoon .getExcludes ().isEmpty ()) {
97+ return new File [] { dir };
98+ }
99+
100+ if (directoryScanner == null ) {
101+ directoryScanner = setupScanner ();
102+ }
103+
104+ directoryScanner .setBasedir (dir );
105+ directoryScanner .scan ();
106+
107+ final String [] includedFiles = directoryScanner .getIncludedFiles ();
108+ final File [] filtered = new File [includedFiles .length ];
109+ for (int i = 0 ; i < includedFiles .length ; i ++) {
110+ filtered [i ] = new File (dir , includedFiles [i ]);
111+ }
112+ return filtered ;
113+ }
114+
115+ private DirectoryScanner setupScanner () {
116+ final DirectoryScanner scanner = new DirectoryScanner ();
117+
118+ if (!spoon .getIncludes ().isEmpty ()) {
119+ scanner .setIncludes (spoon .getIncludes ().toArray (EMPTY_STRING_ARRAY ));
120+ } else {
121+ scanner .setIncludes (DEFAULT_INCLUDES );
122+ }
123+
124+
125+ if (!spoon .getExcludes ().isEmpty ()) {
126+ String [] excludes = spoon .getExcludes ().toArray (EMPTY_STRING_ARRAY );
127+ scanner .setExcludes (excludes );
128+ }
129+
130+ return scanner ;
131+ }
132+
70133 @ Override
71134 public SpoonConfigurationBuilder addOutputFolder () {
72135 // Create output folder if it doesn't exist.
0 commit comments