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

标签 maven maven-3

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

类似地 mvn clean install -pl Parent2 仅应用于 Parent2 而没有其子/模块!!

enter image description here

这是我构建的最新 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 可以按预期工作,将 cleaninstall 应用到 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 projects

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

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

关于maven - 在 maven 命令执行生命周期中排除父项及其子项或模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326247/

相关文章:

java - Maven 不在代理后面下载 jar

java - Spring Boot 2 Maven 目录 View 解析器

spring - java.lang.ClassNotFoundException : org. springFramework.web.context.ContextLoaderListener tomcat 9,maven, Spring

Maven从父POM继承了属性

Maven 万无一失 : append to argLine

java - EJB3.0的Servlet 2.5注入(inject),全部打包为WAR

java - Spring boot + H2 Db 到文件 +/h2-console

java - maven 构建后 getResource() 的问题

java - 运行一个简单的Map-Reduce示例

java - 为什么 SpringJUnit4ClassRunner 不能使用 surefire parallel=methods?