运行Maven项目的主类 [英] run main class of Maven project

查看:96
本文介绍了运行Maven项目的主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用Maven构建的简单控制台Java应用程序。是否有一种方法可以使用maven命令从命令行运行主类(不需要任何参数),如:

I've created a simple console Java application that is built with Maven. Is there a way that the main class (which doesn't require any arguments) can be run from the command-line using a maven command like:

mvn run-app com.example.MainClass


推荐答案

尝试 maven-exec-plugin 。从那里:

mvn exec:java -Dexec.mainClass="com.example.Main"

这将在JVM中运行您的类。您可以使用 -Dexec.args =arg0 arg1来传递参数。

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.


如果您使用的是Windows,请为 exec.mainClass exec.args

mvn exec:java -D"exec.mainClass"="com.example.Main"


如果您经常这样做,您也可以将参数添加到pom.xml中:

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>com.example.Main</mainClass>
    <arguments>
      <argument>foo</argument>
      <argument>bar</argument>
    </arguments>
  </configuration>
</plugin>

这篇关于运行Maven项目的主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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