2013-01-23 31 views
14

我正在尋找一種方法來禁用與特定配置文件一起運行的插件執行。使用特定配置文件時禁用maven插件

這與running a plugin if a profile is selected相反。我的使用案例:我的Maven構建有一整套的插件,但是在我的開發機器上運行時,我想跳過其中的一些。我不希望將這些插件註釋到本地,而是希望能夠使用「開發」配置文件運行構建。插件將繼續在我的連續編譯中運行。

想法?

+2

在所有插件中都有元素的功能請求。 http://jira.codehaus.org/browse/MNG-3102 – ben75

+0

這將涉及所有插件必須支持此功能。我在考慮'<運行條件> dev ' – jrharshath

+0

您可以詳細闡述一下您希望實現的目標嗎?某種包裝?測試等?或者是什麼?也許你可以給你的pom文件摘錄? – khmarbaise

回答

13
  • 定義你的POM所以這是隻有你在開發模式需要
  • 定義dev的輪廓
  • 定義產出輪廓其中包含了想/需要
  • 定義型材生產的所有插件的插件作爲默認

例如POM:

<profiles> 
    <profile> 
    <id>production</id> 

    <activation> 
     <activeByDefault>true</activeByDefault> 
    </activation> 

    <build> 
     <plugins> 
     <!-- 
     <plugin> 
      ... 
     </plugin> 
     --> 
     </plugins> 
    </build> 
    </profile> 

    <profile> 
     <id>dev</id> 
     <!-- Some other logic here, if necessary. 
      Otherwise, there's no need for another profile. --> 
    </profile> 
</profiles> 

開發模式下運行您可以撥打以下:

mvn -Pdev compile 

生產模式運行只需使用正常的步驟:

mvn compile 

在你不想的情況下/需要定義你的開發文檔中的任何特殊內容,你可以省略它的聲明並且像這樣調用你的開發模式!禁用配置文件):

mvn -P!production compile 

注意:您可能需要逃跑的感嘆號,因爲它是bash中的一種特殊字符:

mvn -P\!production compile 
+0

請添加以下步驟:1)使用插件運行項目,只需照常構建; 2)要運行沒有插件的項目,執行'mvn clean package ... -P!production'。注意:''dev'配置文件是不必要的,如果它不包含任何特別的東西。 '-P!profileName'應該足以排除配置文件。 – carlspring

+0

@carlspring謝謝你的建議,編輯我的帖子 –

+1

@carlspring你能幫我解釋爲什麼我得到這個錯誤嗎? '$ mvn -P!production clean -bash:!production:event not found' –

27

有禁用插件運行時特定的配置文件是活動的一種巧妙的方法。

首先你需要一個標識符添加到像插件運行:

<build> 
    <plugins> 
     <!-- (...) --> 
     <plugin> 
      <groupId>nl.geodienstencentrum.maven</groupId> 
      <artifactId>sass-maven-plugin</artifactId> 
      <version>2.1</version> 
      <executions> 
       <execution> 
        <id>styles-compilation</id> <!-- plugin execution identifier --> 
        <phase>generate-resources</phase> 
        <goals> 
         <goal>update-stylesheets</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

然後,你需要定義在這個插件將不會執行配置文件:如果你

<profiles> 
    <profile> 
     <id>no-sass</id> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>nl.geodienstencentrum.maven</groupId> 
        <artifactId>sass-maven-plugin</artifactId> 
        <version>2.1</version> 
        <executions> 
         <execution> 
          <id>styles-compilation</id> <!-- here there must be the same identifier as defined in <build><plugins> section --> 
          <phase>none</phase> <!-- this disables plugin --> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

現在運行標準maven版本:

mvn clean package 

sass-maven-plugin 執行,尚未運行時:

mvn clean package -P no-sass 

的青菜,Maven的插件不會執行。

1

基於Krzysiek的回答,您不需要定義顯式執行,只需查看maven提供的輸出並禁用默認執行即可。

例如,假設從行家輸出如下:

[INFO] --- maven-resources-plugin:2.7:copy-resources (prepare-dockerfile) @ tilbud --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
... 
[INFO] 
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ tilbud --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
.... 
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ tilbud --- 
... 
[INFO] 
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ tilbud --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
... 
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ tilbud --- 
.... 

生成的默認執行名稱在插件中和進球后的括號中列出。以下配置文件禁用上述插件:

<profiles> 
    <profile> 
     <id>packageOnly</id> 
     <build> 
      <pluginManagement> 
       <plugins> 
        <plugin> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>default-compile</id> 
           <phase>none</phase> 
          </execution> 
          <execution> 
           <id>default-testCompile</id> 
           <phase>none</phase> 
          </execution> 
         </executions> 
        </plugin> 
        <plugin> 
         <artifactId>maven-surefire-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>default-test</id> 
           <phase>none</phase> 
          </execution> 
         </executions> 
        </plugin> 
        <plugin> 
         <artifactId>maven-resources-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>default-resources</id> 
           <phase>none</phase> 
          </execution> 
          <execution> 
           <id>default-testResources</id> 
           <phase>none</phase> 
          </execution> 
          <execution> 
           <id>prepare-dockerfile</id> 
           <phase>none</phase> 
          </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </pluginManagement> 
     </build> 
    </profile> 
</profiles>