如何从Maven reactor构建中排除模块?

kpbwa7wx  于 2023-04-11  发布在  Maven
关注(0)|答案(7)|浏览(181)

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

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

假设“data”模块构建起来很耗时,我们想在CI服务器构建项目时将其排除。目前我们使用两个pom.xml文件来实现这一点。一个文件中包含所有模块,另一个文件中包含除了CI可以忽略的模块之外的所有模块。但这非常烦人,因为有时我们忘记将新模块放入两个文件中。
有没有不需要两个独立模块列表的解决方案?

tcbh2hod

tcbh2hod1#

在Maven 3.2.1中,您现在可以使用-pl '!<module_name>,!<module_name>'从reactor构建中排除某些模块:

mvn install -pl '!:module1,!:module2'

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

6za6bjd0

6za6bjd02#

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

然后,您应该检查出的方式,您可以激活配置文件

olhwl3o2

olhwl3o23#

要构建的项目也可以在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
yws3nbqq

yws3nbqq4#

我假设你希望默认的构建总是构建所有的东西,不管速度如何,这样新的开发人员就可以快速入门,而不必了解很多关于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>
cbjzeqam

cbjzeqam5#

另一个想法: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>
jm81lzqq

jm81lzqq6#

您可以使用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中禁用编译和其他标准插件来加速它。

deikduxw

deikduxw7#

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

<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>的模块。

相关问题