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

问题描述 投票:95回答:7

我们有一个Maven 2项目,其中包含许多模块。示例:

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

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

是否有不需要两个单独的模块列表的解决方案?

maven-2 maven
7个回答
72
投票

最简单的方法是像这样使用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>

然后您应该签出ways you can activate profiles


140
投票

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

查看此功能请求:https://issues.apache.org/jira/browse/MNG-5230


44
投票

也可以在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

20
投票

我假设您希望默认构建始终构建所有内容,而不管速度如何,以便新开发人员可以快速入门,而不必了解很多有关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不包括在内(除非开发人员也指定了它)。这使得记住需要包含哪些配置文件变得很复杂。

这是一种解决方法。由于pom.xml文件始终存在,因此始终包含两个配置文件。因此,要排除昂贵的模块,可以在命令行上使用-P!full-build

<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>

7
投票

另一个想法:Reactor模块可以嵌套,因此应该可以将快速构建模块和慢速构建模块分组为单独的pom,然后添加另一个包含这两个模块的聚合pom。然后,您的CI服务器只能引用包含快速构建模块的pom。

<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>

1
投票

您可能会使用Maven profiles。在我们的构建环境中,我们创建了一个配置文件quick,该配置文件禁用了许多插件和测试执行。

这是由]完成的>

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

然后我们通过以下方式调用maven

mvn groupId:artifactId:goal -P quick

您可以在模块的pom中禁用编译和其他标准插件以加快速度。


0
投票

这些人所要求的答案不完全是。我的情况是我只想部署父pom。我在子模块中使用spring-boot-thin-layout。这要求将父模块部署到构件中。我将以下内容添加到我的项目中。它可以跳过install和/或deploy相位。

© www.soinside.com 2019 - 2024. All rights reserved.