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.File;
 import java.net.URL;
 import java.util.*;


The base compile mojo, which all compile mojos extend.

Author(s):
Keegan Witt
 
 public abstract class AbstractCompileMojo extends AbstractGroovySourcesMojo {

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

    
The location for the compiled classes.

Parameter:
default-value="${project.build.outputDirectory}"
 
     protected File outputDirectory;

    
The location for the compiled test classes.

Parameter:
default-value="${project.build.testOutputDirectory}"
 
     protected File testOutputDirectory;

    
The encoding of source files.

Parameter:
default-value="${project.build.sourceEncoding}"
 
     protected String sourceEncoding;

    
The Groovy compiler bytecode compatibility. One of
  • 1.4
  • 1.5
  • 1.6
  • 1.7
  • 1.8

Parameter:
default-value="1.5"
 
     protected String targetBytecode;

    
Whether Groovy compiler should be set to debug.

Parameter:
default-value="false"
 
     protected boolean debug;

    
Whether Groovy compiler should be set to verbose.

Parameter:
default-value="false"
 
     protected boolean verbose;

    
Groovy compiler warning level. Should be one of:
0
None
1
Likely Errors
2
Possible Errors
3
Paranoia

Parameter:
default-value="1"
    protected int warningLevel;

    
Groovy compiler error tolerance (the number of non-fatal errors (per unit) that should be tolerated before compilation is aborted).

Parameter:
default-value="0"
    protected int tolerance;

    
Allow setting whether to support invokeDynamic (requires Java 7 or greater).

Parameter:
property="invokeDynamic" default-value="false"
    protected boolean invokeDynamic;

    
Performs compilation of compile mojos.

Parameters:
sourcesToCompile The sources to compile
classpath The classpath to use for compilation
compileOutputDirectory The directory to write the compiled class files to
Throws:
java.lang.ClassNotFoundException When a class needed for compilation cannot be found
java.lang.InstantiationException When a class needed for compilation cannot be instantiated
java.lang.IllegalAccessException When a method needed for compilation cannot be accessed
java.lang.reflect.InvocationTargetException When a reflection invocation needed for compilation cannot be completed
java.net.MalformedURLException When a classpath element provides a malformed URL
    @SuppressWarnings("unchecked")
    protected synchronized void doCompile(final Set<FilesourcesToCompilefinal List classpathfinal File compileOutputDirectory)
        if (sourcesToCompile == null || sourcesToCompile.isEmpty()) {
            getLog().info("No sources specified for compilation.  Skipping.");
            return;
        }
        // get classes we need with reflection
        Class<?> compilerConfigurationClass = Class.forName("org.codehaus.groovy.control.CompilerConfiguration");
        Class<?> compilationUnitClass = Class.forName("org.codehaus.groovy.control.CompilationUnit");
        Class<?> groovyClassLoaderClass = Class.forName("groovy.lang.GroovyClassLoader");
        // set up compile options
        Object compilerConfiguration = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilerConfigurationClass));
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setDebug"boolean.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setVerbose"boolean.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setWarningLevel"int.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTolerance"int.class), compilerConfiguration);
        if (getGroovyVersion().compareTo(new Version(2, 1, 3)) >= 0) {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfiguration);
        } else {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfiguration"1.5");
        }
        if ( != null) {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setSourceEncoding"String.class), compilerConfiguration);
        }
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetDirectory"String.class), compilerConfigurationcompileOutputDirectory.getAbsolutePath());
        if (getGroovyVersion().compareTo(new Version(2, 0, 0, "beta-3")) >= 0 && ) {
            if (isGroovyIndy()) {
                Map<java.lang.Stringjava.lang.BooleanoptimizationOptions = (Map<StringBoolean>) ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"getOptimizationOptions"), compilerConfiguration);
                optimizationOptions.put("indy"true);
                optimizationOptions.put("int"false);
            } else {
                getLog().warn("Requested to use InvokeDynamic option but the version of Groovy on the project classpath doesn't support it.  Ignoring invokeDynamic option.");
            }
            if (!isJavaSupportIndy()) {
                throw new IllegalArgumentException("Tried to use to use an indy version of Groovy, but your Java version (" + getJavaVersionString() + ") doesn't support it.  Failing build.");
            }
        }
        // append project classpath to groovyClassLoader and transformLoader
        Object groovyClassLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.classcompilerConfigurationClass), compilationUnitClass.getClassLoader(), compilerConfiguration);
        Object transformLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.class), compilationUnitClass.getClassLoader());
        getLog().debug("Classpath: ");
        if (classpath != null) {
            for (Object classpathElement : classpath) {
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyClassLoaderClass"addURL"URL.class), groovyClassLoadernew File((StringclasspathElement).toURI().toURL());
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyClassLoaderClass"addURL"URL.class), transformLoadernew File((StringclasspathElement).toURI().toURL());
                getLog().debug("    " + classpathElement);
            }
        }
        // add Groovy sources
        Object compilationUnit;
        if (getGroovyVersion().compareTo(new Version(1, 6, 0)) >= 0) {
            compilationUnit = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilationUnitClasscompilerConfigurationClassCodeSource.classgroovyClassLoaderClassgroovyClassLoaderClass), compilerConfigurationnullgroovyClassLoadertransformLoader);
        } else {
            compilationUnit = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilationUnitClasscompilerConfigurationClassCodeSource.classgroovyClassLoaderClass), compilerConfigurationnullgroovyClassLoader);
        }
        getLog().debug("Adding Groovy to compile:");
        for (File source : sourcesToCompile) {
            getLog().debug("    " + source);
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilationUnitClass"addSource"File.class), compilationUnitsource);
        }
        // compile the classes
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilationUnitClass"compile"), compilationUnit);
        // log compiled classes
        List classes = (List) ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilationUnitClass"getClasses"), compilationUnit);
        getLog().info("Compiled " + classes.size() + " file" + (classes.size() > 1 || classes.size() == 0 ? "s" : "") + ".");
    }

    
Determines whether this mojo can be run with the version of Groovy supplied. Must be >= 1.5.0 because not all the classes 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