执行 mvn clean install -pl !Parent2
NOT 在 Parent2
子模块或模块上应用生命周期。虽然所有子项目都处于同一级别,但我尝试将 Project2
和 Project2
合并到一个父项目 Parent2
中,以仅将其排除在许多生命周期之外. 这是怎么做到的?
类似地 mvn clean install -pl Parent2
仅应用于 Parent2
而没有其子/模块!!
这是我构建的最新 POM。请注意,我唯一的限制是将所有文件夹保持在同一级别(在 Parent1
下)
父级 1
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>any</groupId>
<artifactId>Parent1</artifactId>
<version>whatever</version>
<packaging>pom</packaging>
<modules>
<module>Parent2</module>
<module>Project1</module>
</modules>
</project>
父级 2
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>any</groupId>
<artifactId>Parent1</artifactId>
<version>whatever</version>
</parent>
<packaging>pom</packaging>
<artifactId>Parent2</artifactId>
<modules>
<module>../Project2</module>
<module>../Project3</module>
</modules>
</project>
项目 1
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>any</groupId>
<artifactId>Parent1</artifactId>
<version>whatever</version>
</parent>
<packaging>jar</packaging>
<artifactId>Project1</artifactId>
</project>
Project2(Project3完全一样)
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>any</groupId>
<artifactId>Parent2</artifactId>
<version>whatever</version>
<relativePath>../Parent2</relativePath>
</parent>
<packaging>jar</packaging>
<artifactId>Project2</artifactId>
</project>
最佳答案
我也有兴趣在子模块上应用排除。然而,mvn clean install -pl Parent2 -amd
可以按预期工作,将 clean
和 install
应用到 Parent2
中,项目 2
& 项目 3
。但是,正如 Pawl 所指出的那样的 comment并根据 MNG-5230问题
Nested modules are not excluded by parent module. The exclusion method mimics the inclusion method and its matcher does not support wildcards etc. So to cascade exclusion would necessitate an extra flag like
-amd
.The dependency graph is filtered in the following order when using -pl:
1. included projects + possibly upstream and downstream
2. excluded projects
3. skipping for resume projectsSo it should be possible to directly exclude nested modules as they should be present in the intial dependency graph before filtering starts.
因此,使用父/持有者 pom
排除嵌套模块是不可能的。这是我解决这个问题的方法。只需使用配置文件并完全删除配置文件定义之外的子模块即可解决它。
简而言之是使用配置文件来识别模块,并指出无论激活什么配置文件,在配置文件之外识别的模块都是共享的
示例 OP 项目命名遵循
Parent1(添加了与 Project1
相同的 Project4
只是为了说明)
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Parent1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<profiles>
<profile>
<id>multi-modules-profile</id>
<modules>
<module>Parent2</module>
<module>Project2</module>
<module>Project3</module>
</modules>
</profile>
<profile>
<id>simple-module-profile</id>
<modules>
<module>Project1</module>
</modules>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<modules>
<module>Project4</module>
</modules>
Project1(与Project4
类似)
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>Parent1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Project1</artifactId>
父级 2
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>Parent1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Parent2</artifactId>
<packaging>pom</packaging>
Project2(与Project3
类似)
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>Parent2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Parent2</relativePath>
</parent>
<artifactId>Project2</artifactId>
现在运行 mvn clean
将应用于 Parent1
、Project1
和 Project4
,因为 Project1
位于默认激活的配置文件中(在执行期间不指定配置文件)。这与运行 mvn clean -P simple-module-profile
具有相同的效果,因为 Project4
在配置文件定义之外共享。
最后,运行 mvn clean -P multi-modules-profile
将应用于 Parent1
、Project4
、Parent2
、Project2
和 Project3
将 Project1
排除在外。注意 Project4
总是在里面,因为它在配置文件定义之外。
关于maven - 在 maven 命令执行生命周期中排除父项及其子项或模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326247/