如何配置pom.xml以在1个Maven项目中运行2个Java Mains [英] How to Configure pom.xml to run 2 java mains in 1 Maven project

查看:84
本文介绍了如何配置pom.xml以在1个Maven项目中运行2个Java Mains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,它在src文件夹中的一个软件包中有2个电源(MyTestApp_A和MyTestApp_B).

I have a Maven project and it has 2 mains (MyTestApp_A, and MyTestApp_B) inside one of the packages from the src folder.

如果我打开它们并单击运行按钮,则可以在Eclipse中运行这些主"类.但是,Eclipse有bug,因此我实际上需要使用Maven在命令行上运行这两个类.

I can run these "main" classes in Eclipse if I open them and click the run button. However, Eclipse is bugged, and so I actually need to run these two classes on a command line using Maven.

我以前从未使用过Maven,但是在寻求帮助并进行了一些研究之后,我知道我必须更改pom.xml文件.

I have never used Maven before, but after asking for help and doing some research I understand that I have to change the pom.xml file.

因此,我使用命令mvn exec:java -Dexec.mainClass="servers.MyTestApp_B"成功更改了pom.xml文件以运行其中一个应用程序:

Consequently, I have altered my pom.xml file successfully to run one of the apps using the command mvn exec:java -Dexec.mainClass="servers.MyTestApp_B":

    <plugins>
        <!-- Allows the example to be run via 'mvn compile exec:java' -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <configuration>
                <mainClass>servers.MyTestApp_A</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>


        </plugin>

    </plugins>

对能够运行MyTestApp_A感到满意,我试图添加另一个配置部分来运行MyTestApp_B:

Happy with being able to run MyTestApp_A, I tried to add another configuration part to run MyTestApp_B:

    <plugins>
        <!-- Allows the example to be run via 'mvn compile exec:java' -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <configuration>
                <mainClass>servers.MyTestApp_A</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>
            <configuration>
                <mainClass>servers.MyTestApp_B</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>

        </plugin>

    </plugins>

但是,此文件格式不正确.显然,我不允许同一pom.xml文件中包含2个<configuration>标记.

However, This file is not well formed. Apparently I am not allowed to have 2 <configuration> tags in the same pom.xml file.

那么,如何使用Maven执行MyTestApp_A和MyTestApp_B?如何配置pom.xml文件?

So, how can I execute MyTestApp_A, and MyTestApp_B using Maven? How do I configure the pom.xml file?

推荐答案

尝试对每个要执行的主类使用执行:

Try using executions for each main class you want to execute:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>MyTestApp_A</id>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>servers.MyTestApp_A</mainClass>
                    <includePluginDependencies>false</includePluginDependencies>
                </configuration>
            </execution>
            <execution>
                <id>MyTestApp_B</id>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>servers.MyTestApp_B</mainClass>
                    <includePluginDependencies>false</includePluginDependencies>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

这篇关于如何配置pom.xml以在1个Maven项目中运行2个Java Mains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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