【问题标题】:How to get Maven to run war:exploded but not war:war如何让 Maven 运行战争:爆炸但不是战争:战争
【发布时间】:2008-12-09 12:49:58
【问题描述】:

我有一个使用 <packaging>war</packaging> 的 Maven pom。但实际上,我不想构建战争文件,我只想收集所有依赖的 jar 并创建一个完整的部署目录。

所以我正在运行war:exploded 目标来生成部署目录:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <webappDirectory>target/${env}/deploy</webappDirectory>
                <archiveClasses>true</archiveClasses>
            </configuration>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
    </executions>
</plugin>

问题是,war 文件仍然会生成。有没有简单的方法让&lt;packaging&gt;war&lt;/packaging&gt; 执行war:exploded 目标而不是war:war 目标?

或者还有其他简单的方法吗?

【问题讨论】:

  • war 文件仍然被创建,那又怎样?为什么会有这样的问题?
  • 我认为这是因为他们想加快构建速度——至少这就是我使用它的原因。我有一个带有 maven 的基本 spring 应用程序,但我正在使用 Google App Engine,它被配置为扫描爆炸的战争构建目录,所以为了让我看到变化,我需要经常构建爆炸战争。有什么方法可以在项目中的任何文件发生更改时自动调用它?

标签: maven-2


【解决方案1】:

解决方案非常简单。您需要覆盖war插件的默认执行以禁用它并添加您自己的执行(用于爆炸):

<pluginManagement>
    <plugins>
            <plugin><!-- don't pack the war  -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-war</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>war-exploded</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exploded</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</pluginManagement>

【讨论】:

  • 这是此处发布的最简单、最简单的解决方案。
  • 是的,这似乎是最好的答案。对我来说就像一个魅力。 :)
  • 你能解释一下这是如何工作的吗?您从哪里得知禁用默认阶段目标绑定是使用 id "default-war" 完成的?
  • 很好的解决方案 - 我花了一两次尝试才能完全理解它并让它发挥作用。
  • +1。是&lt;execution&gt;&lt;id&gt;default-war&lt;/id&gt;&lt;phase&gt;none&lt;/phase&gt;&lt;/execution&gt;实际上消除了战争的创造吗?因为没有它和其他执行,战争仍然被创造?提前致谢
【解决方案2】:

根据builtin lifecycle bindings for war 打包阶段 war:war mojo 被调用。

您可以调用之前的“准备包”阶段 - 所有操作都将执行,然后调用 mojo war:exploded

mvn prepare-package war:exploded

结果将与您的相同,但不会产生战争。

【讨论】:

  • 如果你真的喜欢这个,你可以把war:exploded 调用放到绑定到prepare-package 阶段的pom 中。但是,如果您需要 maven 继续运行其余阶段,您将遇到问题,因此切换到 <packaging>pom 会更好。
  • 我发现这对我来说失败了,因为它试图在“类”文件夹上执行 FileInputStream.open。但是作为两个单独的命令运行 mvn prepare-package 然后 mvn war:exploded 啊,你神秘的野兽 maven
  • 如果您将项目拆分为多个 jar,则此方法不起作用 - 它们需要 installed 才能将它们放入 maven 的静态缓存中,以便它们在准备包阶段拾取.
【解决方案3】:

我想升级到@Michael Wyraz 的答案,并且只包括 install skip 设置,以防有人在多模块项目的顶层执行mvn clean install build 并且子模块之一是 Web 应用程序.

这位于战争模块中:

<profiles>
    <profile>
        <id>war_explode</id>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                        <executions>
                            <execution>
                                <id>default-war</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>war-exploded</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>exploded</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-install</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

如果没有 install skip,构建会失败,因为它会尝试将 war 安装到 .m2 文件夹中。错误信息如下所示:

[INFO] --- maven-install-plugin:2.4:install (default-install) @ *** ---
[INFO]   ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install (default-install) on project ***: The packaging for this project did not assign a file to the build artifact -> [Help 1]

使用此设置执行 mvn clean install -P war_explode(包含在名为 war_explode 的 maven 配置文件中)它会完成构建而不会出错。

【讨论】:

  • 如果安装失败是正确的,因为没有安装任何东西,但这是假装完成。可能最好为爆炸和战争包装制作配置文件,其中爆炸配置文件无法正确安装。
【解决方案4】:

我能想到的唯一方法是设置使用 pom 打包(或创建custom packaging)并将所需目标从战争打包绑定到生命周期的相关阶段。如果您进行 pom 打包,您可以在配置文件中定义 war:war 执行以允许您打包它,但您需要使用 build-helper-maven-plugin attach-artifact goal 将战争附加到 pom .

如果您想使用任何其他特定于战争的处理,请注意这种方法可能会给您带来问题。

war 打包的生命周期绑定在 Introduction to The Build Lifecycle 中列出(请参阅“默认生命周期绑定 - 打包 ejb/ejb3/jar/par/rar/war”部分)。

要将相关的插件执行绑定到 pom 打包,您可以执行以下操作:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>process-resources</id>
          <phase>process-resources</phase>
          <goals>
            <goal>resources</goal>
          </goal>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-compile-plugin</artifactId>
      <executions>
        <execution>
          <id>compile</id>
          <phase>compile</phase>
          <goals>
            <goal>compile</goal>
          </goal>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>process-test-resources</id>
          <phase>process-test-resources</phase>
          <goals>
            <goal>testResources</goal>
          </goal>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <executions>
        <execution>
          <id>test</id>
          <phase>test</phase>
          <goals>
            <goal>test</goal>
          </goal>
        </execution>
      </executions>
    </plugin>
    <!-- package not wanted, install and deploy already defined for pom packaging-->
    <!--define war:war execution in a profile in case it is needed-->

【讨论】:

    【解决方案5】:

    据我所知(我还是 maven 的新手)这是不可能的。您可以跳过的唯一默认生命周期是“测试”。为了进行部署,您必须打包。您可以在此处阅读有关默认生命周期执行顺序的所有信息:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

    【讨论】:

      猜你喜欢
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多