View Javadoc
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   * This mojo provides access to the Groovy sources (including stubs).
22   *
23   * @author Keegan Witt
24   */
25  public abstract class AbstractGroovyStubSourcesMojo extends AbstractGroovySourcesMojo {
26  
27      /**
28       * Removes the source roots from the project, using reflection to avoid breaking changes in Maven 4.
29       *
30       * @param project the Maven project
31       * @param scopeToRemove the scope to remove (main or test)
32       * @param sourceDirectory the source directory to remove
33       * @throws ClassNotFoundException when a class needed cannot be found
34       * @throws NoSuchFieldException when a field needed cannot be found
35       * @throws NoSuchMethodException when a method needed cannot be found
36       * @throws IllegalAccessException when a method needed cannot be accessed
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       * Gets the set of stub files in specified directory.
62       *
63       * @param outputDirectory the directory to write stubs to
64       * @return The set of stub files in specified directory
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  }