Java 运行Maven项目的主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9846046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:29:51  来源:igfitidea点击:

run main class of Maven project

javamavenmaven-2maven-exec-plugin

提问by Dónal

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:

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

mvn run-app com.example.MainClass

采纳答案by Matthew Farwell

Try the maven-exec-plugin. From there:

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

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

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

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

If you're on Windows, apply quotes for exec.mainClassand exec.args:

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

如果您使用的是 Windows,请为exec.mainClass和应用引号exec.args

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

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

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

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

回答by albfan

Although maven exec does the trick here, I found it pretty poor for a real test. While waiting for maven shell, and hoping this could help others, I finally came out to this repo mvnexec

尽管 maven exec 在这里做到了这一点,但我发现它对于真正的测试来说非常糟糕。在等待maven shell并希望这可以帮助其他人时,我终于来到了这个 repo mvnexec

Clone it, and symlink the scriptsomewhere in your path. I use ~/bin/mvnexec, as I have ~/binin my path. I think mvnexecis a good name for the script, but is up to you to change the symlink...

克隆它,并将脚本符号链接到您路径中的某个位置。我使用~/bin/mvnexec,就像我~/bin在我的道路上一样。我认为mvnexec是脚本的好名字,但由您来更改符号链接...

Launch it from the root of your project, where you can see src and target dirs.

从项目的根目录启动它,您可以在其中看到 src 和目标目录。

The script search for classes with main method, offering a select to choose one (Example with mavenized JMeldproject)

使用 main 方法搜索类的脚本,提供一个选择(以 mavenized JMeld项目为例)

$ mvnexec 
 1) org.jmeld.ui.JMeldComponent
 2) org.jmeld.ui.text.FileDocument
 3) org.jmeld.JMeld
 4) org.jmeld.util.UIDefaultsPrint
 5) org.jmeld.util.PrintProperties
 6) org.jmeld.util.file.DirectoryDiff
 7) org.jmeld.util.file.VersionControlDiff
 8) org.jmeld.vc.svn.InfoCmd
 9) org.jmeld.vc.svn.DiffCmd
10) org.jmeld.vc.svn.BlameCmd
11) org.jmeld.vc.svn.LogCmd
12) org.jmeld.vc.svn.CatCmd
13) org.jmeld.vc.svn.StatusCmd
14) org.jmeld.vc.git.StatusCmd
15) org.jmeld.vc.hg.StatusCmd
16) org.jmeld.vc.bzr.StatusCmd
17) org.jmeld.Main
18) org.apache.commons.jrcs.tools.JDiff
#? 

If one is selected (typing number), you are prompt for arguments (you can avoid with mvnexec -P)

如果选择了一个(输入数字),则会提示您输入参数(您可以使用 避免mvnexec -P

By default it compiles project every run. but you can avoid that using mvnexec -B

默认情况下,它每次运行都会编译项目。但你可以避免使用mvnexec -B

It allows to search only in test classes -Mor --no-main, or only in main classes -Tor --no-test. also has a filter by name option -f <whatever>

它允许仅在测试类-M--no-main,或仅在主类-T或 中搜索--no-test。还有一个按名称过滤的选项-f <whatever>

Hope this could save you some time, for me it does.

希望这可以为您节省一些时间,对我来说确实如此。