[WEAVER-15] Ensure that all dependency artifacts are available to commons-weaver mojos regardless of project build status

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/weaver/trunk@1746356 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractCWMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractCWMojo.java
new file mode 100755
index 0000000..b73c96a
--- /dev/null
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractCWMojo.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.commons.weaver.maven;
+
+import java.io.File;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Defines common properties and high-level management common to all commons-weaver Maven goals.
+ */
+abstract class AbstractCWMojo extends AbstractMojo {
+
+    /**
+     * {@code verbose} parameter.
+     */
+    @Parameter(defaultValue = "false")
+    protected boolean verbose;
+
+    /**
+     * {@code weaver.config} parameter.
+     */
+    @Parameter(property = "weaver.config", required = false)
+    protected Properties weaverConfig;
+
+    @Parameter(defaultValue = "${project}")
+    protected MavenProject project;
+
+    /**
+     * Get the classpath for this prepare mojo.
+     * @return {@link List} of {@link String}
+     * @throws DependencyResolutionRequiredException
+     */
+    protected abstract List<String> getClasspath() throws DependencyResolutionRequiredException;
+
+    /**
+     * Get the target directory for this prepare mojo.
+     * @return {@link File}
+     */
+    protected abstract File getTarget();
+
+    /**
+     * Execute this mojo.
+     * @throws MojoExecutionException in the event of failure
+     */
+    @Override
+    public final void execute() throws MojoExecutionException, MojoFailureException {
+        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
+        logRedirector.activate();
+
+        project.setArtifactFilter(new ArtifactFilter() {
+
+            @Override
+            public boolean include(Artifact artifact) {
+                return true;
+            }
+        });
+        try {
+            final List<String> classpath;
+            try {
+                classpath = getClasspath();
+            } catch (DependencyResolutionRequiredException e) {
+                throw new MojoExecutionException("Error getting classpath artifacts", e);
+            }
+            final File target = getTarget();
+            final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
+            
+            getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
+
+            doExecute(target, classpath, config);
+        } finally {
+            logRedirector.deactivate();
+        }
+    }
+
+    protected abstract void doExecute(File target, List<String> classpath, Properties config)
+        throws MojoExecutionException, MojoFailureException;
+
+}
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractPrepareMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractPrepareMojo.java
index 06456fd..a2296a4 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractPrepareMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractPrepareMojo.java
@@ -23,64 +23,23 @@
 import java.util.Properties;
 
 import org.apache.commons.weaver.CleanProcessor;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.Parameter;
 
 /**
- * Defines common properties.
+ * Implements weaver preparation.
  */
-public abstract class AbstractPrepareMojo extends AbstractMojo {
+public abstract class AbstractPrepareMojo extends AbstractCWMojo {
 
-    /**
-     * {@code verbose} parameter.
-     */
-    @Parameter(defaultValue = "false")
-    protected boolean verbose;
-
-    /**
-     * {@code weaver.config} parameter.
-     */
-    @Parameter(property = "weaver.config", required = false)
-    protected Properties weaverConfig;
-
-    /**
-     * Get the classpath for this prepare mojo.
-     * @return {@link List} of {@link String}
-     */
-    protected abstract List<String> getClasspath();
-
-    /**
-     * Get the target directory for this prepare mojo.
-     * @return {@link File}
-     */
-    protected abstract File getTarget();
-
-    /**
-     * Execute this mojo.
-     * @throws MojoExecutionException in the event of failure
-     */
     @Override
-    public void execute() throws MojoExecutionException {
-        if (!getTarget().isDirectory()) {
+    protected void doExecute(File target, List<String> classpath, Properties config) throws MojoExecutionException {
+        if (!target.isDirectory()) {
             return;
         }
-        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
-        logRedirector.activate();
-
-        final List<String> classpath = getClasspath();
-        final File target = getTarget();
-        final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
-
-        getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
-
         try {
             final CleanProcessor cleanProcessor = new CleanProcessor(classpath, target, config);
             cleanProcessor.clean();
         } catch (Exception e) {
             throw new MojoExecutionException("cleaning failed due to " + e.getMessage(), e);
-        } finally {
-            logRedirector.deactivate();
         }
     }
 
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
index beee1b0..6af7116 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
@@ -23,61 +23,20 @@
 import java.util.Properties;
 
 import org.apache.commons.weaver.WeaveProcessor;
-import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.Parameter;
 
 /**
  * Defines common properties.
  */
-public abstract class AbstractWeaveMojo extends AbstractMojo {
+public abstract class AbstractWeaveMojo extends AbstractCWMojo {
 
-    /**
-     * {@code verbose} parameter.
-     */
-    @Parameter(defaultValue = "false")
-    protected boolean verbose;
-
-    /**
-     * {@code weaver.config} parameter.
-     */
-    @Parameter(property = "weaver.config", required = false)
-    protected Properties weaverConfig;
-
-    /**
-     * Get the classpath for this weave mojo.
-     * @return {@link List} of {@link String}
-     */
-    protected abstract List<String> getClasspath();
-
-    /**
-     * Get the target directory for this weave mojo.
-     * @return {@link File}
-     */
-    protected abstract File getTarget();
-
-    /**
-     * Execute this mojo.
-     * @throws MojoExecutionException in the event of failure
-     */
     @Override
-    public void execute() throws MojoExecutionException {
-        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
-        logRedirector.activate();
-
-        final List<String> classpath = getClasspath();
-        final File target = getTarget();
-        final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
-
-        getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
-
+    protected void doExecute(File target, List<String> classpath, Properties config) throws MojoExecutionException {
         try {
             final WeaveProcessor weaveProcessor = new WeaveProcessor(classpath, target, config);
             weaveProcessor.weave();
         } catch (Exception e) {
             throw new MojoExecutionException("weaving failed due to " + e.getMessage(), e);
-        } finally {
-            logRedirector.deactivate();
         }
     }
 
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/PrepareMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/PrepareMojo.java
index abb43e3..c78e67d 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/PrepareMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/PrepareMojo.java
@@ -19,14 +19,17 @@
 package org.apache.commons.weaver.maven;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Set;
 
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.model.Build;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
-import org.apache.maven.project.MavenProject;
 
 /**
  * Goal to clean woven classes.
@@ -34,16 +37,11 @@
 @Mojo(
     name = "prepare",
     defaultPhase = LifecyclePhase.INITIALIZE,
-    requiresDependencyCollection = ResolutionScope.COMPILE
+    requiresDependencyCollection = ResolutionScope.RUNTIME_PLUS_SYSTEM,
+    requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM
 )
 public class PrepareMojo extends AbstractPrepareMojo {
     /**
-     * {@link MavenProject#getCompileClasspathElements()}.
-     */
-    @Parameter(readonly = true, required = true, defaultValue = "${project.compileClasspathElements}")
-    protected List<String> classpath;
-
-    /**
      * {@link Build#getOutputDirectory()}.
      */
     @Parameter(readonly = true, required = true, defaultValue = "${project.build.outputDirectory}")
@@ -53,8 +51,11 @@
      * {@inheritDoc}
      */
     @Override
-    protected List<String> getClasspath() {
-        return classpath;
+    protected List<String> getClasspath() throws DependencyResolutionRequiredException {
+        final Set<String> result = new LinkedHashSet<String>();
+        result.addAll(project.getCompileClasspathElements());
+        result.addAll(project.getRuntimeClasspathElements());
+        return new ArrayList<String>(result);
     }
 
     /**
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestPrepareMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestPrepareMojo.java
index 134385a..79f85a2 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestPrepareMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestPrepareMojo.java
@@ -21,12 +21,12 @@
 import java.io.File;
 import java.util.List;
 
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.model.Build;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
-import org.apache.maven.project.MavenProject;
 
 /**
  * Goal to clean woven test classes.
@@ -34,16 +34,10 @@
 @Mojo(
     name = "test-prepare",
     defaultPhase = LifecyclePhase.INITIALIZE,
-    requiresDependencyCollection = ResolutionScope.TEST
+    requiresDependencyCollection = ResolutionScope.TEST,
+    requiresDependencyResolution = ResolutionScope.TEST
 )
 public class TestPrepareMojo extends AbstractPrepareMojo {
-
-    /**
-     * {@link MavenProject#getTestClasspathElements()}.
-     */
-    @Parameter(readonly = true, required = true, defaultValue = "${project.testClasspathElements}")
-    protected List<String> classpath;
-
     /**
      * {@link Build#getTestOutputDirectory()}.
      */
@@ -54,8 +48,8 @@
      * {@inheritDoc}
      */
     @Override
-    protected List<String> getClasspath() {
-        return classpath;
+    protected List<String> getClasspath() throws DependencyResolutionRequiredException {
+        return project.getTestClasspathElements();
     }
 
     /**
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestWeaveMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestWeaveMojo.java
index aab1310..5500ccb 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestWeaveMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/TestWeaveMojo.java
@@ -21,6 +21,7 @@
 import java.io.File;
 import java.util.List;
 
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.model.Build;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -34,7 +35,8 @@
 @Mojo(
     name = "test-weave",
     defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES,
-    requiresDependencyCollection = ResolutionScope.TEST
+    requiresDependencyCollection = ResolutionScope.TEST,
+    requiresDependencyResolution = ResolutionScope.TEST
 )
 public class TestWeaveMojo extends AbstractWeaveMojo {
 
@@ -54,8 +56,8 @@
      * {@inheritDoc}
      */
     @Override
-    protected List<String> getClasspath() {
-        return classpath;
+    protected List<String> getClasspath() throws DependencyResolutionRequiredException {
+        return project.getTestClasspathElements();
     }
 
     /**
diff --git a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/WeaveMojo.java b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/WeaveMojo.java
index 00844cf..e85128d 100644
--- a/maven-plugin/src/main/java/org/apache/commons/weaver/maven/WeaveMojo.java
+++ b/maven-plugin/src/main/java/org/apache/commons/weaver/maven/WeaveMojo.java
@@ -19,14 +19,17 @@
 package org.apache.commons.weaver.maven;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Set;
 
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.model.Build;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
-import org.apache.maven.project.MavenProject;
 
 /**
  * Goal to weave classes.
@@ -34,17 +37,12 @@
 @Mojo(
     name = "weave",
     defaultPhase = LifecyclePhase.PROCESS_CLASSES,
-    requiresDependencyCollection = ResolutionScope.COMPILE
+    requiresDependencyCollection = ResolutionScope.RUNTIME_PLUS_SYSTEM,
+    requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM
 )
 public class WeaveMojo extends AbstractWeaveMojo {
 
     /**
-     * {@link MavenProject#getCompileClasspathElements()}.
-     */
-    @Parameter(readonly = true, required = true, defaultValue = "${project.compileClasspathElements}")
-    protected List<String> classpath;
-
-    /**
      * {@link Build#getOutputDirectory()}.
      */
     @Parameter(readonly = true, required = true, defaultValue = "${project.build.outputDirectory}")
@@ -54,8 +52,11 @@
      * {@inheritDoc}
      */
     @Override
-    protected List<String> getClasspath() {
-        return classpath;
+    protected List<String> getClasspath() throws DependencyResolutionRequiredException {
+        final Set<String> result = new LinkedHashSet<String>();
+        result.addAll(project.getCompileClasspathElements());
+        result.addAll(project.getRuntimeClasspathElements());
+        return new ArrayList<String>(result);
     }
 
     /**