如何从 Maven 反应堆构建中排除模块?

我们有一个 Maven2项目,里面有很多模块。例如:

<modules>
<module>common</module>
<module>foo</module>
<module>data</module>
<module>bar</module>
... more ...
</module>

假设构建“数据”模块非常耗时,并且我们希望在 CI 服务器构建项目时将其排除在外。目前我们使用两个 pom.xml 文件来实现这一点。一个包含所有的模块,另一个包含所有的模块,除了那些可以留给 CI 的模块。但是这很烦人,因为有时候我们忘记在 both文件中放入一个新模块。

Is there a solution which doesn't need two separate module lists?

116312 次浏览

要生成的项目也可以在 mvn 命令行上指定。这将消除单独使用 pom 的需要,但是您必须在每次有新模块时更改 CI 配置。

-pl,--projects <arg>                Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.

也许这个标志和 --also-make-dependents--also-make的组合将再次减少这种维护负担。

-am,--also-make                     If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents         If project list is specified, also
build projects that depend on
projects on the list

你可以使用美文 资料。在我们的构建环境中,我们创建了一个配置文件 quick,它禁用了许多插件和测试执行。

这是由

    <profile>
<id>quick</id>
<properties>
<skipTests>true</skipTests>
<!-- others... -->
</properties>
<build>
<plugins>
<!-- configuration... -->
</plugins>
</build>
</profile>

然后我们以下面的方式调用 maven

mvn groupId:artifactId:goal -P quick

你也许可以在你的模块中禁用编译和其他标准插件来提高速度。

Another idea: Reactor modules can be nested, so it should be possible to group your fast and slow-building modules into separate poms and then add another aggregator pom containing these two as modules. Your CI Server could then only reference the pom containing the fast building modules.

<artifactId>fast</artifactId>
<modules>
<module>fast-a</module>
<module>fast-b</module>
<module>fast-c</module>
</module>


<artifactId>all</artifactId>
<modules>
<module>fast</module>
<module>slow</module>
</module>

最简单的方法可能是像下面这样使用 profiles:

<project>
...
<modules>
<module>common</module>
<module>foo</module>
<module>bar</module>
<modules>
...
<profiles>
<profile>
<id>expensive-modules-to-build</id>
<modules>
<module>data</module>
</modules>
</profile>
</profiles>
</project>

然后您应该检查 激活配置文件的方法

我假设您希望默认构建始终构建所有内容,而不管构建速度如何,这样新开发人员就可以快速入门,而不必对 POM 有太多了解。你可以这样使用配置文件:

<modules>
<module>common</module>
<module>foo</module>
<module>bar</module>
</modules>
...
<profiles>
<profile>
<id>expensive-modules-to-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>data</module>
</modules>
</profile>
</profiles>
</project>

这样做的问题是,如果开发人员在命令行上指定了另一个概要文件,那么就不包括 expensive-modules-to-build(除非开发人员也指定了它)。这使得记住需要包含哪些配置文件变得很复杂。

Here is a hacky way around that. Both profiles are always included, because the pom.xml file always exists. So to exclude the expensive modules, you can use -P!full-build on the command line.

<profiles>
<profile>
<id>full-build</id>
<activation>
<file>
<exists>pom.xml</exists>
</file>
</activation>
<modules>
<module>data</module>
</modules>
</profile>
<profile>
<id>short-build</id>
<activation>
<file>
<exists>pom.xml</exists>
</file>
</activation>
<modules>
<module>common</module>
<module>foo</module>
<module>bar</module>
</modules>
</profile>
</profiles>

在 Maven3.2.1中,您现在可以使用 -pl !<module_name>,!<module_name>从反应堆构建中排除某些模块。

查看这个特性请求: https://issues.apache.org/jira/browse/MNG-5230

这不是他们想要的答案。我的情况是,我只想部署父 Pom。我在子模块中使用 spring-boot-thin-layout。这就要求将父模块部署到工厂中。我在项目中添加了以下内容。它允许跳过 install和/或 deploy阶段。

在我父母的诗里:

<properties>
<disable.install>true</disable.install>
<disable.deploy>true</disable.deploy>
<enable.deployAtEnd>true</enable.deployAtEnd>
</properties>


<profiles>
<profile>
<id>deploy-parent</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<disable.install>true</disable.install>
<disable.deploy>true</disable.deploy>
<deployAtEnd>${enable.deployAtEnd}</deployAtEnd>
</properties>
<build>
<finalName>${project.version}</finalName>
</build>
</profile>
</profiles>

并且在我的子 pom (s)或任何您不希望与父模块一起部署的模块中:

<properties>
<maven.install.skip>${disable.install}</maven.install.skip>
<maven.deploy.skip>${disable.deploy}</maven.deploy.skip>
<deployAtEnd>${enable.deployAtEnd}</deployAtEnd>
</properties>

因此,当我在父 pom 上运行 mvn deploy时,它将编译所有模块,而不在任何模块上运行 install,然后在最后部署任何在其属性中没有 <maven.deploy.skip>${disable.deploy}</maven.deploy.skip>的模块。所以在我的例子中,只部署父元素。