在Maven构build期间跳过一个子模块

我们需要能够在某些环境中跳过子模块。

有问题的模块包含集成testing,需要半小时才能运行。 所以我们希望在CI服务器上构build时包含它,但是当开发人员在本地构build(并且testing运行)时,我们希望跳过该模块。

有没有办法做到这一点与configuration文件设置? 我做了一些Googlesearch,看了看其他问题/答案,并没有find一个好的解决scheme。

我想有一个select是从父pom.xml完全删除该子模块,只需在我们的CI服务器上添加另一个项目即可构build该模块。

build议?

当然,这可以使用configuration文件来完成。 你可以在父pom.xml中做如下的事情。

  ... <modules> <module>module1</module> <module>module2</module> ... </modules> ... <profiles> <profile> <id>ci</id> <modules> <module>module1</module> <module>module2</module> ... <module>module-integration-test</module> </modules> </profile> </profiles> ... 

在你的configuration文件中,你可以用ciconfiguration文件运行maven,即mvn -P ci clean install

Maven版本3.2.1增加了这个function,你可以使用-pl开关和“!” 排除某些子模块。

 mvn -pl '!submodule-to-exclude' install 

小心点击字符! 是一个特殊的字符,所以你要么单引号(就像我做的那样)或者用反斜杠字符来转义它。

排除多个模块的语法与包含相同

 mvn -pl '!submodule1,!submodule2' install 

编辑 Windows似乎不喜欢单引号,但它是必要的bash; 在Windows中,使用双引号(谢谢@awilkinson)

 mvn -pl "!submodule1,!submodule2" install 

通过指定-pl命令行参数可以确定要build立哪个反应堆项目:

 $ mvn --help [...] -pl,--projects <arg> Build specified reactor projects instead of all projects [...] 

它以下列forms之一接受以逗号分隔的参数列表:

  • 包含POM的文件夹的相对path
  • [groupId]:artifactId

因此,给定以下结构:

 project-root [com.mycorp:parent] | + --- server [com.mycorp:server] | | | + --- orm [com.mycorp.server:orm] | + --- client [com.mycorp:client] 

你可以指定下面的命令行:

 mvn -pl .,server,:client,com.mycorp.server:orm clean install 

build立一切。 删除列表中的元素以仅构build您请求的模块。


编辑:正如blackbuild指出的那样,从Maven 3.2.1开始,你有一个新的-el标志 ,从反应堆中排除项目,类似于-pl所做的:

多模块项目的概念就是为了满足项目中相关部分的需求。 这样的客户端依赖于依赖于EJB或数据访问例程的服务。 您可以按照这种方式对您的持续集成(CI)testing进行分组。 我会理解这一点,说CItesting需要与应用程序逻辑变化保持一致。

假设你的项目结构如下:

 project-root | + --- ci | + --- client | + --- server 

project-root/pom.xml定义了模块

 <modules> <module>ci</module> <module>client</module> <module>server</module> </modules> 

ci/pom.xml定义configuration文件,例如:

 ... <profiles> <profile> <id>default</id> <activation> <activeByDefault>true</activeByDefault> </activation> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </profile> <profile> <id>CI</id> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>false</skip> </configuration> </plugin> </profile> </profiles> 

这将导致Maven在此模块中跳过testing,除非名为CI的configuration文件处于活动状态。 您的CI服务器必须被指示执行mvn clean package -P CI 。 Maven网站对剖析机制进行了深入的解释 。

现在(从1.1.1版本)在坑中有一个“跳过”的标志。

所以你可以做这样的事情:

  <profile> <id>pit</id> <build> <plugins> <plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </profile> 

在你的模块中,坑将跳过

[INFO] — pitest-maven:1.1.3:mutationCoverage(default-cli)@ module-selenium — [INFO]跳过项目