简体   繁体   English

如何从 Eclipse 运行 Maven 项目?

[英]How to run a Maven project from Eclipse?

I am trying to run a simple Java project.我正在尝试运行一个简单的 Java 项目。 I had created a project using the 'Maven Project' type.我使用“Maven Project”类型创建了一个项目。 I have one main class called 'TestMain'.我有一个名为“TestMain”的主类。 When I tried to run the project using right click -> run, there was no menu to run the application as 'Run As Java Application'.当我尝试使用右键单击 -> 运行来运行该项目时,没有将应用程序作为“作为 Java 应用程序运行”运行的菜单。 I am wondering where that option has gone.我想知道那个选项去了哪里。

Can anyone please help me to run the Java application?任何人都可以帮我运行 Java 应用程序吗?

Well, you need to incorporate exec-maven-plugin , this plug-in performs the same thing that you do on command prompt when you type in java -cp .;jarpaths TestMain .好吧,您需要合并exec-maven-plugin ,当您键入java -cp .;jarpaths TestMain时,该插件执行的操作与您在命令提示符下执行的操作相同。 You can pass argument and define which phase ( test , package , integration , verify , or deploy ), you want this plug-in to call your main class.您可以传递参数并定义哪个阶段( testpackageintegrationverifydeploy ),您希望此插件调用您的主类。

You need to add this plug-in under <build> tag and specify parameters.您需要在<build>标签下添加此插件并指定参数。 For example例如

   <project>
    ...
    ...
    <build>
     <plugins>
      <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>exec-maven-plugin</artifactId>
       <version>1.1.1</version>
       <executions>
        <execution>
         <phase>test</phase>
         <goals>
          <goal>java</goal>
         </goals>
         <configuration>
          <mainClass>my.company.name.packageName.TestMain</mainClass>
          <arguments>
           <argument>myArg1</argument>
           <argument>myArg2</argument>
          </arguments>
         </configuration>
        </execution>
       </executions>
      </plugin>
     </plugins>
    </build>
    ...
    ...
   </project>

Now, if you right-click on on the project folder and do Run As > Maven Test , or Run As > Maven Package or Run As > Maven Install , the test phase will execute and so your Main class.现在,如果您右键单击项目文件夹并执行Run As > Maven Test ,或Run As > Maven PackageRun As > Maven Installtest phase将执行,因此您的 Main 类。

(Alt + Shift + X) , then M to Run Maven Build. (Alt + Shift + X) , then M运行 Maven Build。 You will need to specify the Maven goals you want on Run -> Run Configurations您需要在Run -> Run Configurations上指定所需的 Maven 目标

Your Maven project doesn't seem to be configured as a Eclipse Java project, that is the Java nature is missing (the little 'J' in the project icon).您的 Maven 项目似乎没有配置为 Eclipse Java 项目,即缺少 Java性质(项目图标中的小“J”)。

To enable this, the <packaging> element in your pom.xml should be jar (or similar).要启用此功能,您的 pom.xml 中的<packaging>元素应该是jar (或类似的)。

Then, right-click the project and select Maven > Update Project Configuration然后,右键单击该项目并选择Maven > Update Project Configuration

For this to work, you need to have m2eclipse installed.为此,您需要安装 m2eclipse。 But since you had the _ New ... > New Maven Project_ wizard, I assume you have m2eclipse installed.但是由于您拥有 _ New ... > New Maven Project_ 向导,我假设您已经安装了 m2eclipse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM