View Javadoc
1   package com.simpligility.maven.plugins.android.phase04processclasses;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.plugin.MojoFailureException;
8   import org.apache.maven.plugins.annotations.LifecyclePhase;
9   import org.apache.maven.plugins.annotations.Mojo;
10  import org.apache.maven.plugins.annotations.Parameter;
11  import org.apache.maven.plugins.annotations.ResolutionScope;
12  import org.codehaus.plexus.util.StringUtils;
13  
14  import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
15  import com.simpligility.maven.plugins.android.configuration.Emma;
16  import com.vladium.emma.instr.InstrProcessor;
17  import com.vladium.emma.instr.InstrProcessor.OutMode;
18  
19  /**
20   * After compiled Java classes use emma tool
21   * 
22   * @author mariusz@saramak.eu
23   */
24  @Mojo(
25          name = "emma",
26          defaultPhase = LifecyclePhase.PROCESS_CLASSES,
27          requiresDependencyResolution = ResolutionScope.COMPILE
28  )
29  public class EmmaMojo extends AbstractAndroidMojo
30  {
31  
32      private static final String EMMA_FOLDER_NAME = "emma";
33      private static final String CLASSES_FOLDER_NAME = "classes";
34      private static final String COVERAGE_METADATA_NAME = "coverage.em";
35  
36      /**
37       * Configuration for the emma command execution. It can be configured in the plugin configuration like so
38       * 
39       * 
40       * <pre>
41       * &lt;emma&gt;
42       *   &lt;enable&gt;true|false&lt;/enable&gt;
43       *   &lt;classFolders&gt;${project}/target/classes&lt;/classFolders&gt;
44       *   &lt;outputMetaFile&gt;${project}/target/emma/coverage.em&lt;/outputMetaFile&gt;
45       *   &lt;filters&gt;${project}emma filter&lt;/filters&gt;
46       * &lt;/emma&gt;
47       * </pre>
48       * 
49       * or via properties emma.* or command line parameters android.emma.*
50       */
51      @Parameter
52      private Emma emma;
53      /**
54       * Decides whether to enable or not enable emma.
55       */
56      @Parameter( property = "android.emma.enable", defaultValue = "false" )
57      private boolean emmaEnable;
58  
59      /**
60       * Configure directory where compiled classes are.
61       */
62      @Parameter( property = "android.emma.classFolders", defaultValue = "${project.build.directory}/classes/" )
63      private String emmaClassFolders;
64  
65      /**
66       * Path of the emma meta data file (.em).
67       */
68      @Parameter( property = "android.emma.outputMetaFile", defaultValue = "${project.build.directory}/emma/coverage.em" )
69      private File emmaOutputMetaFile;
70  
71      /**
72       * Emma filter. Refer to the <a href="http://emma.sourceforge.net/reference/ch02s06s02.html">emma syntax for
73       * filters</a>.
74       */
75      @Parameter( property = "android.emma.filters" )
76      private String emmaFilters;
77  
78      private boolean parsedEnable;
79      private String[] parsedEmmaClassFolders;
80      private String parsedOutputMetadataFile;
81      private String parsedFilters;
82  
83      @Override
84      public void execute() throws MojoExecutionException, MojoFailureException
85      {
86          getLog().debug( "Emma start working. Before parse configuration" );
87          parseConfiguration();
88          if ( parsedEnable )
89          {
90              getLog().info( "Emma OVERWRITE compiled class is on for this project! " //
91                      + "Do NOT use this project on production" );
92              getLog().debug(
93                      "configuration:  Class Folders - this file will be modified by emma " + parsedEmmaClassFolders );
94              getLog().debug( "configuration:  parsedOutputMetadataFile " + parsedOutputMetadataFile );
95              InstrProcessor processor = InstrProcessor.create();
96              if ( StringUtils.isNotEmpty( parsedFilters ) )
97              {
98                  processor.setInclExclFilter( parsedFilters.split( "," ) );
99              }
100             processor.setInstrPath( parsedEmmaClassFolders, true );
101             processor.setInstrOutDir( parsedEmmaClassFolders[ 0 ] ); // always to
102                                                                      // first define
103                                                                      // folder
104             processor.setMetaOutFile( parsedOutputMetadataFile );
105             processor.setOutMode( OutMode.OUT_MODE_OVERWRITE );
106             processor.setMetaOutMerge( Boolean.TRUE );
107             processor.run();
108         }
109         getLog().debug(
110                 "Emma OVERWRITE is OFF for this project (" + project.getArtifactId()
111                         + ") target/classes files are safe" );
112     }
113 
114     private void parseConfiguration() throws MojoExecutionException
115     {
116         if ( emma != null )
117         {
118             if ( emma.isEnable() == null )
119             {
120                 parsedEnable = emmaEnable;
121             }
122             else
123             {
124                 parsedEnable = emma.isEnable();
125             }
126             if ( emma.getClassFolders() != null )
127             {
128                 parsedEmmaClassFolders = getAllCompiledDirectory();
129             }
130             else
131             {
132                 parsedEmmaClassFolders = getDefaultCompiledFolders();
133             }
134             if ( emma.getOutputMetaFile() != null )
135             {
136                 parsedOutputMetadataFile = emma.getOutputMetaFile();
137             }
138             else
139             {
140                 parsedOutputMetadataFile = getDefaultMetaDataFile();
141             }
142             if ( StringUtils.isNotEmpty( emma.getFilters() ) )
143             {
144                 parsedFilters = emma.getFilters();
145             }
146         }
147         else
148         {
149             parsedEnable = emmaEnable;
150             parsedEmmaClassFolders = new String[]
151             { emmaClassFolders };
152             parsedOutputMetadataFile = emmaOutputMetaFile.getAbsolutePath();
153             parsedFilters = emmaFilters;
154         }
155     }
156 
157     private String getDefaultMetaDataFile()
158     {
159         File outputFolder = new File( targetDirectory + File.separator + EMMA_FOLDER_NAME
160                 + File.separator + COVERAGE_METADATA_NAME );
161         return outputFolder.getAbsolutePath();
162     }
163 
164     private String[] getDefaultCompiledFolders()
165     {
166         File sourceJavaFolder = new File( targetDirectory + File.separator + CLASSES_FOLDER_NAME
167                 + File.separator );
168         return new String[]
169         { sourceJavaFolder.getAbsolutePath() };
170     }
171 
172     private String[] getAllCompiledDirectory() throws MojoExecutionException
173     {
174         String classFoldersTemp = emma.getClassFolders();
175         String[] classFolders;
176         if ( classFoldersTemp == null )
177         {
178             return new String[]
179             { emmaClassFolders };
180         }
181         else
182         {
183             classFolders = classFoldersTemp.split( "," );
184         }
185         ArrayList< String > classFoldersArray = new ArrayList< String >();
186         for ( String folder : classFolders )
187         {
188             File directory = new File( folder );
189             if ( directory.exists() && directory.isDirectory() )
190             {
191                 classFoldersArray.add( directory.getAbsolutePath() );
192             }
193         }
194         return classFoldersArray.toArray( new String[ 0 ] );
195     }
196 }