在 maven 命令执行生命周期中排除父项及其子项或模块

Exclude parent and its children or modules in maven command executing lifecycle

执行 mvn clean install -pl !Parent2 NOT 将生命周期应用于 Parent2 子模块的 nor 模块。虽然所有子项目都处于同一级别,但我尝试将 Project2 & Project2 合并为一个父项目 Parent2 以仅将其排除在许多生命周期之外。 这是怎么做到的?

类似地 mvn clean install -pl Parent2 仅适用于 Parent2 而没有其 children/modules !!

这是我构建的最新 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>

编辑:答案仅适用于:

Similarly mvn clean install -pl Parent2 only applied on Parent2 without its children/modules !!

使用 mvn -pl Parent2 -amd clean install 构建 Parent2 及其所有模块。

见参考:https://books.sonatype.com/mvnref-book/reference/_using_advanced_reactor_options.html#_making_project_dependents

我也有兴趣在 sub-modules 上应用排除。然而,mvn clean install -pl Parent2 -amd 按需要和预期工作,将 cleaninstall 应用到 Parent2Project2Project3。但是,正如 Pawl's and according to MNG-5230 issue

所指出的

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 projects

So it should be possible to directly exclude nested modules as they should be present in the intial dependency graph before filtering starts.

因此使用 parent/holder pom 排除嵌套模块是不可能的。这是我解决这个问题的方法。只需使用配置文件并完全删除 sub-modules 外部配置文件定义即可解决它。

简而言之 正在使用配置文件来识别模块,并指出无论激活什么配置文件,在配置文件之外识别的模块都是共享的

示例 OP 项目命名遵循

Parent1(添加 Project4Project1 相同只是为了澄清)

<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 将应用于 Parent1Project1Project4,因为 Project1 在默认激活的配置文件中(在执行期间不指定配置文件)。这具有相同的效果 运行 mvn clean -P simple-module-profile 因为 Project4 在配置文件定义之外共享。

最后,运行 mvn clean -P multi-modules-profile 将应用于 Parent1Project4Parent2Project2Project3Project1 留在所需的位置。注意 Project4 总是在里面,因为它在配置文件定义之外。