Java Maven 运行项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1089285/
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
Maven Run Project
提问by Verhogen
Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".
是否有 Maven“阶段”或“目标”来简单地执行 Java 类的主要方法?我有一个项目,我想通过简单地执行“mvn run”之类的操作来手动测试。
采纳答案by Robert Munteanu
See the exec maven plugin. You can run Java classes using:
请参阅exec maven 插件。您可以使用以下方法运行 Java 类:
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
The invocation can be as simple as mvn exec:java
if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.
调用可以像mvn exec:java
插件配置在您的 pom.xml 中一样简单。Mojohaus 上的插件站点有一个更详细的示例。
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
回答by dfa
Give the Exec Maven plugina try
给Exec的Maven插件一试
回答by Abhijeet
clean package exec:java -P Class_Containing_Main_Method
command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.
clean package exec:java -P Class_Containing_Main_Method
如果项目中只有一个 Main 方法 (PSVM),则命令也是一个选项,具有以下 Maven 设置。
Don't forget to mention the class in the <properties></properties>
section of pom.xml
:
不要忘记在以下<properties></properties>
部分提及该课程pom.xml
:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.main.class>com.test.service.MainTester</java.main.class>
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>${java.main.class}</mainClass>
</configuration>
</plugin>
STSRun Configuration along with above Maven Setup:
STS运行配置以及上述 Maven 设置:
回答by shshnk
The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass
and give its value as the class which contains your main method. For me it was DriverClass
in mainpkg
. Change it as per your project.
上面提到的答案是正确的,但我正在为像我这样的菜鸟简化它。转到您项目的 pom 文件。添加一个新属性exec.mainClass
并将其值指定为包含您的主要方法的类。对我来说,它DriverClass
在mainpkg
. 根据您的项目更改它。
Having done this navigate to the folder that contains your project's pom.xml
and run this on the command prompt mvn exec:java
. This should call the main method.
完成此操作后,导航到包含您的项目的文件夹pom.xml
并在命令提示符下运行它mvn exec:java
。这应该调用 main 方法。
回答by arulraj.net
No need to add new plugin in pom.xml. Just run this command
无需在 pom.xml 中添加新插件。只需运行此命令
mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)'
See the maven exec pluginfor more usage.
有关更多用法,请参阅maven exec 插件。
回答by Saikat
1. Edit POM.xml
1.编辑POM.xml
Add the following property in pom.xml
. Make sure you use the fully qualified class name (i.e. with package name) which contains the main
method:
在pom.xml
. 确保使用包含main
方法的完全限定类名(即包名):
<properties>
<exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>
2. Run Command
2.运行命令
Now from the terminal, trigger the following command:
现在从终端,触发以下命令:
mvn clean compile exec:java
NOTEYou can pass further arguments via -Dexec.args="xxx"
flag.
注意您可以通过-Dexec.args="xxx"
标志传递更多参数。