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.project.MavenProject;
20 import org.apache.maven.shared.model.fileset.FileSet;
21 import org.apache.maven.shared.model.fileset.util.FileSetManager;
22
23 import java.io.File;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.nio.file.Path;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.Objects;
31 import java.util.Set;
32
33 import static java.util.Collections.singletonList;
34
35
36
37
38
39
40
41
42 public abstract class AbstractGroovyStubSourcesMojo extends AbstractGroovySourcesMojo {
43
44
45
46
47
48
49
50
51
52
53
54
55 protected static void removeSourceRoot(MavenProject project, String scopeToRemove, File sourceDirectory)
56 throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException {
57 Class<?> sourceRoot = project.getClass().getClassLoader().loadClass("org.apache.maven.api.SourceRoot");
58 Path path = project.getBasedir().toPath().resolve(sourceDirectory.getAbsolutePath()).normalize();
59 Field field = project.getClass().getDeclaredField("sources");
60 field.setAccessible(true);
61 Method scope = sourceRoot.getMethod("scope");
62 Method language = sourceRoot.getMethod("language");
63 Method directory = sourceRoot.getMethod("directory");
64 Method id = project.getClass().getClassLoader().loadClass("org.apache.maven.api.ExtensibleEnum").getMethod("id");
65 Collection<?> sources = (Collection) field.get(project);
66 sources.removeIf(source -> {
67 try {
68 return Objects.equals(id.invoke(scope.invoke(source)), scopeToRemove)
69 && Objects.equals(id.invoke(language.invoke(source)), "java")
70 && Objects.equals(directory.invoke(source), path);
71 } catch (IllegalAccessException | InvocationTargetException ex) {
72 throw new RuntimeException(ex);
73 }
74 });
75 }
76
77
78
79
80
81
82
83 protected Set<File> getStubs(File outputDirectory) {
84 Set<File> files = new HashSet<>();
85 FileSetManager fileSetManager = new FileSetManager();
86
87 FileSet fileSet = new FileSet();
88 fileSet.setDirectory(outputDirectory.getAbsolutePath());
89 fileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
90 for (String file : fileSetManager.getIncludedFiles(fileSet)) {
91 files.add(new File(outputDirectory, file));
92 }
93
94 return files;
95 }
96
97 }