如何在Eclipse中使用Maven exec插件?

5
我是Maven的新手,正在尝试使用exec:java运行一个项目。我的构建失败了,可能是因为我没有安装exec插件。我已经下载了插件(http://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin),但现在我不知道该如何在Eclipse中使用它。我尝试将其添加到构建路径中,但似乎没有起作用。这可能是一个非常愚蠢的问题,但我该如何让插件正常工作呢?
以下是我的构建输出:
[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Twitter2 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ Twitter2 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.826 s
[INFO] Finished at: 2015-06-11T14:24:29-06:00
[INFO] Final Memory: 10M/114M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project Twitter2: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java are missing or invalid -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException
1个回答

8

根据错误提示,似乎mainClass参数没有正确设置,该参数告诉插件要运行哪个类。请参阅插件的java目标使用方式:http://www.mojohaus.org/exec-maven-plugin/java-mojo.html。尝试在POM配置中添加该参数:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
         <mainClass>fully.qualified.class.name</mainClass>
    </configuration>
</plugin>

或者通过命令行中的属性-Dexec.mainClass=fullClassName


谢谢您的回复。我似乎无法弄清楚如何正确编写完全限定类名。您能给出大纲吗? - user
@user 请查看https://docs.oracle.com/javase/tutorial/java/package/index.html。它应该依赖于包名。例如,如果您的类`MyClass`位于源目录`src`下,特定地位于包文件夹`a/b/c`下,则完全限定名称为`a.b.c.MyClass`。 - M A
我已经让它运行了,但是它出现了这个警告: [WARNING] Some problems were encountered while building the effective model for Test:Twitter2:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:exec-maven-plugin is missing. @ line 19, column 14这是需要我修复的吗? - user
这只是一个警告。但是您可以通过在POM中的插件配置中添加插件版本,例如<version>1.4.0</version>来消除它(1.4.0是Maven在构建输出中显示的版本)。 - M A
非常感谢!现在我的程序正常运行了。 - user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接