Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以编程方式调用maven依赖插件_Java_Maven Plugin_Maven 3 - Fatal编程技术网

Java 如何以编程方式调用maven依赖插件

Java 如何以编程方式调用maven依赖插件,java,maven-plugin,maven-3,Java,Maven Plugin,Maven 3,我试图以编程方式调用maven依赖插件。我使用的是maven 3版本。问题是,当我通过pluginManager.executeMojo(会话,执行)调用它时,我收到以下错误消息: [ERROR] **The parameters 'project', 'local', 'remoteRepos', 'reactorProjects' for goal org.apache.maven.plugins:maven-dependency-plugin:2.1:unpack are missing

我试图以编程方式调用maven依赖插件。我使用的是maven 3版本。问题是,当我通过pluginManager.executeMojo(会话,执行)调用它时,我收到以下错误消息:

[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 dependency插件的解包目标所需的唯一参数是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>

com.sap.ldi.demo.calc
com.sap.ldi.demo.calc.cmd
0.1.2-快照
罐子
真的
目标/演示计算内容
**/*.*
谢谢


我这边的一个更正。使用的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调用它,但它仍然返回与上面粘贴的相同的异常。

maven插件不应以编程方式调用

它们依赖于基础丛容器注入的值

因此,要么您必须找到如何注入这些值,要么您必须依赖默认机制

你可以使用的一件事是。有了它,您可以通过编程启动maven生命周期,但它们将在单独的VM中执行。因此,如果需要事先动态更改模型,则需要将模型序列化为临时pom.xml,并与maven invoker一起使用。这是一件很重的事情,但我在大约两年前就成功地完成了。

各位,我想我明白了。 问题不在我使用的Maven版本中。这是我用来调用maven依赖插件的配置。maven dependency插件的解包目标需要以下参数:artifactItemslocalprojectreactorProjectsremoteRepos。以下是用于调用插件的配置的正确版本:

<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>`

com.sap.ldi.demo.calc
com.sap.ldi.demo.calc.cmd
0.1.3-快照
罐子
真的
目标/演示计算内容
**/*.*
${localRepository}
${project}
${reactorProjects}
${project.remoteArtifactRepositories}
`

以下是为Maven 3设计的Mojo Executor的更新版本:

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;
        }
    }
}
package com.googlecode.boostmavenproject;
导入java.util.Collections;
导入org.apache.maven.execution.MavenSession;
导入org.apache.maven.model.Plugin;
导入org.apache.maven.plugin.MojoExecution;
导入org.apache.maven.plugin.MojoExecutionException;
导入org.apache.maven.plugin.descriptor.PluginDescriptor;
导入org.apache.maven.project.MavenProject;
导入org.codehaus.plexus.util.xml.Xpp3Dom;
导入org.apache.maven.plugin.BuildPluginManager;
导入org.apache.maven.plugin.descriptor.MojoDescriptor;
导入org.codehaus.plexus.configuration.PlexusConfiguration;
导入org.codehaus.plexus.util.xml.Xpp3DomUtils;
导入org.sonatype.ether.repository.RemoteRepository;
/**
*使用fluent接口执行任意mojo。这意味着要在Maven 2的上下文中执行
*魔咒。
*
*以下是调用依赖插件的执行:
* 
*行政长官(
*插件(
*groupId(“org.apache.maven.plugins”),
*artifactId(“maven依赖插件”),
*版本(“2.0”)
*              ),
*目标(“复制依赖项”),
*配置(
*元素(名称(“outputDirectory”),“${project.build.directory}/foo”)
*              ),
*执行环境(
*项目,,
*会议,
*插件管理器
*              )
*          );
* 
*@见http://code.google.com/p/mojo-executor/
*/
公共类执行器
{
/**
*执行mojo的入口点
*
*@param plugin要执行的插件
*@param goal要执行的目标
*@param配置执行配置
*@param env执行环境
*@如果有任何查找或执行mojo的异常,则会抛出MojoExecutionException
*/
公共静态void executeMojo(插件、字符串目标、Xpp3Dom配置、,
ExecutionEnvironment)抛出MojoExecutionException
{
if(配置==null)
抛出新的NullPointerException(“配置可能不为null”);
尝试
{
MavenSession session=env.getMavenSession();
PluginDescriptor PluginDescriptor=env.getPluginManager().loadPlugin(插件,
Collections.emptyList(),session.getRepositorySession());
MojoDescriptor mojo=pluginDescriptor.getMojo(目标);
if(mojo==null)
{
抛出新的MojoExecutionException(“在插件中找不到目标“+”目标“+”
+plugin.getGroupId()+“:”
+plugin.getArtifactId()+“:”
+plugin.getVersion());
}
configuration=Xpp3DomUtils.mergeXpp3Dom(配置,
toXpp3Dom(mojo.getMojoConfiguration());
MojoExecution=newmojoexecution(mojo,配置);
env.getPluginM