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   * @since 1.0-beta-3
41   */
42  public abstract class AbstractGroovyStubSourcesMojo extends AbstractGroovySourcesMojo {
43  
44      /**
45       * Removes the source roots from the project, using reflection to avoid breaking changes in Maven 4.
46       *
47       * @param project the Maven project
48       * @param scopeToRemove the scope to remove (main or test)
49       * @param sourceDirectory the source directory to remove
50       * @throws ClassNotFoundException when a class needed cannot be found
51       * @throws NoSuchFieldException when a field needed cannot be found
52       * @throws NoSuchMethodException when a method needed cannot be found
53       * @throws IllegalAccessException when a method needed cannot be accessed
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       * Gets the set of stub files in specified directory.
79       *
80       * @param outputDirectory the directory to write stubs to
81       * @return The set of stub files in specified directory
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  }