2011-11-29 1415 views
99

我们需要能够在特定环境中跳过子模块。在Maven构建期间跳过子模块

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

有没有办法做到这一点与配置文件设置?我做了一些Google搜索,并查看了其他问题/答案,但没有找到一个好的解决方案。

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

对此提出建议?

+0

为什么不使用Maven Way?这对我来说是完全有效的。 – MaDa

+0

嗯。现在我无法找到人们似乎在争论的地方......所以我更新了我原来的问题,以消除我的观点,即这似乎并不是“Maven方式”。 – denishaskin

回答

113

当然,这可以使用配置文件完成。你可以在父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> 
... 

在你的CI,您可以运行与ci轮廓,即行家mvn -P ci clean install

+2

哦!那很好。比我的建议更简单。 –

+3

优秀的答案!我不知道为什么我很难从Maven文档中找到它。我想提出的一个建议是,因为我更喜欢默认运行集成测试,所以我在该配置文件中添加了'activeByDefault',然后必须添加另一个空配置文件(例如,'skip-integration-tests')能够跳过它们。 – denishaskin

+5

有没有办法做到这一点,而不重复所有的共享的东西? –

5

多模块项目的概念就是为了满足项目中相关部分的需求。这样的客户端依赖于依赖于EJB或数据访问例程的服务。你可以可以以这种方式对你的持续集成(CI)测试进行分组。我认为CI测试需要与应用程序逻辑更改锁定在一起,从而使其合理化。

假设你的项目的结构为:

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

project-root/pom.xml定义模块

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

ci/pom.xml轮廓定义,如:

... 
<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的跳绳测试在此模块中,除了配置文件n已登记的CI已激活。 您的CI服务器必须被指示执行mvn clean package -P CI。 Maven网站有一个in-depth explanation of the profiling mechanism

32

有可能通过指定-pl命令行参数来决定建造一反应器的项目:

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

它接受在以下形式中的一种以逗号分隔的参数列表:

  • 包含POM的文件夹的相对路径
  • [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 

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


编辑:blackbuild指出,随着Maven 3.2.1你有a new -el flag排除项目从反应器,类似于什么-pl做:

+1

谢谢。这对我很好。另请注意,您也可以添加“-am”(也称为“另外make”)以构建您指定的模块所需的项目。 – GaZ

+1

太棒了!我使用'mvn install -pl .'为了在没有构建模块的情况下在本地repo中安装父pom。 – Marcin

+0

另外,请看https://jira.codehaus.org/browse/MNG-5230。您现在可以从反应堆中排除项目。 – blackbuild

111

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

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

小心bash的人物!是一个特殊的字符,所以你要么必须单引号(就像我做的那样),要么用反斜杠字符转义它。

语法排除多个模块是一样的包容

mvn -pl '!submodule1,!submodule2' install 

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

mvn -pl "!submodule1,!submodule2" install 
+9

重要提示:如果您要排除嵌套子模块,则需要使用限定版本'mvn -pl !com.acme:nestedmodule1' –

2

现在有(从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-行家:1.1.3:mutationCoverage(默认CLI) @ module-selenium --- [INFO]跳过项目