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.util.List;
 import java.util.Map;
 import java.util.Set;


The base compile mojo, which all compile mojos extend.

Author(s):
Keegan Witt
Since:
1.0-beta-1
 
 public abstract class AbstractCompileMojo extends AbstractGroovySourcesMojo {

    
Groovy 2.1.0 beta-3 version.
 
     protected static final Version GROOVY_2_1_0_BETA3 = new Version(2, 1, 0, "beta-3");

    
Groovy 2.1.0 beta-1 version.
 
     protected static final Version GROOVY_2_1_0_BETA1 = new Version(2, 1, 0, "beta-1");

    
Groovy 1.6.0 version.
 
     protected static final Version GROOVY_1_6_0 = new Version(1, 6, 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
Using 1.6 or 1.7 requires Groovy >= 2.1.3, and using 1.8 requires Groovy >= 2.3.3. If an invalid selection is made, Groovy will default to VM determined version (1.4 or 1.5).

Parameter:
property="maven.compiler.target" 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;

    
Whether to support invokeDynamic (requires Java 7 or greater and Groovy indy 2.0.0-beta-3 or greater).

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

    
A script for tweaking the configuration options (requires Groovy 2.1.0-beta-1 or greater). Note that its encoding must match your source encoding.

Parameter:
property="configScript"
    protected File configScript;

    
Performs compilation of compile mojos.

Parameters:
sources 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<Filesourcesfinal List classpathfinal File compileOutputDirectory)
         = new ClassWrangler(classpathgetLog());
        if (sources == null || sources.isEmpty()) {
            getLog().info("No sources specified for compilation.  Skipping.");
            return;
        }
        if (groovyVersionSupportsAction()) {
            logPluginClasspath();
            if (getLog().isDebugEnabled()) {
                try {
                    getLog().debug("Project compile classpath:\n" + .getCompileClasspathElements());
                } catch (DependencyResolutionRequiredException e) {
                    getLog().warn("Unable to log project compile classpath"e);
                }
            }
        } else {
            getLog().error("Your Groovy version (" + .getGroovyVersion() + ") doesn't support compilation.  The minimum version of Groovy required is " +  + ".  Skipping compiling.");
            return;
        }
        // get classes we need with reflection
        Class compilerConfigurationClass = .getClass("org.codehaus.groovy.control.CompilerConfiguration");
        Class compilationUnitClass = .getClass("org.codehaus.groovy.control.CompilationUnit");
        Class groovyClassLoaderClass = .getClass("groovy.lang.GroovyClassLoader");
        // setup compile options
        Object compilerConfiguration = setupCompilerConfiguration(compileOutputDirectorycompilerConfigurationClass);
        Object groovyClassLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.classcompilerConfigurationClass), .getClassLoader(), compilerConfiguration);
        Object transformLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.class), .getClassLoader());
        // add Groovy sources
        Object compilationUnit = setupCompilationUnit(sourcescompilerConfigurationClasscompilationUnitClassgroovyClassLoaderClasscompilerConfigurationgroovyClassLoadertransformLoader);
        // 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" : "") + ".");
    }

    
Sets up the CompilationUnit to use for compilation.

Parameters:
sources the sources to compile
compilerConfigurationClass the CompilerConfiguration class
compilationUnitClass the CompilationUnit class
groovyClassLoaderClass the GroovyClassLoader class
compilerConfiguration the CompilerConfiguration
groovyClassLoader the GroovyClassLoader
transformLoader the GroovyClassLoader to use for transformation
Returns:
the CompilationUnit
Throws:
java.lang.InstantiationException when a class needed for setting up compilation unit cannot be instantiated
java.lang.IllegalAccessException when a method needed for setting up compilation unit cannot be accessed
java.lang.reflect.InvocationTargetException when a reflection invocation needed for setting up compilation unit cannot be completed
    protected Object setupCompilationUnit(final Set<Filesourcesfinal Class compilerConfigurationClassfinal Class compilationUnitClassfinal Class groovyClassLoaderClassfinal Object compilerConfigurationfinal Object groovyClassLoaderfinal Object transformLoaderthrows InvocationTargetExceptionIllegalAccessExceptionInstantiationException {
        Object compilationUnit;
        if (.getGroovyVersion().compareTo() >= 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 : sources) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("    " + source);
            }
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilationUnitClass"addSource"File.class), compilationUnitsource);
        }
        return compilationUnit;
    }

    
Sets up the CompilationConfiguration to use for compilation.

Parameters:
compileOutputDirectory the directory to write the compiled classes to
compilerConfigurationClass the CompilerConfiguration class
Returns:
the CompilerConfiguration
Throws:
java.lang.ClassNotFoundException when a class needed for setting up CompilerConfiguration cannot be found
java.lang.InstantiationException when a class needed for setting up CompilerConfiguration cannot be instantiated
java.lang.IllegalAccessException when a method needed for setting up CompilerConfiguration cannot be accessed
java.lang.reflect.InvocationTargetException when a reflection invocation needed for setting up CompilerConfiguration cannot be completed
    @SuppressWarnings("unchecked")
    protected Object setupCompilerConfiguration(final File compileOutputDirectoryfinal Class compilerConfigurationClassthrows InvocationTargetExceptionIllegalAccessExceptionInstantiationExceptionClassNotFoundException {
        Object compilerConfiguration = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilerConfigurationClass));
        if ( != null) {
            if (.getGroovyVersion().compareTo() >= 0) {
                Class bindingClass = .getClass("groovy.lang.Binding");
                Class importCustomizerClass = .getClass("org.codehaus.groovy.control.customizers.ImportCustomizer");
                Class groovyShellClass = .getClass("groovy.lang.GroovyShell");
                Object binding = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(bindingClass));
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(bindingClass"setVariable"String.classObject.class), binding"configuration"compilerConfiguration);
                Object shellCompilerConfiguration = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilerConfigurationClass));
                Object importCustomizer = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(importCustomizerClass));
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(importCustomizerClass"addStaticStar"String.class), importCustomizer"org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
                List compilationCustomizers = (List) ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"getCompilationCustomizers"), shellCompilerConfiguration);
                compilationCustomizers.add(importCustomizer);
                Object shell = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyShellClassbindingClasscompilerConfigurationClass), bindingshellCompilerConfiguration);
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyShellClass"evaluate"File.class), shell , );
            } else {
                getLog().warn("Requested to use configScript, but your Groovy version doesn't support it (must be 2.1.0-beta-1 or newer).  Ignoring configScript parameter.");
            }
        }
        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);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfiguration);
        if ( != null) {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setSourceEncoding"String.class), compilerConfiguration);
        }
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetDirectory"String.class), compilerConfigurationcompileOutputDirectory.getAbsolutePath());
        if () {
            if (.getGroovyVersion().compareTo() >= 0) {
                if (.isGroovyIndy()) {
                    if (isJavaSupportIndy()) {
                        Map<StringBooleanoptimizationOptions = (Map<StringBoolean>) ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"getOptimizationOptions"), compilerConfiguration);
                        optimizationOptions.put("indy"true);
                        optimizationOptions.put("int"false);
                    } else {
                        getLog().warn("Tried to use to use an indy version of Groovy, but your Java version (" + getJavaVersionString() + ") doesn't support it.  Ignoring invokeDynamic parameter.");
                    }
                } else {
                    getLog().warn("Requested to use InvokeDynamic, but the version of Groovy on the project classpath doesn't support it (must use have indy classifier).  Ignoring invokeDynamic parameter.");
                }
            } else {
                getLog().warn("Requested to use invokeDynamic, but your Groovy version doesn't support it (must be 2.0.0-beta-3 or newer).  Ignoring invokeDynamic parameter.");
            }
        }
        return compilerConfiguration;
    }
New to GrepCode? Check out our FAQ X