如何以编程方式调用 Maven 依赖插件
我正在尝试以编程方式调用 maven-dependency-plugin 。我正在使用 Maven 3 版本。问题是,当我通过pluginManager.executeMojo(session,execution)调用它时,我收到以下错误消息:
[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 调用它,但它仍然返回与上面粘贴的相同的异常。
I am trying to invoke maven-dependency-plugin programatically. i am using maven 3 version. the problem is that when i invoke it through pluginManager.executeMojo(session, execution), i receive the following error message:
[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 --
As I know, the only required parameter for the unpack goal of maven dependency plugin is artifactItems. I set the plugin configuration by using PluginExecution.setConfiguration() method. It seems that this plugin configuration is not correctly set.
Do you have any idea why this exception is thrown?
Here is the configuration that I am using:
<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>
Thanks
One correction from my side. The used Maven version is not Maven 3.0 but Maven 3.0-beta-1. I see that BuildPluginManager.loadPlugin()
in version 3.0-beta-1 has two args, and the same method in version 3.0 has three.
I am wondering, does anyone tried to invoke a maven plugin programatically with maven 3.0 or maven 3.0-beta-1. I am still trying to invoke it with maven 3.0-beta-1, but it still returns the same exception as pasted above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是为 Maven 3 设计的 Mojo Executor 的更新版本:
我将尝试将我的更改贡献回官方 Mojo Executor 插件。
Here is an updated version of Mojo Executor designed for Maven 3:
I will attempt to contribute my changes back into the official Mojo Executor plugin.
伙计们,我想我明白了。
问题不在于我使用的 Maven 版本。它位于我用于调用 maven-dependency-plugin 的配置中。 maven-dependency-plugin 的解包目标需要以下参数:artifactItems、local、project、reactorProjects 和远程存储库。以下是用于调用插件的正确配置版本:
Folks, I think I get it.
The problem is not in the version of Maven that I am using. It is in the configuration that I am using for invoking maven-dependency-plugin. The unpack goal of maven-dependency-plugin requires the following parameters: artifactItems, local, project, reactorProjects and remoteRepos. Here is the correct version of the configuration used for invoking the plugin:
Maven 插件并不意味着以编程方式调用。
它们依赖于底层 plexus 容器注入的值。
因此,要么您必须找出如何注入这些值,要么必须依赖默认机制。
您可以使用的一件事是 Maven Invoker。这样,您就可以以编程方式启动 Maven 生命周期,但它们将在单独的虚拟机中执行。因此,如果您需要预先动态更改模型,则需要将模型序列化为临时 pom.xml 并将其与 Maven 调用程序一起使用。这是一件很繁重的事情,但我大约两年前就成功地完成了。
Maven Plugins are not meant to be invoked programmatically.
They rely on values that are injected by the underlying plexus container.
So either you will have to find out how to inject those values or you will have to rely on the default mechanism.
One thing you can use is the Maven Invoker. With that, you can programmatically launch maven lifecycles, but they will execute in a separate VM. So if you need to change the model dynamically beforehand, you will need to serialize the model out to a temporary pom.xml and use that with maven invoker. This is heavy stuff, but I have done it successfully some two years ago.