我有一个使用的Maven pom <packaging>war</packaging>.但实际上,我不想构建war文件,我只想要收集所有依赖的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>
Run Code Online (Sandbox Code Playgroud)
问题是,战争文件仍然建立.是否有一种简单的方法来<packaging>war</packaging>执行war:exploded目标而不是战争:战争目标?
或者还有另一种简单的方法吗?
Mic*_*raz 72
解决方案非常简单.您需要覆盖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>
Run Code Online (Sandbox Code Playgroud)
cet*_*nar 44
根据包装阶段战争中war包装的内置生命周期绑定:war mojo被称为.
您可以调用之前的'prepare-package'阶段 - 所有操作都将执行,并在此之后调用mojo war:爆炸
mvn prepare-package war:exploded
Run Code Online (Sandbox Code Playgroud)
结果将与您的相同,但不会产生战争.
我想升级到@Michael Wyraz的答案,只是包含安装跳过设置,以防有人mvn clean install在多模块项目的顶层执行构建,其中一个子模块是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>
Run Code Online (Sandbox Code Playgroud)
没有安装跳过构建失败,因为它尝试将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]
Run Code Online (Sandbox Code Playgroud)
执行mvn clean install -P war_explode此设置(包含在名为war_explode的 maven配置文件中),它完成构建而没有错误.
我能想到做你想做的事的唯一方法是设置使用pom包装(或创建自定义包装)并将所需的目标从战争包装绑定到生命周期的相关阶段.如果你去pom打包你可以使用在一个配置文件中定义war:war执行来允许你打包它,但是你需要使用build-helper-maven-plugin attach-artifact目标来将战争附加到POM.
注意这种方法如果你想使用任何其他特定于战争的处理,它可能会导致你的问题.
战争包装的生命周期绑定在"构建生命周期简介"中列出(请参阅"默认生命周期绑定 - 打包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-->
Run Code Online (Sandbox Code Playgroud)