在单个POM中执行两个不同的maven exec-maven-plugin插件是否可行?

75

我使用mvn exec:java com.mycompany.FooServer执行以下代码。

我想添加另一个服务器,像这样执行:mvn exec:java com.mycompany.BarServer

如何在一个单独的POM文件中实现?

<build>
    <plugins>
        <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.mycompany.FooServer</mainClass>
            </configuration>
        </plugin>
 </build>  
6个回答

123

试试这个。你可以在executions下有多个执行。你所需要做的就是将配置元素放在execution下面。该插件有配置,但每个执行也可以有单独的配置元素。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>first-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.FooServer</mainClass>
                    </configuration>
                </execution>
                <execution>
                    <id>second-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.BarServer</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
 </build>

使用Maven 3.3.1及以上版本,您可以通过其ID运行一个执行:

mvn exec:java@id

在这种情况下,命令将是 mvn exec:java@first-executionmvn exec:java@second-execution。有关更多详细信息,请参见此答案


7
如果你想知道如何通过id调用执行操作,我的搜索没有找到相应的方法。 - Daniel Kaplan
1
确实。我不确定为什么这个解决方案被接受了,因为事实上似乎没有办法利用第二次执行,这使它变得无用... - Petr Baudis
1
我也尝试了类似于上面解释的方法,但似乎不起作用。只有第一个“执行”被触发,后面的被忽略了。 - victor
6
是的,使用 Maven 版本大于 3.3.1,你可以通过 mvn exec:java@execId 命令调用指定 ID 的执行。 - George
显示剩余2条评论

10

@tieTYT: 你可以使用两个不同的配置文件通过id来选择执行:

mvn test -Pmanager

mvn test -Pproxy

<profiles> 
<profile>
    <id>proxy</id>
    <build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>pt.inesc.proxy.Proxy</mainClass>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>
</profile> 
<profile>
    <id>manager</id>
    <build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>pt.inesc.manager.Manager</mainClass>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>
</profile> 
</profiles>

你如何在主类中传递命令行参数? - Muhammad Adnan

9

使用 Maven > 3.3.1 版本,可以通过以下方式指定执行 ID:

mvn exec:java@execId

3
对我来说,在执行块中包含配置并没有起作用,Maven会抱怨未设置主类。但是受到达里奥的答案的启发,我将如下回答这个问题:
<profiles>
    <profile>
        <id>foo</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <mainClass>com.mycompany.FooServer</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <mainClass>com.mycompany.BarServer</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这将允许您使用以下命令之一运行一个服务器:

mvn exec:java -Pfoo

或者
mvn exec:java -Pbar

Cheers,


请注意,如果您指定了多个配置文件(例如 mvn exec:java -Pfoo,bar ),则只有一个配置文件将使用此技术执行,请小心。 - Mark McLaren
那么当您想同时运行两个服务器时怎么办? - Muhammad Adnan
1
@Jacek,我找到了解决方案:“我将<configuration>放在<execution>中”。 - Stéphane GRILLON

3
我找到了解决方案:我将<configuration>放在<execution>中 你可以使用mvn clean test -Pfoo,bar
<profiles>
    <profile>
        <id>foo</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <executions>
                        <execution>
                            <id>CountContinusIntegr-execution</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.mycompany.FooServer</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <executions>
                        <execution>
                            <id>CountContinusIntegr-execution</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.mycompany.BarServer</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

请注意:除了将<configuration>放在<execution>内部之外,您还需要确保每个执行的<id>。 - Duy Chung

0

很抱歉,您想要的不可能实现。我找不到一种方法来直接调用相同的exec-maven-plugin目标(mvn exec:java)并在.pom文件中使用不同的配置。

话虽如此,您可以使用多个exec-maven-plugin执行。问题是您不能直接调用目标。您必须使用多个执行并将它们绑定到特定的构建阶段。

您还可以使用以下适合我的解决方案。您仍然可以在.pom中使用其配置直接调用一个目标:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <id>Acceptance Tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>pybot</executable>
                <arguments>
                    <!--...-->
                </arguments>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <mainClass>pt.jandias.someapp.persistence.SchemaGenerator</mainClass>
        <arguments>
            <!--...-->
        </arguments>
    </configuration>
</plugin>

然后可以随意使用mvn exec:javamvn integration-test


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接