View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.mojo;
18  
19  import org.apache.maven.shared.model.fileset.FileSet;
20  import org.apache.maven.shared.model.fileset.util.FileSetManager;
21  
22  import java.io.File;
23  import java.util.*;
24  
25  import static java.util.Collections.singletonList;
26  
27  
28  /**
29   * This mojo provides access to the Groovy sources.
30   *
31   * @author Keegan Witt
32   */
33  public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {
34  
35      /**
36       * Main source directory name.
37       */
38      protected static final String MAIN = "main";
39  
40      /**
41       * Test source directory name.
42       */
43      protected static final String TEST = "test";
44  
45      /**
46       * Gets the set of included files from the specified source files or source directory (if sources are null).
47       *
48       * @param fromSources        The sources to get the included files from
49       * @param includeJavaSources Whether to include Java sources
50       * @return The included files from the specified sources
51       */
52      protected SortedSet<File> getFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
53          SortedSet<File> files = new TreeSet<>();
54          FileSetManager fileSetManager = new FileSetManager();
55  
56          for (FileSet fileSet : getFilesets(fromSources, includeJavaSources)) {
57              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
58                  files.add(new File(fileSet.getDirectory(), include));
59              }
60          }
61  
62          return files;
63      }
64  
65      /**
66       * Gets the set of included files from the specified source files or source directory (if sources are null).
67       *
68       * @param fromSources        The sources to get the included files from
69       * @param includeJavaSources Whether to include Java sources
70       * @return The included files from the specified sources
71       */
72      protected SortedSet<File> getTestFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
73          SortedSet<File> files = new TreeSet<>();
74          FileSetManager fileSetManager = new FileSetManager();
75  
76          for (FileSet fileSet : getTestFilesets(fromSources, includeJavaSources)) {
77              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
78                  files.add(new File(fileSet.getDirectory(), include));
79              }
80          }
81  
82          return files;
83      }
84  
85      /**
86       * Gets the set of included filesets from the specified source files or source directory (if sources are null).
87       *
88       * @param fromSources        The sources to get the included files from
89       * @param includeJavaSources Whether to include Java sources
90       * @return The included filesets from the specified sources
91       */
92      protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
93          FileSet[] result;
94          FileSet[] groovyFileSets;
95  
96          if (fromSources != null) {
97              groovyFileSets = fromSources;
98          } else {
99              FileSet groovyFileSet = new FileSet();
100             String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
101             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
102             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
103             groovyFileSets = new FileSet[]{groovyFileSet};
104         }
105 
106         if (includeJavaSources) {
107             List<FileSet> javaFileSets = new ArrayList<>();
108             for (String sourceRoot : project.getCompileSourceRoots()) {
109                 FileSet javaFileSet = new FileSet();
110                 javaFileSet.setDirectory(sourceRoot);
111                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
112                 javaFileSets.add(javaFileSet);
113             }
114             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
115             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
116             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
117         } else {
118             result = groovyFileSets;
119         }
120 
121         return result;
122     }
123 
124     /**
125      * Gets the set of included filesets from the specified source files or source directory (if sources are null).
126      *
127      * @param fromSources        The sources to get the included files from
128      * @param includeJavaSources Whether to include Java sources
129      * @return The included filesets from the specified sources
130      */
131     protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
132         FileSet[] result;
133         FileSet[] groovyFileSets;
134 
135         if (fromSources != null) {
136             groovyFileSets = fromSources;
137         } else {
138             FileSet groovyFileSet = new FileSet();
139             String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
140             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
141             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
142             groovyFileSets = new FileSet[]{groovyFileSet};
143         }
144 
145         if (includeJavaSources) {
146             List<FileSet> javaFileSets = new ArrayList<>();
147             for (String sourceRoot : project.getTestCompileSourceRoots()) {
148                 FileSet javaFileSet = new FileSet();
149                 javaFileSet.setDirectory(sourceRoot);
150                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
151                 javaFileSets.add(javaFileSet);
152             }
153             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
154             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
155             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
156         } else {
157             result = groovyFileSets;
158         }
159 
160         return result;
161     }
162 }