在 Mavenplugin 内调用 Maven 目标

发布于 2024-10-17 07:52:36 字数 4210 浏览 1 评论 0原文

我想编写一个 Maven 插件,它在一组项目上执行一些特定的 Maven 目标。

目前我的插件 pom:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
    <version>2.0.11</version>
</dependency>

和插件代码中

InvocationRequest request = new DefaultInvocationRequest();
request.setBaseDirectory(file.getParentFile());
request.setGoals(newArrayList("clean", "generate-sources"));
request.setPomFile(file);
Inoker invoker = new DefaultInvoker();
InvocationResult execute = invoker.execute(request);

如果我在 Maven 项目上运行它,它会失败,

[FATAL ERROR] de.test.builder.MyBuilder#execute() caused a linkage error (java.lang.NoSuchMethodError) and may be out-of-date. Check the realms:
[FATAL ERROR] Plugin realm = app0.child-container[de.test.plugins:maven-builder-plugin:0.1-SNAPSHOT]
urls[0] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/de/test/plugins/maven-builder-plugin/0.1-SNAPSHOT/maven-builder-plugin-
.1-SNAPSHOT.jar
urls[1] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
urls[2] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/org/apache/maven/shared/maven-invoker/2.0.11/maven-invoker-2.0.11.jar
urls[3] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/com/google/collections/google-collections/1.0/google-collections-1.0.jar
[FATAL ERROR] Container realm = plexus.core
urls[0] = file:/d:/development/build-tools/apache-maven-2.2.1/lib/maven-2.2.1-uber.jar
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] org.codehaus.plexus.util.cli.Commandline.createArg()Lorg/codehaus/plexus/util/cli/Arg;
[INFO] ------------------------------------------------------------------------
[INFO] Trace
java.lang.NoSuchMethodError: org.codehaus.plexus.util.cli.Commandline.createArg()Lorg/codehaus/plexus/util/cli/Arg;
        at org.apache.maven.shared.invoker.MavenCommandLineBuilder.setFlags(MavenCommandLineBuilder.java:407)
        at org.apache.maven.shared.invoker.MavenCommandLineBuilder.build(MavenCommandLineBuilder.java:83)
        at org.apache.maven.shared.invoker.DefaultInvoker.execute(DefaultInvoker.java:91)
        at de.test.builder.Builder.execute(Builder.java:46)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        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.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

有人能告诉我为什么会失败吗?以及如何实现我的实际目标?

感谢您的帮助

I want to write a maven plugin which executes some specific maven goals on a set of projects.

Currently I have in my plugins pom:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
    <version>2.0.11</version>
</dependency>

and in the plugins code

InvocationRequest request = new DefaultInvocationRequest();
request.setBaseDirectory(file.getParentFile());
request.setGoals(newArrayList("clean", "generate-sources"));
request.setPomFile(file);
Inoker invoker = new DefaultInvoker();
InvocationResult execute = invoker.execute(request);

If I ran it on a maven project it fails with

[FATAL ERROR] de.test.builder.MyBuilder#execute() caused a linkage error (java.lang.NoSuchMethodError) and may be out-of-date. Check the realms:
[FATAL ERROR] Plugin realm = app0.child-container[de.test.plugins:maven-builder-plugin:0.1-SNAPSHOT]
urls[0] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/de/test/plugins/maven-builder-plugin/0.1-SNAPSHOT/maven-builder-plugin-
.1-SNAPSHOT.jar
urls[1] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
urls[2] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/org/apache/maven/shared/maven-invoker/2.0.11/maven-invoker-2.0.11.jar
urls[3] = file:/C:/Dokumente und Einstellungen/user/.m2/repository/com/google/collections/google-collections/1.0/google-collections-1.0.jar
[FATAL ERROR] Container realm = plexus.core
urls[0] = file:/d:/development/build-tools/apache-maven-2.2.1/lib/maven-2.2.1-uber.jar
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] org.codehaus.plexus.util.cli.Commandline.createArg()Lorg/codehaus/plexus/util/cli/Arg;
[INFO] ------------------------------------------------------------------------
[INFO] Trace
java.lang.NoSuchMethodError: org.codehaus.plexus.util.cli.Commandline.createArg()Lorg/codehaus/plexus/util/cli/Arg;
        at org.apache.maven.shared.invoker.MavenCommandLineBuilder.setFlags(MavenCommandLineBuilder.java:407)
        at org.apache.maven.shared.invoker.MavenCommandLineBuilder.build(MavenCommandLineBuilder.java:83)
        at org.apache.maven.shared.invoker.DefaultInvoker.execute(DefaultInvoker.java:91)
        at de.test.builder.Builder.execute(Builder.java:46)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        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.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

Can someone tell me why it fails ? And how to achieve my actual goal ?

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-10-24 07:52:36

让我简单介绍一下我认为您想要做的事情:您希望确保您的插件在运行 clean 和generate-sources 后运行,因为依赖于某些状态。

它是一个插件,从 Maven 生命周期内的各个点调用。

我认为您需要在“流程源”阶段或以后创建一个目标。当该目标运行时,clean 和generate-sources 将已经运行。

Let me paraphase what I think you're trying to do: You want to ensure that your plugin is run after clean and generate-sources are run, because of dependence on some state.

It's a plugin, and it's called from points within the maven lifecycle.

I think what you need to do create a goal on the 'process-sources' phase, or later. When that goal is run, clean and generate-sources will have already been run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文