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

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

Maven Run Project

javamaven

提问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:javaif 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

回答by Abhijeet

clean package exec:java -P Class_Containing_Main_Methodcommand 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 设置:

enter image description here

在此处输入图片说明

回答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.mainClassand give its value as the class which contains your main method. For me it was DriverClassin mainpkg. Change it as per your project. enter image description here

上面提到的答案是正确的,但我正在为像我这样的菜鸟简化它。转到您项目的 pom 文件。添加一个新属性exec.mainClass并将其值指定为包含您的主要方法的类。对我来说,它DriverClassmainpkg. 根据您的项目更改它。 在此处输入图片说明

Having done this navigate to the folder that contains your project's pom.xmland 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 mainmethod:

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"标志传递更多参数。