Author: vmassol
Date: Thu Mar 30 13:38:37 2006
New Revision: 390248

URL: http://svn.apache.org/viewcvs?rev=390248&view=rev
Log:
MCLOVER-28: Add support for multiple source roots
MCLOVER-24: Add support to include/exclude files from instrumentation

Added:
    
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
   (with props)
Modified:
    maven/plugins/trunk/maven-clover-plugin/pom.xml
    
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/pom.xml
    
maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentMojo.java
    maven/plugins/trunk/maven-clover-plugin/src/site/apt/howto.apt

Modified: maven/plugins/trunk/maven-clover-plugin/pom.xml
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-clover-plugin/pom.xml?rev=390248&r1=390247&r2=390248&view=diff
==============================================================================
--- maven/plugins/trunk/maven-clover-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-clover-plugin/pom.xml Thu Mar 30 13:38:37 2006
@@ -69,12 +69,17 @@
       <artifactId>maven-artifact</artifactId>
       <version>2.0</version>
     </dependency>
-      <dependency>
-        <groupId>jmock</groupId>
-        <artifactId>jmock</artifactId>
-        <version>1.0.1</version>
-        <scope>test</scope>
-      </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-compiler-api</artifactId>
+      <version>1.5.2</version>
+    </dependency>
+    <dependency>
+      <groupId>jmock</groupId>
+      <artifactId>jmock</artifactId>
+      <version>1.0.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <!--
   <build>

Modified: 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/pom.xml
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/pom.xml?rev=390248&r1=390247&r2=390248&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/pom.xml
 (original)
+++ 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/pom.xml
 Thu Mar 30 13:38:37 2006
@@ -36,6 +36,9 @@
         <configuration>
           <jdk>1.4</jdk>
           <targetPercentage>1%</targetPercentage>
+          <excludes>
+            <exclude>**/*Dummy*.java</exclude>
+          </excludes>
         </configuration>
         <executions>
           <execution>

Added: 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java?rev=390248&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
 (added)
+++ 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
 Thu Mar 30 13:38:37 2006
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.maven.plugin.clover.samples.simple;
+
+public class Dummy
+{
+    public void dummyMethod()
+    {
+        int i = 0;
+    }
+}

Propchange: 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-clover-plugin/src/it/maven-clover-plugin-sample-simple/src/main/java/org/apache/maven/plugin/clover/samples/simple/Dummy.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentMojo.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentMojo.java?rev=390248&r1=390247&r2=390248&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentMojo.java
 Thu Mar 30 13:38:37 2006
@@ -21,13 +21,13 @@
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
+import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
+import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
+import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Instrument source roots.
@@ -70,6 +70,18 @@
      */
     private ArtifactFactory factory;
 
+    /**
+     * The list of file to include in the instrumentation.
+     * @parameter
+     */
+    private Set includes = new HashSet();
+
+    /**
+     * The list of file to exclude from the instrumentation.
+     * @parameter
+     */
+    private Set excludes = new HashSet();
+
     private String cloverOutputSourceDirectory;
 
     public void execute()
@@ -82,17 +94,25 @@
 
             super.execute();
 
-            instrumentSources();
-            addCloverDependencyToCompileClasspath();
-            redirectSourceDirectories();
-            redirectOutputDirectories();
+            Set filesToInstrument = computeFilesToInstrument();
+            if ( filesToInstrument.isEmpty() )
+            {
+                getLog().warn("No Clover instrumentation done as no matching 
sources files found");
+            }
+            else
+            {
+                instrumentSources( filesToInstrument );
+                addCloverDependencyToCompileClasspath();
+                redirectSourceDirectories();
+                redirectOutputDirectories();
+            }
         }
     }
 
     private boolean shouldExecute()
     {
         boolean shouldExecute = true;
-        
+
         // Only execute reports for java projects
         ArtifactHandler artifactHandler = 
this.project.getArtifact().getArtifactHandler();
         File srcDir = new File(this.project.getBuild().getSourceDirectory());
@@ -110,10 +130,10 @@
 
         return shouldExecute;
     }
-    
-    private void instrumentSources() throws MojoExecutionException
+
+    private void instrumentSources(Set filesToInstrument) throws 
MojoExecutionException
     {
-        int result = CloverInstr.mainImpl( createCliArgs() );
+        int result = CloverInstr.mainImpl( createCliArgs( filesToInstrument ) 
);
         if ( result != 0 )
         {
             throw new MojoExecutionException( "Clover has failed to instrument 
the source files" );
@@ -138,7 +158,7 @@
         String oldSourceDirectory = 
this.project.getBuild().getSourceDirectory();
 
         this.project.getBuild().setSourceDirectory( 
this.cloverOutputSourceDirectory );
-        
+
         // Maven2 limitation: changing the source directory doesn't change the 
compile source roots
         // See http://jira.codehaus.org/browse/MNG-1945
         List sourceRoots = this.project.getCompileSourceRoots();
@@ -155,7 +175,7 @@
             }
         }
     }
-    
+
     private void addCloverDependencyToCompileClasspath()
         throws MojoExecutionException
     {
@@ -186,13 +206,56 @@
     }
 
     /**
+     * @return the list of files to instrument taking into account the 
includes and excludes specified by the user
+     */
+    private Set computeFilesToInstrument()
+    {
+        Set filesToInstrument = new HashSet();
+
+        SourceInclusionScanner scanner = null;
+
+        if ( includes.isEmpty() && excludes.isEmpty() )
+        {
+            includes = Collections.singleton( "**/*.java" );
+            scanner = new SimpleSourceInclusionScanner( includes, 
Collections.EMPTY_SET );
+        }
+        else
+        {
+            if ( includes.isEmpty() )
+            {
+                includes.add( "**/*.java" );
+            }
+            scanner = new SimpleSourceInclusionScanner( includes, excludes );
+        }
+
+        // Note: we shouldn't have to do this but this is a limitation of the 
Plexus SimpleSourceInclusionScanner
+        scanner.addSourceMapping(new SuffixMapping("dummy", "dummy"));
+
+        Iterator roots = this.project.getCompileSourceRoots().iterator();
+        while (roots.hasNext())
+        {
+            String sourceRoot = (String) roots.next();
+            try
+            {
+                filesToInstrument.addAll(scanner.getIncludedSources(new 
File(sourceRoot), null));
+            }
+            catch (InclusionScanException e)
+            {
+                getLog().warn("Failed to add sources from [" + sourceRoot + 
"]", e);
+            }
+        }
+
+        return filesToInstrument;
+    }
+
+    /**
      * @return the CLI args to be passed to CloverInstr
      * @todo handle multiple source roots. At the moment only the first source 
root is instrumented
      */
-    private String[] createCliArgs() throws MojoExecutionException
+    private String[] createCliArgs(Set filesToInstrument) throws 
MojoExecutionException
     {
         List parameters = new ArrayList();
-     
+
         parameters.add( "-p" );
         parameters.add( this.flushPolicy );
         parameters.add( "-f" );
@@ -200,10 +263,6 @@
 
         parameters.add( "-i" );
         parameters.add( this.cloverDatabase );
-        parameters.add( "-s" );
-
-        // TODO: Allow support for several source roots in the future.
-        parameters.add( (String) this.project.getCompileSourceRoots().get( 0 ) 
);
 
         parameters.add( "-d" );
         parameters.add( this.cloverOutputSourceDirectory );
@@ -220,13 +279,19 @@
             }
             else
             {
-                throw new MojoExecutionException("Unsupported jdk version [" + 
this.jdk 
+                throw new MojoExecutionException("Unsupported jdk version [" + 
this.jdk
                     + "]. Valid values are [1.4] and [1.5]");
             }
         }
-        
+
+        for ( Iterator files = filesToInstrument.iterator(); files.hasNext(); )
+        {
+            File file = (File) files.next();
+            parameters.add( file.getPath() );
+        }
+
         getLog().debug( "Instrumenting using parameters [" + 
parameters.toString() + "]");
-        
+
         return (String[]) parameters.toArray(new String[0]);
     }
 }

Modified: maven/plugins/trunk/maven-clover-plugin/src/site/apt/howto.apt
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-clover-plugin/src/site/apt/howto.apt?rev=390248&r1=390247&r2=390248&view=diff
==============================================================================
--- maven/plugins/trunk/maven-clover-plugin/src/site/apt/howto.apt (original)
+++ maven/plugins/trunk/maven-clover-plugin/src/site/apt/howto.apt Thu Mar 30 
13:38:37 2006
@@ -157,3 +157,25 @@
 
   Note that you can control the location of the aggregated Clover database by 
using the <<<cloverMergeDatabase>>>
   configuration property.
+
+Controlling files to instrument
+
+  By default all Java files are included during the instrumentation. To 
specify inclusion and exclusion use the
+  <<<includes>>> and <<<excludes>>> configuration elements as shown in this 
example:
+
++--------
+  <plugin>
+    <groupId>org.apache.maven.plugins</groupId>
+    <artifactId>maven-clover-plugin</artifactId>
+    <configuration>
+      <includes>
+        <include>**/api/**/*.java</include>
+        <include>some/path/MyFile.java</include>
+        [...]
+      </includes>
+      <excludes>
+        <exclude>**/*Test/java</exclude>
+        [...]
+      </excludes>
+[...]
++---------


Reply via email to