AbstractGroovySourcesMojo.java

  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. package org.codehaus.gmavenplus.mojo;

  17. import org.apache.maven.shared.model.fileset.FileSet;
  18. import org.apache.maven.shared.model.fileset.util.FileSetManager;

  19. import java.io.File;
  20. import java.util.*;

  21. import static java.util.Collections.singletonList;


  22. /**
  23.  * This mojo provides access to the Groovy sources.
  24.  *
  25.  * @author Keegan Witt
  26.  * @since 1.0-beta-2
  27.  */
  28. public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {

  29.     /**
  30.      * Main source directory name.
  31.      */
  32.     protected static final String MAIN = "main";

  33.     /**
  34.      * Test source directory name.
  35.      */
  36.     protected static final String TEST = "test";

  37.     /**
  38.      * Gets the set of included files from the specified source files or source directory (if sources are null).
  39.      *
  40.      * @param fromSources        The sources to get the included files from
  41.      * @param includeJavaSources Whether to include Java sources
  42.      * @return The included files from the specified sources
  43.      */
  44.     protected SortedSet<File> getFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
  45.         SortedSet<File> files = new TreeSet<>();
  46.         FileSetManager fileSetManager = new FileSetManager();

  47.         for (FileSet fileSet : getFilesets(fromSources, includeJavaSources)) {
  48.             for (String include : fileSetManager.getIncludedFiles(fileSet)) {
  49.                 files.add(new File(fileSet.getDirectory(), include));
  50.             }
  51.         }

  52.         return files;
  53.     }

  54.     /**
  55.      * Gets the set of included files from the specified source files or source directory (if sources are null).
  56.      *
  57.      * @param fromSources        The sources to get the included files from
  58.      * @param includeJavaSources Whether to include Java sources
  59.      * @return The included files from the specified sources
  60.      */
  61.     protected SortedSet<File> getTestFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
  62.         SortedSet<File> files = new TreeSet<>();
  63.         FileSetManager fileSetManager = new FileSetManager();

  64.         for (FileSet fileSet : getTestFilesets(fromSources, includeJavaSources)) {
  65.             for (String include : fileSetManager.getIncludedFiles(fileSet)) {
  66.                 files.add(new File(fileSet.getDirectory(), include));
  67.             }
  68.         }

  69.         return files;
  70.     }

  71.     /**
  72.      * Gets the set of included filesets from the specified source files or source directory (if sources are null).
  73.      *
  74.      * @param fromSources        The sources to get the included files from
  75.      * @param includeJavaSources Whether to include Java sources
  76.      * @return The included filesets from the specified sources
  77.      */
  78.     protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
  79.         FileSet[] result;
  80.         FileSet[] groovyFileSets;

  81.         if (fromSources != null) {
  82.             groovyFileSets = fromSources;
  83.         } else {
  84.             FileSet groovyFileSet = new FileSet();
  85.             String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
  86.             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
  87.             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
  88.             groovyFileSets = new FileSet[]{groovyFileSet};
  89.         }

  90.         if (includeJavaSources) {
  91.             List<FileSet> javaFileSets = new ArrayList<>();
  92.             for (String sourceRoot : project.getCompileSourceRoots()) {
  93.                 FileSet javaFileSet = new FileSet();
  94.                 javaFileSet.setDirectory(sourceRoot);
  95.                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
  96.                 javaFileSets.add(javaFileSet);
  97.             }
  98.             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
  99.             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
  100.             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
  101.         } else {
  102.             result = groovyFileSets;
  103.         }

  104.         return result;
  105.     }

  106.     /**
  107.      * Gets the set of included filesets from the specified source files or source directory (if sources are null).
  108.      *
  109.      * @param fromSources        The sources to get the included files from
  110.      * @param includeJavaSources Whether to include Java sources
  111.      * @return The included filesets from the specified sources
  112.      */
  113.     protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
  114.         FileSet[] result;
  115.         FileSet[] groovyFileSets;

  116.         if (fromSources != null) {
  117.             groovyFileSets = fromSources;
  118.         } else {
  119.             FileSet groovyFileSet = new FileSet();
  120.             String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
  121.             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
  122.             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
  123.             groovyFileSets = new FileSet[]{groovyFileSet};
  124.         }

  125.         if (includeJavaSources) {
  126.             List<FileSet> javaFileSets = new ArrayList<>();
  127.             for (String sourceRoot : project.getTestCompileSourceRoots()) {
  128.                 FileSet javaFileSet = new FileSet();
  129.                 javaFileSet.setDirectory(sourceRoot);
  130.                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
  131.                 javaFileSets.add(javaFileSet);
  132.             }
  133.             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
  134.             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
  135.             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
  136.         } else {
  137.             result = groovyFileSets;
  138.         }

  139.         return result;
  140.     }
  141. }