【问题标题】:Is it possible to execute two different maven exec-maven-plugin in a single POM是否可以在单个 POM 中执行两个不同的 maven exec-maven-plugin
【发布时间】:2011-11-24 04:07:25
【问题描述】:

我使用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>  

【问题讨论】:

    标签: maven


    【解决方案1】:

    试试这个。您可以在执行中执行多个执行。您需要做的就是在执行下移动配置元素。插件有配置,但每次执行也可以有一个单独的配置元素。

    <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。详情请见this answer

    【讨论】:

    • 对于那些想知道如何通过 id 调用执行的人,my search 没有找到这样做的方法。
    • 确实如此。我不确定为什么这个解决方案被接受,而实际上似乎没有办法利用第二次执行,这使得它无用......
    • 我也尝试做类似于上面解释的事情,但它似乎不起作用。只有第一个 execution 被触发,后者被忽略。
    • @DanielKaplan 是的,对于 maven > 3.3.1,您使用 mvn exec:java@execId 调用 ID 执行
    【解决方案2】:

    @tieTYT:您可以使用两个不同的配置文件按 id 选择执行:

    mvn 测试 -Pmanager

    mvn 测试 -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>
    

    【讨论】:

    • 如何将命令行参数传递给主类?
    【解决方案3】:

    使用 maven > 3.3.1 可以将执行 ID 指定为:

    mvn exec:java@execId
    

    【讨论】:

      【解决方案4】:

      对我来说,在执行块中包含配置不起作用,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
      

      干杯,

      【讨论】:

      • 请注意,如果您指定多个配置文件(例如 mvn exec:java -Pfoo,bar),那么使用此技术只会执行一个配置文件,所以要小心。
      • 那么当你想同时运行两个服务器怎么办?
      • @Jacek,我找到了解决方案:I put &lt;configuration&gt; in &lt;execution&gt;
      【解决方案5】:

      我找到了解决办法: I put &lt;configuration&gt; in &lt;execution&gt;

      你可以使用 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>
      

      【讨论】:

        【解决方案6】:

        恐怕你想要的东西是不可能的。我找不到在 .pom 文件中使用不同配置直接调用相同 exec-maven-plugin 目标 (mvn exec:java) 的方法。

        也就是说,您可以多次执行 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

        【讨论】:

          最近更新 更多