You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Andreas Gies <an...@wayofquality.de> on 2010/10/12 19:06:47 UTC

Invoking a plugin programmatically with Maven 3.0-beta

  Hello Maveners ,

mainly for self learning purposes I am trying to build some plugins for 
Maven 3 using the Scala language.
One of the things I had going before (Maven 2 & Java based) is to invoke 
another plugin programmatically.

Now I am trying to invoke the dependency plugin, namely the 
unpack-dependencies goal and am running
into a rather cryptic error message:

[ERROR] Failed to execute goal 
com.progress.maven.plugins:plugin-sandbox:8.0-SNAPSHOT:echo 
(default-cli) on project test-plugin: The parameters 'proje
ct', 'local', 'remoteRepos', 'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack-dependencies 
are missing or invali
d -> [Help 1]

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to 
execute goal com.progress.maven.plugins:plugin-sandbox:8.0-SNAPSHOT:echo 
(default-cl
i) on project test-plugin: The parameters 'project', 'local', 
'remoteRepos', 'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack-dependencies 
are missing or invalid
         at 
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:157)
         at 
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:88)
         at 
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:80)
         at 
org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:87)
         at 
org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
         at 
org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
         at 
org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:315)
         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
         at org.apache.maven.cli.MavenCli.execute(MavenCli.java:445)
         at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:168)
         at org.apache.maven.cli.MavenCli.main(MavenCli.java:132)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:597)
         at 
org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
         at 
org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
         at 
org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
         at 
org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.PluginParameterException: The 
parameters 'project', 'local', 'remoteRepos', 'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack-dependencies 
are missing or invalid
         at 
org.apache.maven.plugin.internal.DefaultMavenPluginManager.populatePluginFields(DefaultMavenPluginManager.java:514)
         at 
org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:467)
         at 
org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:96)
         at de.woq.maven.plugins.TestMojo.execute(TestMojo.scala:109)
         at 
org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:107)
         at 
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
         ... 19 more

It seems that the current project is not passed correctly to the plugin. 
I have noticed that the API for invoking plugins has slightly changed, 
but was unable to get more information.
Any hints where to dig deeper would be greatly appreciated.

For completeness here is the code of my plugin so far .... (As I said 
its a learning exercise :))

/*
  * Copyright (C) 2010, Way of Quality
  * All rights reserved.
  *
  * 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 de.woq.maven.plugins

import org.apache.maven.plugin._
import descriptor.MojoDescriptor
import org.scala_tools.maven.mojo.annotations._
import org.apache.maven.project.MavenProject
import org.apache.maven.execution.MavenSession
import org.apache.maven.model.Plugin

import scala.collection.JavaConversions._
import org.codehaus.plexus.component.annotations.Requirement
import org.codehaus.plexus.util.xml.Xpp3Dom

@goal("echo")
@phase("process-sources")
@requiresProject
class TestMojo extends AbstractMojo {

   @readOnly @parameter @expression("${project}")
   var project : MavenProject = _

   @readOnly @parameter @expression("${session}")
   var session : MavenSession = _

   @Requirement
   var pluginManager: BuildPluginManager = _

   private def getBuildPlugin(groupId: String, artifactId: String, 
version: Option[String]) = {

     
List.fromIterator(project.getBuildPlugins.iterator).asInstanceOf[List[Plugin]]
       .filter(
          p => (p.getGroupId.equals(groupId)) && 
(p.getArtifactId.equals(artifactId) && (version == None || 
p.getVersion.equals(version)))
       )
   }

   def resolvePluginManager = {
     
session.getContainer.lookup("org.apache.maven.plugin.BuildPluginManager").asInstanceOf[BuildPluginManager]
   }

   def buildConfiguration(params: Map[String, String]): Xpp3Dom = {
     var config = new Xpp3Dom("configuration")

     for{
       key <- params.keys
       value = params.get(key)
     } {
       var child = new Xpp3Dom(key)
       child.setValue(
         params.get(key) match {
           case Some(v) => v
           case None => ""
         }
       )
       config.addChild(child)
     }

     println(config.toUnescapedString)
     config
   }

   override def execute = {

     val plugins = getBuildPlugin("org.apache.maven.plugins", 
"maven-dependency-plugin", None)
     assert(plugins.length == 1, "Found " + plugins.length + " plugins 
for dependdency resolution, expected 1")

     val dep: Plugin = plugins.head

     getLog.info("==============================")

     // needed as component injection seems to be broken in scala 
plugins ...
     pluginManager = resolvePluginManager

     assert(project != null)
     assert(session != null)
     assert(pluginManager != null)

     var config : Xpp3Dom = buildConfiguration(Map("outputDirectory" -> 
"test"))

     var md : MojoDescriptor = pluginManager.getMojoDescriptor(
       dep, "unpack-dependencies", project.getRemotePluginRepositories, 
session.getRepositorySession
     )

     var execution : MojoExecution = new MojoExecution(md, config)

     assert(md != null)

     pluginManager.executeMojo(session, execution)    // <==== fails 
here ....

     getLog.info("==============================")
   }
}


Thanks and best regards
Andreas

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org