View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This mojo provides access to the Groovy sources (including stubs).
38   *
39   * @author Keegan Witt
40   */
41  public abstract class AbstractGroovyStubSourcesMojo extends AbstractGroovySourcesMojo {
42  
43      /**
44       * Removes the source roots from the project, using reflection to avoid breaking changes in Maven 4.
45       *
46       * @param project the Maven project
47       * @param scopeToRemove the scope to remove (main or test)
48       * @param sourceDirectory the source directory to remove
49       * @throws ClassNotFoundException when a class needed cannot be found
50       * @throws NoSuchFieldException when a field needed cannot be found
51       * @throws NoSuchMethodException when a method needed cannot be found
52       * @throws IllegalAccessException when a method needed cannot be accessed
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       * Gets the set of stub files in specified directory.
78       *
79       * @param outputDirectory the directory to write stubs to
80       * @return The set of stub files in specified directory
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  }