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