2011-11-29 25 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]跳過項目