提供了两个Maven项目:J(jar)、W(war);两者都依赖于一个父 P (pom)。父级有一个插件,只能为项目“W”运行。
如何做到这一点:
- 无需创建单独的父项目
- 不使用配置文件(因此仍然必须使用
mvn clean package
执行构建)
J( jar )
<project>
<parent>
<artifactId>P</artifactId>
</parent>
<artifactId>J</artifactId>
<packaging>jar</packaging>
</project>
W( war )
<project>
<parent>
<artifactId>P</artifactId>
</parent>
<artifactId>W</artifactId>
<packaging>war</packaging>
</project>
P(pom)
<project>
<artifactId>P</artifactId>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>classes</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最佳答案
我认为您可以使用 Profile Activation 实现此目的.理想情况下,激活条件应该类似于“packaging type is war”,但显然,这个条件无法在 Maven 中实现。但是,在您的情况下,有一个条件可以实现并且在实践中可能是等效的:“有一个src/main/webapp
目录”。
这就是您的 pom.xml
的样子:
<profiles>
<profile>
<activation>
<file>
<exists>src/main/webapp</exists>
</file>
</activation>
<build>
[plugin configuration]
</build>
</profile>
</profiles>
关于java - 如何在 Maven 中仅针对 "war"包类型运行插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7934504/