View Javadoc
1   package org.codehaus.gmavenplus.mojo;
2   
3   import org.apache.maven.shared.model.fileset.FileSet;
4   import org.apache.maven.shared.model.fileset.util.FileSetManager;
5   
6   import java.io.File;
7   import java.util.*;
8   
9   import static java.util.Collections.singletonList;
10  
11  
12  /**
13   * This mojo provides access to the Groovy sources.
14   *
15   * @author Keegan Witt
16   */
17  public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {
18  
19      /**
20       * Main source directory name.
21       */
22      protected static final String MAIN = "main";
23  
24      /**
25       * Test source directory name.
26       */
27      protected static final String TEST = "test";
28  
29      /**
30       * Gets the set of included files from the specified source files or source directory (if sources are null).
31       *
32       * @param fromSources        The sources to get the included files from
33       * @param includeJavaSources Whether to include Java sources
34       * @return The included files from the specified sources
35       */
36      protected SortedSet<File> getFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
37          SortedSet<File> files = new TreeSet<>();
38          FileSetManager fileSetManager = new FileSetManager();
39  
40          for (FileSet fileSet : getFilesets(fromSources, includeJavaSources)) {
41              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
42                  files.add(new File(fileSet.getDirectory(), include));
43              }
44          }
45  
46          return files;
47      }
48  
49      /**
50       * Gets the set of included files from the specified source files or source directory (if sources are null).
51       *
52       * @param fromSources        The sources to get the included files from
53       * @param includeJavaSources Whether to include Java sources
54       * @return The included files from the specified sources
55       */
56      protected SortedSet<File> getTestFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
57          SortedSet<File> files = new TreeSet<>();
58          FileSetManager fileSetManager = new FileSetManager();
59  
60          for (FileSet fileSet : getTestFilesets(fromSources, includeJavaSources)) {
61              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
62                  files.add(new File(fileSet.getDirectory(), include));
63              }
64          }
65  
66          return files;
67      }
68  
69      /**
70       * Gets the set of included filesets from the specified source files or source directory (if sources are null).
71       *
72       * @param fromSources        The sources to get the included files from
73       * @param includeJavaSources Whether to include Java sources
74       * @return The included filesets from the specified sources
75       */
76      protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
77          FileSet[] result;
78          FileSet[] groovyFileSets;
79  
80          if (fromSources != null) {
81              groovyFileSets = fromSources;
82          } else {
83              FileSet groovyFileSet = new FileSet();
84              String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
85              groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
86              groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
87              groovyFileSets = new FileSet[]{groovyFileSet};
88          }
89  
90          if (includeJavaSources) {
91              List<FileSet> javaFileSets = new ArrayList<>();
92              for (String sourceRoot : project.getCompileSourceRoots()) {
93                  FileSet javaFileSet = new FileSet();
94                  javaFileSet.setDirectory(sourceRoot);
95                  javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
96                  javaFileSets.add(javaFileSet);
97              }
98              FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
99              result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
100             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
101         } else {
102             result = groovyFileSets;
103         }
104 
105         return result;
106     }
107 
108     /**
109      * Gets the set of included filesets from the specified source files or source directory (if sources are null).
110      *
111      * @param fromSources        The sources to get the included files from
112      * @param includeJavaSources Whether to include Java sources
113      * @return The included filesets from the specified sources
114      */
115     protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
116         FileSet[] result;
117         FileSet[] groovyFileSets;
118 
119         if (fromSources != null) {
120             groovyFileSets = fromSources;
121         } else {
122             FileSet groovyFileSet = new FileSet();
123             String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
124             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
125             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
126             groovyFileSets = new FileSet[]{groovyFileSet};
127         }
128 
129         if (includeJavaSources) {
130             List<FileSet> javaFileSets = new ArrayList<>();
131             for (String sourceRoot : project.getTestCompileSourceRoots()) {
132                 FileSet javaFileSet = new FileSet();
133                 javaFileSet.setDirectory(sourceRoot);
134                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
135                 javaFileSets.add(javaFileSet);
136             }
137             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
138             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
139             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
140         } else {
141             result = groovyFileSets;
142         }
143 
144         return result;
145     }
146 }