2010-11-22 24 views
6

我想以編程方式調用maven-dependency-plugin。我正在使用maven 3版本。問題是,當我通過pluginManager.executeMojo(會話,執行)調用它,我收到以下錯誤信息:如何以編程方式調用Maven依賴項插件

[ERROR] **The parameters 'project', 'local', 'remoteRepos', 
'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack are 
missing or invalid** 
**org.apache.maven.plugin.PluginParameterException: The parameters 'project', 
'local', 'remoteRepos', 'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack are missing or 
invalid** 
     at org.apache.maven.plugin.internal.DefaultMavenPluginManager 
     .populatePluginFields(DefaultMavenPluginManager.java:518) 
     at org.apache.maven.plugin.internal.DefaultMavenPluginManager 
     .getConfiguredMojo(DefaultMavenPluginManager.java:471) 
     at org.apache.maven.plugin.DefaultBuildPluginManager 
     .executeMojo(DefaultBuildPluginManager.java:99) 
     at com.sap.ldi.qi.osgi.OSGiManifesrMfHandlerMojo 
     .invokeMavenDependencyPlugin(OSGiManifesrMfHandlerMojo.java:139) 
     at com.sap.ldi.qi.osgi.OSGiManifesrMfHandlerMojo 
     .execute(OSGiManifesrMfHandlerMojo.java:100) 
     at org.apache.maven.plugin.DefaultBuildPluginManager 
     .executeMojo(DefaultBuildPluginManager.java:110) 
     at org.apache.maven.lifecycle.internal.MojoExecutor 
     .execute(MojoExecutor.java:144) 
     at org.apache.maven.lifecycle.internal.MojoExecutor 
     .execute(MojoExecutor.java:87) 
     at org.apache.maven.lifecycle.internal.MojoExecutor 
     .execute(MojoExecutor.java:79) 
-- many lines stripped from stack trace -- 
[INFO] ---------------------------------------------------------------------- 
[INFO] BUILD FAILURE 
[INFO] ---------------------------------------------------------------------- 
[INFO] Total time: 17.938s 
[INFO] Finished at: Mon Nov 22 10:27:42 EET 2010 
[INFO] Final Memory: 12M/23M 
[INFO] ---------------------------------------------------------------------- 
[ERROR] Failed to execute goal 
com.sap.ldi.qi:osgi-manifest-handler-plugin:0.0.1-SNAPSHOT:handle 
(osgi-manifest-handler plugin) on project com.sap.ldi.demo.calc 
.cmd.tests: The parameters 'project', 'local', 'remoteRepos', 
'reactorProjects' for goal 
org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack are missing 
or invalid -> [Help 1] 
-- stripped rest -- 

據我所知,Maven的依賴插件的解壓目標的唯一需要的參數是artifactItems 。我使用PluginExecution.setConfiguration()方法設置插件配置。看來這個插件配置沒有正確設置。 你知道爲什麼拋出這個異常嗎?

這裏是我使用的配置:

<configuration> 
    <artifactItems> 
    <artifactItem> 
     <groupId>com.sap.ldi.demo.calc</groupId> 
     <artifactId>com.sap.ldi.demo.calc.cmd</artifactId> 
     <version>0.1.2-SNAPSHOT</version> 
     <type>jar</type> 
     <overWrite>true</overWrite> 
     <outputDirectory>target/demo-calc-stuff</outputDirectory> 
     <includes>**/*.*</includes> 
    </artifactItem> 
    </artifactItems> 
</configuration> 

感謝

從我身邊

一個修正。使用的Maven版本不是Maven 3.0,而是Maven 3.0-beta-1。我發現版本3.0-beta-1中的BuildPluginManager.loadPlugin()有兩個參數,3.0版本中的相同方法有三個參數。 我想知道,沒有人試圖用maven 3.0或maven 3.0-beta-1以編程方式調用maven插件。我仍然試圖用maven 3.0-beta-1調用它,但它仍然返回與上面粘貼的相同的異常。

回答

0

Maven插件並不意味着以編程方式調用。

它們依賴於由底層叢容器注入的值。

所以要麼你必須找出如何注入這些值,或者你將不得不依靠默認機制。

您可以使用的一件事是Maven Invoker。有了這個,你可以以編程方式啓動Maven生命週期,但它們將在單獨的VM中執行。因此,如果您需要事先動態更改模型,則需要將該模型序列化爲臨時pom.xml,並將其用於maven調用程序。這是很重要的東西,但是我在兩年前成功地完成了它。

+1

我認爲BuildPluginManager.executeMojo()適用於這種用例。我還找到了以下鏈接,解釋瞭如何以編程方式調用Maven插件:http://code.google。com/p/mojo-executor/ – moisko 2010-11-22 09:43:08

+0

看來,使用的實現是爲maven 2,但唐布朗使用相同的想法,如上所述 – moisko 2010-11-22 09:44:54

+0

http://stackoverflow.com/questions/526733/maven-plugin-executing-另一個插件 - 這是我正在討論的帖子的鏈接。 – moisko 2010-11-22 09:50:55

2

人們,我想我明白了。 問題不在我使用的Maven版本中。這是我用來調用maven-dependency-plugin的配置。 Maven的依賴,插件的解包目標需要下列參數:artifactItems當地項目reactorProjectsremoteRepos。下面是用於調用插件配置的正確版本:

<configuration> 
    <artifactItems> 
    <artifactItem> 
     <groupId>com.sap.ldi.demo.calc</groupId> 
     <artifactId>com.sap.ldi.demo.calc.cmd</artifactId> 
     <version>0.1.3-SNAPSHOT</version> 
     <type>jar</type> 
     <overWrite>true</overWrite> 
     <outputDirectory>target/demo-calc-stuff</outputDirectory> 
     <includes>**/*.*</includes> 
    </artifactItem> 
    </artifactItems> 
    <local>${localRepository}</local> 
    <project>${project}</project> 
    <reactorProjects>${reactorProjects}</reactorProjects> 
    <remoteRepos>${project.remoteArtifactRepositories}</remoteRepos> 
</configuration>` 
+0

哇,看起來不錯(+1) – 2010-11-23 15:08:40

+0

問題是,Maven告訴你,你沒有使用調用maven-dependency-plugin所需的完整參數集合:解壓縮,但它不是很容易找到什麼是這些標籤元素的可能值。我必須通過調試模式下的pom.xml調用插件才能找到這些值並構建配置。 – moisko 2010-11-23 15:32:48

10

這裏是魔執行人的更新版本設計的Maven 3:

package com.googlecode.boostmavenproject; 

import java.util.Collections; 
import org.apache.maven.execution.MavenSession; 
import org.apache.maven.model.Plugin; 
import org.apache.maven.plugin.MojoExecution; 
import org.apache.maven.plugin.MojoExecutionException; 
import org.apache.maven.plugin.descriptor.PluginDescriptor; 
import org.apache.maven.project.MavenProject; 
import org.codehaus.plexus.util.xml.Xpp3Dom; 
import org.apache.maven.plugin.BuildPluginManager; 
import org.apache.maven.plugin.descriptor.MojoDescriptor; 
import org.codehaus.plexus.configuration.PlexusConfiguration; 
import org.codehaus.plexus.util.xml.Xpp3DomUtils; 
import org.sonatype.aether.repository.RemoteRepository; 

/** 
* Executes an arbitrary mojo using a fluent interface. This is meant to be executed within the context of a Maven 2 
* mojo. 
* 
* Here is an execution that invokes the dependency plugin: 
* <pre> 
* executeMojo(
*    plugin(
*      groupId("org.apache.maven.plugins"), 
*      artifactId("maven-dependency-plugin"), 
*      version("2.0") 
*    ), 
*    goal("copy-dependencies"), 
*    configuration(
*      element(name("outputDirectory"), "${project.build.directory}/foo") 
*    ), 
*    executionEnvironment(
*      project, 
*      session, 
*      pluginManager 
*    ) 
*   ); 
* </pre> 
* @see http://code.google.com/p/mojo-executor/ 
*/ 
public class MojoExecutor 
{ 
    /** 
    * Entry point for executing a mojo 
    * 
    * @param plugin The plugin to execute 
    * @param goal The goal to execute 
    * @param configuration The execution configuration 
    * @param env The execution environment 
    * @throws MojoExecutionException If there are any exceptions locating or executing the mojo 
    */ 
    public static void executeMojo(Plugin plugin, String goal, Xpp3Dom configuration, 
                   ExecutionEnvironment env) throws MojoExecutionException 
    { 
     if (configuration == null) 
      throw new NullPointerException("configuration may not be null"); 
     try 
     { 
      MavenSession session = env.getMavenSession(); 

      PluginDescriptor pluginDescriptor = env.getPluginManager().loadPlugin(plugin, 
       Collections.<RemoteRepository>emptyList(), session.getRepositorySession()); 
      MojoDescriptor mojo = pluginDescriptor.getMojo(goal); 
      if (mojo == null) 
      { 
       throw new MojoExecutionException("Could not find goal '" + goal + "' in plugin " 
                       + plugin.getGroupId() + ":" 
                       + plugin.getArtifactId() + ":" 
                       + plugin.getVersion()); 
      } 
      configuration = Xpp3DomUtils.mergeXpp3Dom(configuration, 
       toXpp3Dom(mojo.getMojoConfiguration())); 
      MojoExecution exec = new MojoExecution(mojo, configuration); 
      env.getPluginManager().executeMojo(session, exec); 
     } 
     catch (Exception e) 
     { 
      throw new MojoExecutionException("Unable to execute mojo", e); 
     } 
    } 

    /** 
    * Converts PlexusConfiguration to a Xpp3Dom. 
    * 
    * @param config the PlexusConfiguration 
    * @return the Xpp3Dom representation of the PlexusConfiguration 
    */ 
    private static Xpp3Dom toXpp3Dom(PlexusConfiguration config) 
    { 
     Xpp3Dom result = new Xpp3Dom(config.getName()); 
     result.setValue(config.getValue(null)); 
     for (String name: config.getAttributeNames()) 
      result.setAttribute(name, config.getAttribute(name)); 
     for (PlexusConfiguration child: config.getChildren()) 
      result.addChild(toXpp3Dom(child)); 
     return result; 
    } 

    /** 
    * Constructs the {@link ExecutionEnvironment} instance fluently 
    * @param mavenProject The current Maven project 
    * @param mavenSession The current Maven session 
    * @param pluginManager The Build plugin manager 
    * @return The execution environment 
    * @throws NullPointerException if mavenProject, mavenSession or pluginManager 
    * are null 
    */ 
    public static ExecutionEnvironment executionEnvironment(MavenProject mavenProject, 
                                MavenSession mavenSession, 
                                BuildPluginManager pluginManager) 
    { 
     return new ExecutionEnvironment(mavenProject, mavenSession, pluginManager); 
    } 

    /** 
    * Builds the configuration for the goal using Elements 
    * @param elements A list of elements for the configuration section 
    * @return The elements transformed into the Maven-native XML format 
    */ 
    public static Xpp3Dom configuration(Element... elements) 
    { 
     Xpp3Dom dom = new Xpp3Dom("configuration"); 
     for (Element e: elements) 
      dom.addChild(e.toDom()); 
     return dom; 
    } 

    /** 
    * Defines the plugin without its version 
    * @param groupId The group id 
    * @param artifactId The artifact id 
    * @return The plugin instance 
    */ 
    public static Plugin plugin(String groupId, String artifactId) 
    { 
     return plugin(groupId, artifactId, null); 
    } 

    /** 
    * Defines a plugin 
    * @param groupId The group id 
    * @param artifactId The artifact id 
    * @param version The plugin version 
    * @return The plugin instance 
    */ 
    public static Plugin plugin(String groupId, String artifactId, String version) 
    { 
     Plugin plugin = new Plugin(); 
     plugin.setArtifactId(artifactId); 
     plugin.setGroupId(groupId); 
     plugin.setVersion(version); 
     return plugin; 
    } 

    /** 
    * Wraps the group id string in a more readable format 
    * @param groupId The value 
    * @return The value 
    */ 
    public static String groupId(String groupId) 
    { 
     return groupId; 
    } 

    /** 
    * Wraps the artifact id string in a more readable format 
    * @param artifactId The value 
    * @return The value 
    */ 
    public static String artifactId(String artifactId) 
    { 
     return artifactId; 
    } 

    /** 
    * Wraps the version string in a more readable format 
    * @param version The value 
    * @return The value 
    */ 
    public static String version(String version) 
    { 
     return version; 
    } 

    /** 
    * Wraps the goal string in a more readable format 
    * @param goal The value 
    * @return The value 
    */ 
    public static String goal(String goal) 
    { 
     return goal; 
    } 

    /** 
    * Wraps the element name string in a more readable format 
    * @param name The value 
    * @return The value 
    */ 
    public static String name(String name) 
    { 
     return name; 
    } 

    /** 
    * Constructs the element with a textual body 
    * @param name The element name 
    * @param value The element text value 
    * @return The element object 
    */ 
    public static Element element(String name, String value) 
    { 
     return new Element(name, value); 
    } 

    /** 
    * Constructs the element containg child elements 
    * @param name The element name 
    * @param elements The child elements 
    * @return The Element object 
    */ 
    public static Element element(String name, Element... elements) 
    { 
     return new Element(name, elements); 
    } 

    /** 
    * Element wrapper class for configuration elements 
    */ 
    public static class Element 
    { 
     private final Element[] children; 
     private final String name; 
     private final String text; 

     public Element(String name, Element... children) 
     { 
      this(name, null, children); 
     } 

     public Element(String name, String text, Element... children) 
     { 
      this.name = name; 
      this.text = text; 
      this.children = children; 
     } 

     public Xpp3Dom toDom() 
     { 
      Xpp3Dom dom = new Xpp3Dom(name); 
      if (text != null) 
      { 
       dom.setValue(text); 
      } 
      for (Element e: children) 
      { 
       dom.addChild(e.toDom()); 
      } 
      return dom; 
     } 
    } 

    /** 
    * Collects Maven execution information 
    */ 
    public static class ExecutionEnvironment 
    { 
     private final MavenProject mavenProject; 
     private final MavenSession mavenSession; 
     private final BuildPluginManager pluginManager; 

     public ExecutionEnvironment(MavenProject mavenProject, MavenSession mavenSession, 
                   BuildPluginManager pluginManager) 
     { 
      if (mavenProject == null) 
       throw new NullPointerException("mavenProject may not be null"); 
      if (mavenSession == null) 
       throw new NullPointerException("mavenSession may not be null"); 
      if (pluginManager == null) 
       throw new NullPointerException("pluginManager may not be null"); 
      this.mavenProject = mavenProject; 
      this.mavenSession = mavenSession; 
      this.pluginManager = pluginManager; 
     } 

     public MavenProject getMavenProject() 
     { 
      return mavenProject; 
     } 

     public MavenSession getMavenSession() 
     { 
      return mavenSession; 
     } 

     public BuildPluginManager getPluginManager() 
     { 
      return pluginManager; 
     } 
    } 
} 

我將嘗試有助於我變回進入官方的Mojo Executor插件。

+6

我是Mojo Executor庫的當前維護者。 Gili與我取得聯繫,讓我知道他的補丁版本。現在已經在https://github.com/TimMoore/mojo-executor併入項目的主分支,並且已經部署爲版本2.0-SNAPSHOT 。如果有任何Maven 3用戶可以測試它並讓我知道是否有任何問題(最好是通過在GitHub站點上提交問題),那真的很有幫助。謝謝! – 2011-04-25 03:58:32

+0

是的,Mojo Executor插件非常棒! – JodaStephen 2013-06-17 10:05:28

+0

@Gili嗨你能告訴我如何初始化這些領域? MavenProject mavenProject; MavenSession mavenSession; BuildPluginManager pluginManager; – 2015-07-01 05:34:40

相關問題