如何使用Maven执行程序? [英] How do I execute a program using Maven?
问题描述
我想让Maven目标触发Java类的执行.我正在尝试通过Makefile
行进行迁移:
I would like to have a Maven goal trigger the execution of a java class. I'm trying to migrate over a Makefile
with the lines:
neotest:
mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse"
我希望mvn neotest
产生make neotest
当前所做的事情.
And I would like mvn neotest
to produce what make neotest
does currently.
Neither the exec plugin documentation nor the Maven Ant tasks pages had any sort of straightforward example.
当前,我在:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>org.dhappy.test.NeoTraverse</mainClass>
</configuration>
</plugin>
不过,我不知道如何从命令行触发插件.
I don't know how to trigger the plugin from the command line, though.
推荐答案
使用您为exec-maven-plugin定义的全局配置:
With the global configuration that you have defined for the exec-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>org.dhappy.test.NeoTraverse</mainClass>
</configuration>
</plugin>
在命令行上调用mvn exec:java
将会调用配置为执行类org.dhappy.test.NeoTraverse
的插件.
invoking mvn exec:java
on the command line will invoke the plugin which is configured to execute the class org.dhappy.test.NeoTraverse
.
因此,要从命令行触发插件,只需运行:
So, to trigger the plugin from the command line, just run:
mvn exec:java
现在,如果要在标准构建中执行exec:java
目标,则需要将该目标绑定到
Now, if you want to execute the exec:java
goal as part of your standard build, you'll need to bind the goal to a particular phase of the default lifecycle. To do this, declare the phase
to which you want to bind the goal in the execution
element:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>my-execution</id>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.dhappy.test.NeoTraverse</mainClass>
</configuration>
</plugin>
在此示例中,您的类将在package
阶段执行.这只是一个示例,请对其进行调整以适合您的需求.在插件版本1.1中也可以使用.
With this example, your class would be executed during the package
phase. This is just an example, adapt it to suit your needs. Works also with plugin version 1.1.
这篇关于如何使用Maven执行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!