1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34 public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {
35
36
37
38
39 protected static final String MAIN = "main";
40
41
42
43
44 protected static final String TEST = "test";
45
46
47
48
49
50
51
52
53 protected SortedSet<File> getFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
54 SortedSet<File> files = new TreeSet<>();
55 FileSetManager fileSetManager = new FileSetManager();
56
57 for (FileSet fileSet : getFilesets(fromSources, includeJavaSources)) {
58 for (String include : fileSetManager.getIncludedFiles(fileSet)) {
59 files.add(new File(fileSet.getDirectory(), include));
60 }
61 }
62
63 return files;
64 }
65
66
67
68
69
70
71
72
73 protected SortedSet<File> getTestFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
74 SortedSet<File> files = new TreeSet<>();
75 FileSetManager fileSetManager = new FileSetManager();
76
77 for (FileSet fileSet : getTestFilesets(fromSources, includeJavaSources)) {
78 for (String include : fileSetManager.getIncludedFiles(fileSet)) {
79 files.add(new File(fileSet.getDirectory(), include));
80 }
81 }
82
83 return files;
84 }
85
86
87
88
89
90
91
92
93 protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
94 FileSet[] result;
95 FileSet[] groovyFileSets;
96
97 if (fromSources != null) {
98 groovyFileSets = fromSources;
99 } else {
100 FileSet groovyFileSet = new FileSet();
101 String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
102 groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
103 groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
104 groovyFileSets = new FileSet[]{groovyFileSet};
105 }
106
107 if (includeJavaSources) {
108 List<FileSet> javaFileSets = new ArrayList<>();
109 for (String sourceRoot : project.getCompileSourceRoots()) {
110 FileSet javaFileSet = new FileSet();
111 javaFileSet.setDirectory(sourceRoot);
112 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
113 javaFileSets.add(javaFileSet);
114 }
115 FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
116 result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
117 System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
118 } else {
119 result = groovyFileSets;
120 }
121
122 return result;
123 }
124
125
126
127
128
129
130
131
132 protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
133 FileSet[] result;
134 FileSet[] groovyFileSets;
135
136 if (fromSources != null) {
137 groovyFileSets = fromSources;
138 } else {
139 FileSet groovyFileSet = new FileSet();
140 String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
141 groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
142 groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
143 groovyFileSets = new FileSet[]{groovyFileSet};
144 }
145
146 if (includeJavaSources) {
147 List<FileSet> javaFileSets = new ArrayList<>();
148 for (String sourceRoot : project.getTestCompileSourceRoots()) {
149 FileSet javaFileSet = new FileSet();
150 javaFileSet.setDirectory(sourceRoot);
151 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
152 javaFileSets.add(javaFileSet);
153 }
154 FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
155 result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
156 System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
157 } else {
158 result = groovyFileSets;
159 }
160
161 return result;
162 }
163 }