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.tasks;
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.codehaus.mojo.cobertura.configuration.ConfigInstrumentation;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.codehaus.plexus.util.StringUtils;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * The Instrument Task.
34   *
35   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
36   */
37  public class InstrumentTask
38      extends AbstractTask
39  {
40      private ConfigInstrumentation config = null;
41  
42      private File dataFile = null;
43  
44      private File destinationDir = null;
45  
46      /**
47       * Create a new InstrumentTask.
48       */
49      public InstrumentTask()
50      {
51          super( net.sourceforge.cobertura.instrument.InstrumentMain.class.getName() );
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      public void execute()
58          throws MojoExecutionException
59      {
60          /* this task should always use the commands file approach */
61          cmdLineArgs.setUseCommandsFile( true );
62  
63          if ( StringUtils.isNotEmpty( config.getMaxmem() ) )
64          {
65              this.setMaxmem( config.getMaxmem() );
66          }
67  
68          if ( dataFile != null )
69          {
70              cmdLineArgs.addArg( "--datafile", dataFile.getAbsolutePath() );
71          }
72  
73          if ( destinationDir != null )
74          {
75              cmdLineArgs.addArg( "--destination", destinationDir.getAbsolutePath() );
76          }
77  
78          if ( config.getIgnoreTrivial() )
79          {
80              cmdLineArgs.addArg( "--ignoreTrivial" );
81          }
82  
83          for ( String ignoreMethodAnnotation : config.getIgnoreMethodAnnotations() )
84          {
85              cmdLineArgs.addArg( "--ignoreMethodAnnotation", ignoreMethodAnnotation );
86          }
87  
88          for ( String ignore : config.getIgnores() )
89          {
90              cmdLineArgs.addArg( "--ignore", ignore );
91          }
92  
93          String includes = joinCludes( config.getIncludes() );
94          String excludes = joinCludes( config.getExcludes() );
95          @SuppressWarnings( "unchecked" ) String defaultExcludes = joinCludes( FileUtils.getDefaultExcludesAsList() );
96  
97          if ( StringUtils.isNotEmpty( excludes ) )
98          {
99              excludes += "," + defaultExcludes;
100         }
101         else
102         {
103             excludes = defaultExcludes;
104         }
105 
106         try
107         {
108             if ( getLog().isDebugEnabled() )
109             {
110                 getLog().debug( "Config : " + config );
111                 getLog().debug( "Basedir: " + config.getBasedir() );
112                 getLog().debug( "Include: " + includes );
113                 getLog().debug( "Exclude: " + excludes );
114                 getLog().debug( "Max Mem: " + config.getMaxmem() );
115             }
116 
117             @SuppressWarnings( "unchecked" ) List<String> filenames =
118                 FileUtils.getFileNames( config.getBasedir(), includes, excludes, false );
119 
120             if ( filenames.isEmpty() )
121             {
122                 getLog().warn( "No files to instrument." );
123                 return;
124             }
125 
126             cmdLineArgs.addArg( "--basedir", config.getBasedir().getAbsolutePath() );
127             for ( String filename : filenames )
128             {
129                 if ( getLog().isDebugEnabled() )
130                 {
131                     getLog().debug( "To Instrument: " + filename );
132                 }
133                 cmdLineArgs.addArg( filename );
134             }
135         }
136         catch ( IOException e )
137         {
138             throw new MojoExecutionException( "Unable to obtain file list from includes/excludes.", e );
139         }
140 
141         int returnCode = executeJava();
142 
143         // Check the return code and print a message
144         if ( returnCode == 0 )
145         {
146             if ( !isQuiet() )
147             {
148                 getLog().info( "Instrumentation was successful." );
149             }
150         }
151         else
152         {
153             throw new MojoExecutionException( "Unable to instrument project." );
154         }
155 
156     }
157 
158     /**
159      * @return the config.
160      */
161     public ConfigInstrumentation getConfig()
162     {
163         return config;
164     }
165 
166     /**
167      * @return the data file
168      */
169     public File getDataFile()
170     {
171         return dataFile;
172     }
173 
174     /**
175      * @return the destination directory.
176      */
177     public File getDestinationDir()
178     {
179         return destinationDir;
180     }
181 
182     /**
183      * Construct a ,-separated string from a list of strings.
184      *
185      * @param cludes the list of strings.
186      * @return the ,-separated string.
187      */
188     public String joinCludes( List<String> cludes )
189     {
190         StringBuffer sb = new StringBuffer();
191         Iterator<String> it = cludes.iterator();
192         while ( it.hasNext() )
193         {
194             String clude = it.next();
195             sb.append( clude );
196             if ( it.hasNext() )
197             {
198                 sb.append( "," );
199             }
200         }
201         return sb.toString();
202     }
203 
204     /**
205      * Set the configuration
206      *
207      * @param config the configuration
208      */
209     public void setConfig( ConfigInstrumentation config )
210     {
211         this.config = config;
212     }
213 
214     /**
215      * Set the data file
216      *
217      * @param dataFile the data file
218      */
219     public void setDataFile( File dataFile )
220     {
221         this.dataFile = dataFile;
222     }
223 
224     /**
225      * Set the destination directory
226      *
227      * @param destinationDir the directory
228      */
229     public void setDestinationDir( File destinationDir )
230     {
231         this.destinationDir = destinationDir;
232     }
233 
234     @Override
235     public String createClasspath()
236         throws MojoExecutionException
237     {
238         return this.config.getBasedir().getAbsolutePath() + super.createClasspath();
239     }
240 
241 
242 }