maven - 如何从 Maven Reactor 构建中排除模块及其子模块?

标签 maven maven-3 pom.xml maven-module

这个问题被问了很多次,但我没有找到解决方案,如何禁用模块的子模块!?!

例如: 我想禁用项目 BB 和 CC 以及它们的子模块

A
|-AA
|  |--AAA
|-BB
|  |-BBB
|-CC
   |-CCC

但是当我运行这个

mvn -pl \!AA,\!BB clean install

我看到 BBB 和 CCC 也被执行。这只是一个示例。我们有一个多模块项目,大约有一百个项目......所以我不想列出所有项目,只想列出 parent 。这可能吗?

谢谢

最佳答案

您可以使用Maven profiles为了您的目的。

在父项目中,您可以定义一个配置文件,该配置文件会覆盖提供所有必需模块的modules部分。这样的配置文件默认处于事件状态,因此预期行为不会发生变化。

但是,普通 POM(配置文件外)的 modules 部分将为空。请注意,我们不能做相反的事情(这会更有意义):我刚刚尝试过,但没有成功。

因此,这样的 POM 示例如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>test3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
</modules>

<profiles>
    <profile>
        <id>aggregator</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>..\test</module>
            <module>..\test2</module>
        </modules>
    </profile>
</profiles>

执行:

mvn clean install

Maven 还将激活配置文件并按预期执行聚合器构建。因此,没有回归。

但是,执行时:

mvn clean install -P!aggregator

Maven 将停用上面的配置文件,并因此执行不带模块的 react 器构建,仅根据您的意愿执行父级。
遗憾的是,相反的方法(具有空模块的配置文件)在我的测试中不起作用,但该方法有望为您提供足够的提示来实现您的目标。

<小时/>

更新
您实际上正在使用 this new feature自 Maven 3.2.1 起可用。从票上的评论来看,有人提出了同样的问题:

how about excluding nested modules? I tried the new feature and it seems as though:
1. when a top module is excluded, its nested modules are not.
2. it is not possible to exclude nested modules directly.

答案是:

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.

关于maven - 如何从 Maven Reactor 构建中排除模块及其子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897170/

相关文章:

Maven - 完全禁用本地存储库,只转到远程

maven - 如何阻止maven尝试访问http ://repo. maven.apache.org?

maven-3 - 在哪里可以找到 lifecycleMappingMetadata 模式?

java - 从 spring-boot-starter-parent 继承依赖项的 Maven pom.xml 不起作用

scala - sbt-仅在发布期间排除某些依赖项

maven - 将 maven 项目部署到现有的 Tomcat 服务器中

maven - 在 Maven 部署期间失去对 webapps 的权限

Maven 从 Parent 构建特定的 child

java - 带 jetty 的 PermGen 空间

java - Maven 怎么可能构建不在我的项目中的类?