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  /*
23   * Copyright 2011
24   *
25   * Licensed under the Apache License, Version 2.0 (the "License");
26   * you may not use this file except in compliance with the License.
27   * You may obtain a copy of the License at
28   *
29   *  http://www.apache.org/licenses/LICENSE-2.0
30   *
31   * Unless required by applicable law or agreed to in writing, software
32   * distributed under the License is distributed on an "AS IS" BASIS,
33   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34   * See the License for the specific language governing permissions and
35   * limitations under the License.
36   */
37  
38  import org.apache.maven.artifact.Artifact;
39  import org.apache.maven.plugin.AbstractMojo;
40  import org.apache.maven.plugin.MojoExecution;
41  import org.apache.maven.project.MavenProject;
42  import org.codehaus.mojo.cobertura.configuration.MaxHeapSizeUtil;
43  import org.codehaus.mojo.cobertura.tasks.AbstractTask;
44  
45  import java.io.File;
46  import java.util.List;
47  
48  /**
49   * Abstract Base for Cobertura Mojos.
50   *
51   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
52   */
53  public abstract class AbstractCoberturaMojo
54      extends AbstractMojo
55  {
56      /**
57       * <i>Maven Internal</i>: Project to interact with.
58       *
59       * @parameter default-value="${project}"
60       * @required
61       * @readonly
62       */
63      private MavenProject project;
64  
65      /**
66       * Maximum memory to pass to the JVM for Cobertura processes.
67       *
68       * @parameter expression="${cobertura.maxmem}" default-value="64m"
69       */
70      // Duplicate def to please MojoTestCase.
71      // Use only default-value once tests have been refactored to IT's
72      private String maxmem = "64m";
73  
74      /**
75       * <p>
76       * The Datafile Location.
77       * </p>
78       *
79       * @parameter expression="${cobertura.datafile}" default-value="${project.build.directory}/cobertura/cobertura.ser"
80       * @required
81       * @readonly
82       */
83      private File dataFile;
84  
85      /**
86       * @parameter default-value="${mojoExecution}"
87       * @required
88       * @readonly
89       */
90      private MojoExecution mojoExecution;
91  
92      /**
93       * Only output Cobertura errors, avoid info messages.
94       *
95       * @parameter expression="${quiet}" default-value="false"
96       */
97      private boolean quiet;
98  
99      /**
100      * <i>Maven Internal</i>: List of artifacts for the plugin.
101      *
102      * @parameter default-value="${plugin.artifacts}"
103      * @required
104      * @readonly
105      */
106     protected List<Artifact> pluginClasspathList;
107 
108     /**
109      * When <code>true</code>, skip the execution.
110      *
111      * @parameter expression="${cobertura.skip}"
112      * default-value="false"
113      * @since 2.5
114      */
115     private boolean skip;
116 
117     /**
118      * Usually most of our Cobertura mojos will not get executed on parent poms.
119      * Setting this parameter to <code>true</code> will force
120      * the execution of this mojo, even if it would usually get skipped in this case.
121      *
122      * @parameter expression="${cobertura.force}"
123      * default-value=false
124      * @required
125      * @since 2.5
126      */
127     private boolean forceMojoExecution;
128 
129 
130     /**
131      * Setup the Task defaults.
132      *
133      * @param task the task to setup.
134      */
135     protected void setTaskDefaults( AbstractTask task )
136     {
137         task.setLog( getLog() );
138         task.setPluginClasspathList( pluginClasspathList );
139 
140         if ( MaxHeapSizeUtil.getInstance().envHasMavenMaxMemSetting() )
141         {
142             maxmem = MaxHeapSizeUtil.getInstance().getMavenMaxMemSetting();
143         }
144         else
145         {
146             task.setMaxmem( maxmem );
147         }
148         task.setQuiet( quiet );
149     }
150 
151     /**
152      * <p>Determine if the mojo execution should get skipped.</p>
153      * This is the case if:
154      * <ul>
155      * <li>{@link #skip} is <code>true</code></li>
156      * <li>if the mojo gets executed on a project with packaging type 'pom' and
157      * {@link #forceMojoExecution} is <code>false</code></li>
158      * </ul>
159      *
160      * @return <code>true</code> if the mojo execution should be skipped.
161      */
162     protected boolean skipMojo()
163     {
164         if ( skip )
165         {
166             getLog().info( "Skipping cobertura execution" );
167             return true;
168         }
169 
170         if ( !forceMojoExecution && "pom".equals( this.project.getPackaging() ) )
171         {
172             getLog().info( "Skipping cobertura mojo for project with packaging type 'pom'" );
173             return true;
174         }
175 
176         return false;
177     }
178 
179     /**
180      * Get the data file which is or will be generated by Cobertura, never <code>null</code>.
181      *
182      * @return the data file
183      */
184     protected File getDataFile()
185     {
186         return CoberturaMojoUtils.getDataFile( new File( this.project.getBuild().getDirectory() ), mojoExecution );
187     }
188 
189     /**
190      * @return the project
191      */
192     protected final MavenProject getProject()
193     {
194         return project;
195     }
196 
197 }