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 public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {
34
35
36
37
38 protected static final String MAIN = "main";
39
40
41
42
43 protected static final String TEST = "test";
44
45
46
47
48
49
50
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
67
68
69
70
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
87
88
89
90
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
126
127
128
129
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 }