View Javadoc
1   /*
2    * Copyright (C) 2011 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.artifact.DependencyResolutionRequiredException;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugins.annotations.LifecyclePhase;
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.plugins.annotations.ResolutionScope;
25  import org.apache.maven.shared.model.fileset.FileSet;
26  
27  import java.io.File;
28  import java.lang.reflect.InvocationTargetException;
29  import java.net.MalformedURLException;
30  
31  
32  /**
33   * Compiles the test sources.
34   * Note that this mojo requires Groovy >= 1.5.0, and >= 2.0.0-beta-3 (the indy version) for compiling with invokedynamic option.
35   *
36   * @author Keegan Witt
37   * @since 1.0-beta-1
38   */
39  @Mojo(name = "compileTests", defaultPhase = LifecyclePhase.TEST_COMPILE, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
40  public class CompileTestsMojo extends AbstractCompileMojo {
41  
42      /**
43       * The Groovy test source files (relative paths).
44       * Default: "${project.basedir}/src/test/groovy/**/*.groovy"
45       */
46      @Parameter
47      protected FileSet[] testSources;
48  
49      /**
50       * The location for the compiled test classes.
51       */
52      @Parameter(defaultValue = "${project.build.testOutputDirectory}")
53      protected File testOutputDirectory;
54  
55      /**
56       * Flag to allow test compilation to be skipped.
57       */
58      @Parameter(property = "maven.test.skip", defaultValue = "false")
59      protected boolean skipTests;
60  
61      /**
62       * Executes this mojo.
63       *
64       * @throws MojoExecutionException If an unexpected problem occurs (causes a "BUILD ERROR" message to be displayed)
65       */
66      @Override
67      public void execute() throws MojoExecutionException {
68          if (!skipTests) {
69              try {
70                  try {
71                      getLog().debug("Project test classpath:\n" + project.getTestClasspathElements());
72                  } catch (DependencyResolutionRequiredException e) {
73                      getLog().debug("Unable to log project test classpath");
74                  }
75                  doCompile(getTestFiles(testSources, false), project.getTestClasspathElements(), testOutputDirectory);
76              } catch (ClassNotFoundException e) {
77                  throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project?", e);
78              } catch (InvocationTargetException e) {
79                  throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
80              } catch (InstantiationException e) {
81                  throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
82              } catch (IllegalAccessException e) {
83                  throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
84              } catch (DependencyResolutionRequiredException e) {
85                  throw new MojoExecutionException("Test dependencies weren't resolved.", e);
86              } catch (MalformedURLException e) {
87                  throw new MojoExecutionException("Unable to add project test dependencies to classpath.", e);
88              }
89          } else {
90              getLog().info("Compilation of tests is skipped.");
91          }
92      }
93  
94  }