Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.codehaus.gmavenplus.mojo;
 
 import java.io.*;
 import java.util.*;
The base groovydoc mojo, which all groovydoc mojos extend.

Author(s):
Keegan Witt
 
 public abstract class AbstractGroovydocMojo extends AbstractGroovySourcesMojo {
     // TODO: support Groovy 1.5.0 - 1.6.1?
     /*
      * For some reason some NPE about a rootDoc occurs with older versions
      * (note that I used a different constructor and an addSource(File) instead
      * of addSources(List<File>) because that didn't exist back then.
      */

    
The minimum version of Groovy that this mojo supports.
 
     protected static final Version MIN_GROOVY_VERSION = new Version(1, 6, 2);

    
The location for the generated API docs.

Parameter:
default-value="${project.build.directory}/gapidocs"
 
     protected File groovydocOutputDirectory;

    
The location for the generated test API docs.

Parameter:
default-value="${project.build.directory}/testgapidocs"
 
     protected File testGroovydocOutputDirectory;

    
The window title.

Parameter:
default-value="Groovy Documentation"
 
     protected String windowTitle;

    
The page title.

Parameter:
default-value="Groovy Documentation"
 
     protected String docTitle;

    
The page footer.

Parameter:
default-value="Groovy Documentation"
 
     protected String footer;

    
The page header.

Parameter:
default-value="Groovy Documentation"
 
     protected String header;

    
Whether to display the author in the generated Groovydoc.

Parameter:
default-value="true"
 
     protected boolean displayAuthor;

    
The HTML file to be used for overview documentation.

Parameter:
    protected File overviewFile;

    
The stylesheet file (absolute path) to copy to output directory (will overwrite default stylesheet.css).

Parameter:
    protected File stylesheetFile;

    
The encoding of stylesheetFile.

Parameter:
default-value="UTF-8"
    protected String stylesheetEncoding;

    
The scope to generate Groovydoc for, should be one of:
  • "public"
  • "protected"
  • "package"
  • "private"

Parameter:
default-value="private"
    protected String scope;

    
Links to include in the generated groovydoc (key is link href, value is comma-separated packages to use that link).

Parameter:
    protected List<Linklinks;

    
Whether to include Java sources in groovydoc generation.

Parameter:
default-value="true"
    protected boolean groovydocJavaSources;

    
Generates the groovydoc for the specified sources.

Parameters:
sourceDirectories The source directories to generate groovydoc for
outputDirectory The directory to save the generated groovydoc in
Throws:
java.lang.ClassNotFoundException When a class needed for groovydoc generation cannot be found
java.lang.InstantiationException When a class needed for groovydoc generation cannot be instantiated
java.lang.IllegalAccessException When a method needed for groovydoc generation cannot be accessed
java.lang.reflect.InvocationTargetException When a reflection invocation needed for groovydoc generation cannot be completed
    @SuppressWarnings("unchecked")
    protected void generateGroovydoc(final FileSet[] sourceDirectoriesfinal File outputDirectorythrows ClassNotFoundExceptionInvocationTargetExceptionIllegalAccessExceptionInstantiationException {
        if (sourceDirectories == null || sourceDirectories.length == 0) {
            getLog().info("No source directories specified for Groovydoc generation.  Skipping.");
            return;
        }
        // get classes we need with reflection
        Class<?> groovyDocToolClass = Class.forName("org.codehaus.groovy.tools.groovydoc.GroovyDocTool");
        Class<?> outputToolClass = Class.forName("org.codehaus.groovy.tools.groovydoc.OutputTool");
        Class<?> fileOutputToolClass = Class.forName("org.codehaus.groovy.tools.groovydoc.FileOutputTool");
        Class<?> resourceManagerClass = Class.forName("org.codehaus.groovy.tools.groovydoc.ResourceManager");
        Class<?> classpathResourceManagerClass = Class.forName("org.codehaus.groovy.tools.groovydoc.ClasspathResourceManager");
        Class<?> linkArgumentClass = Class.forName("org.codehaus.groovy.tools.groovydoc.LinkArgument");
        // set up Groovydoc options
        Properties properties = new Properties();
        properties.setProperty("windowTitle");
        properties.setProperty("docTitle");
        properties.setProperty("footer");
        properties.setProperty("header");
        properties.setProperty("author", Boolean.toString());
        properties.setProperty("overviewFile" != null ? .getAbsolutePath() : "");
        try {
            Scopes scopeVal = Scopes.valueOf(.toUpperCase());
            if (scopeVal.equals(.)) {
                properties.setProperty("publicScope""true");
            } else if (scopeVal.equals(.)) {
                properties.setProperty("protectedScope""true");
            } else if (scopeVal.equals(.)) {
                properties.setProperty("packageScope""true");
            } else if (scopeVal.equals(.)) {
                properties.setProperty("privateScope""true");
            }
        } catch (IllegalArgumentException e) {
            getLog().warn("Scope (" +  + ") was not recognized.  Skipping argument.");
        }
        Object fileOutputTool = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(fileOutputToolClass));
        Object classpathResourceManager = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(classpathResourceManagerClass));
        // generate Groovydoc
        FileSetManager fileSetManager = new FileSetManager(getLog());
        List<StringsourceDirectoriesStrings = new ArrayList<String>();
        for (FileSet sourceDirectory : sourceDirectories) {
            sourceDirectoriesStrings.add(sourceDirectory.getDirectory());
        }
        List linksList = new ArrayList();
        if ( != null) {
            for (Link link : ) {
                Object linkArgument = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(linkArgumentClass));
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(linkArgumentClass"setHref"String.class), linkArgumentlink.getHref());
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(linkArgumentClass"setPackages"String.class), linkArgumentlink.getPackages());
                linksList.add(linkArgument);
            }
        }
        Object groovyDocTool = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyDocToolClassresourceManagerClassString[].classString[].classString[].classString[].classList.classProperties.class),
                classpathResourceManager,
                sourceDirectoriesStrings.toArray(new String[sourceDirectoriesStrings.size()]),
                .,
                .,
                .,
                linksList,
                properties
        );
        List<StringjavaSources = new ArrayList<String>();
        List<StringgroovySources = new ArrayList<String>();
        List<StringpossibleGroovyStubs = new ArrayList<String>();
        for (FileSet sourceDirectory : sourceDirectories) {
            List<Stringsources = Arrays.asList(fileSetManager.getIncludedFiles(sourceDirectory));
            for (String source : sources) {
                if (source.endsWith(".java") && !javaSources.contains(source)) {
                    javaSources.add(source);
                } else if (!groovySources.contains(source)) {
                    groovySources.add(source);
                    possibleGroovyStubs.add(source.replaceFirst("\\." + Files.getFileExtension(source), ".java"));
                }
            }
        }
        javaSources.removeAll(possibleGroovyStubs);
        List<StringgroovydocSources = new ArrayList<String>();
        groovydocSources.addAll(javaSources);
        groovydocSources.addAll(groovySources);
        getLog().debug("Adding sources to generate Groovydoc for:");
        getLog().debug("    " + groovydocSources);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyDocToolClass"add"List.class), groovyDocToolgroovydocSources);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyDocToolClass"renderToOutput"outputToolClassString.class), groovyDocToolfileOutputTooloutputDirectory.getAbsolutePath());
        // overwrite stylesheet.css with provided stylesheet (if configured)
        if ( != null) {
            copyStylesheet(outputDirectory);
        }
    }

    
Copies the stylesheet to the specified output directory.

Parameters:
outputDirectory The output directory to copy the stylesheet to
    private void copyStylesheet(final File outputDirectory) {
        getLog().info("Using stylesheet from " + .getAbsolutePath() + ".");
        Closer closer = Closer.create();
        try {
            try {
                BufferedReader bufferedReader = closer.register(new BufferedReader(new InputStreamReader(new FileInputStream(), )));
                StringBuilder css = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    css.append(line).append("\n");
                }
                File outfile = new File(outputDirectory"stylesheet.css");
                BufferedWriter bufferedWriter = closer.register(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), )));
                bufferedWriter.write(css.toString());
            } catch (Throwable throwable) {
                throw closer.rethrow(throwable);
            } finally {
                closer.close();
            }
        } catch (IOException e) {
            getLog().warn("Unable to copy specified stylesheet (" + .getAbsolutePath() + ").");
        }
    }

    
Determines whether this mojo can be run with the version of Groovy supplied. Must be >= 1.6.2 because not all the classes/methods needed were available and functioning correctly in previous versions.

Returns:
true only if the version of Groovy supports this mojo
    protected boolean groovyVersionSupportsAction() {
        return getGroovyVersion() != null && getGroovyVersion().compareTo() >= 0;
    }
New to GrepCode? Check out our FAQ X