如何使用Maven执行程序? [英] How do I execute a program using Maven?

查看:95
本文介绍了如何使用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.

exec插件文档

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆