【问题标题】:Mvn compile before execmvn 在 exec 之前编译
【发布时间】:2013-08-23 12:39:06
【问题描述】:

我正在尝试设置我的 POM,以便当我执行 mvn exec:execmvn exec:java 时,它将首先编译源代码,如果成功,则执行它。

我有以下内容并尝试移动 <execution> 部分但无法使其工作:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <phase>exec</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>my.main.class</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

当我执行mvn exec:exec ...mvn exec:java 时,它不会首先编译。我尝试将&lt;execution&gt; 部分放在exec 插件部分,但这也没有用?

【问题讨论】:

  • 为什么不在 exec:exec/exec:java 之前运行“compiler:compile”?
  • @Elad 我一直忘记编译,然后很困惑为什么什么都没有改变。
  • @Lerp 遇到同样的问题,你找到解决办法了吗?
  • 似乎不可能,也许这不是正确的“maven”方式:/
  • "exec" 不是一个阶段,这就是为什么你不能将编译插件绑定到它

标签: maven compilation exec phase


【解决方案1】:

这并不能完全回答问题,但它会帮助我们实现您的主要目标。

我们可以在调用“构建后”阶段/目标时执行mvn exec:java,而不是在运行mvn exec:java 时执行build 目标。

这将有效地使该阶段无法使用,因为我们将通过执行替换它,但如果我们有一个我们没有使用的“构建后”目标(如packageinstall),我们可以像这样使用它来执行: 使用包目标最终会像:

...
    <properties>
        ...
        <main.class>${project.groupId}.MainClass</main.class>
        ...
    </properties>
...
    <build>
        <plugins>
...
            <!-- region Forces the app to be executed when using mvn package -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <mainClass>${main.class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- endregion -->
...
        </plugins>
    </build>

这样,当我们执行目标package 时:mvn package 将执行插件并运行应用程序。

【讨论】:

    【解决方案2】:

    这是一个老话题,但其他人可能对此感兴趣的替代解决方案。

    这不是您要查找的内容,但您可以使用单个命令编译和执行:

    mvn compile exec:exec
    

    这样 Maven 将始终在执行项目之前对其进行编译。

    【讨论】:

      【解决方案3】:

      您可以将 exec 插件绑定到 build lifecycle 中的 compile 之后的阶段(在下面的示例中为 verify):

      <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>verify</phase>
                              <goals>
                                  <goal>exec</goal>
                              </goals>
                              <configuration>
                                 <mainClass>my.main.class</mainClass>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      </profile> 
      

      然后运行mvn verify

      我看到答案很晚,您可能已经找到了解决方案。 我只是留下作为其他可能需要它的人的参考。

      【讨论】:

      • 感谢您,但如果我理解正确,OP 会尝试在 [直接] 执行 mvn exec:[exec|java] 期间进行编译,而 not 作为一个“正常”的 Maven 构建周期。澄清一下,他们和我一样,希望在我们运行“mvn exec”时首先构建代码,但希望在正常的 Maven 构建周期/阶段运行“exec”。跨度>
      猜你喜欢
      • 2012-11-16
      • 1970-01-01
      • 2020-10-04
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多