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