View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.codehaus.mojo.cobertura;
21  
22  import org.apache.maven.artifact.handler.ArtifactHandler;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.codehaus.mojo.cobertura.configuration.ConfigCheck;
26  import org.codehaus.mojo.cobertura.tasks.CheckTask;
27  
28  /**
29   * Check the coverage percentages for unit tests from the last instrumentation,
30   * and optionally fail the build if the targets are not met. To fail the build
31   * you need to set
32   * <code>configuration/check/haltOnFailure=true</code> in the plugin's
33   * configuration.
34   *
35   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
36   * @goal check
37   * @execute phase="test" lifecycle="cobertura"
38   * @phase verify
39   */
40  public class CoberturaCheckMojo
41      extends AbstractCoberturaMojo
42  {
43      /**
44       * The <a href="usage.html#Check">Check Configuration</a>.
45       *
46       * @parameter
47       * @required
48       */
49      private ConfigCheck check;
50  
51      /**
52       * {@inheritDoc}
53       */
54      public void execute()
55          throws MojoExecutionException, MojoFailureException
56      {
57          if ( skipMojo() )
58          {
59              return;
60          }
61  
62          ArtifactHandler artifactHandler = getProject().getArtifact().getArtifactHandler();
63          if ( !"java".equals( artifactHandler.getLanguage() ) )
64          {
65              getLog().info(
66                  "Not executing cobertura:instrument as the project is not a Java classpath-capable package" );
67          }
68          else
69          {
70              if ( !getDataFile().exists() )
71              {
72                  getLog().info( "Cannot perform check, instrumentation not performed - skipping." );
73              }
74              else
75              {
76                  CheckTask task = new CheckTask();
77                  setTaskDefaults( task );
78                  task.setConfig( check );
79                  task.setDataFile( getDataFile().getAbsolutePath() );
80  
81                  task.execute();
82              }
83          }
84      }
85  }