2011-04-15 115 views
20

我試圖激活使用內部pom.xml定義的屬性行家簡介:如何通過maven屬性激活配置文件?

<project> 
    [...] 
    <properties> 
    <run.it>true</run.it> 
    </properties> 
    [...] 
    <profiles> 
    <profile> 
     <activation> 
     <property><name>run.it</name></property> 
     </activation> 
     [...] 
    </profile> 
    </profiles> 
    [...] 
</project> 

顯然,這是行不通的。然而,激活從命令行起作用:

mvn -Drun.it 

它是「按設計」嗎?如果是,可能的解決方法是什麼?

+0

這是一個解決方法:http://stackoverflow.com/a/14386303/290918 – kldavis4 2013-01-17 21:40:59

+0

我已經創建了一個功能請求,它將'settings.xml'中的''標籤添加到'pom。xml「,以便您可以定義要激活的配置文件。 https://issues.apache.org/jira/browse/MNG-6000 – flungo 2016-04-14 15:36:03

回答

22

編輯,完全重寫,因爲我現在明白了這個問題。

this forum post

輪廓激活基於SYSTEM性能。 的 構建計劃已開始執行

+0

您能否再次查看該問題?我試圖用'pom.xml'中定義的屬性激活配置文件,而不是在命令行中。 – yegor256 2011-04-15 12:19:33

+0

事實上,我錯過了這一點,看到activeByDefault爲M. Jessup告訴 – moritz 2011-04-15 12:25:31

+0

我改變了我的答案 – moritz 2011-04-15 12:34:27

-3

後無法激活根據您的POM 您無法激活基於定義的系統屬性配置文件定義的屬性配置文件,我認爲你的問題是與此類似。

Activation of maven profile based on multiple properties

如前所述,可以激活一個配置文件,並設置各種屬性按您的要求和使用命令mvn -Prun-it設置屬性爲true。

<project> 
    [...] 

    [...] 
    <profiles> 
    <profile> 
    <id>don't-run</id> 
<properties> 
    <run.it>false</run.it> 
    </properties> 


     [...] 
    </profile> 


    <profile> 
    <id>run-it</id> 
<properties> 
    <run.it>true</run.it> 
    </properties> 

     [...] 
    </profile> 


    </profiles> 
    [...] 
</project> 
+1

請仔細查看我的問題。我有興趣從'pom.xml'內部打開/關閉配置文件,而不是從命令行或'settings.xml'。 – yegor256 2011-04-15 14:19:04

+0

但是它對你有什麼影響,請你詳細說明一下?另外,正如Moritz所提到的,激活是基於系統屬性而不是通過命令行完成的。請參閱此參考http://maven.apache.org/guides/introduction/introduction-to-profiles.html – Prabhjot 2011-04-15 14:26:16

-2

作爲莫里茨Heuser先生解釋,型材激活是基於系統性質。但是,您可以嘗試類似的東西:

<project> 
    ... 
    <properties> 
    <run.it>true</run.it> 
    </properties> 
    ... 
    <profiles> 
    <profile> 
     <activation> 
     <activeByDefault>${run.it}</activeByDefault> 
     </activation> 
     ... 
    </profile> 
    </profiles> 
    ... 
</project> 

的想法是在<properties>定義activeByDefault標誌。

+1

我不明白這有何幫助。在pom中定義run.it對於配置文件生效來說太晚了。每個https://jira.codehaus.org/browse/MNG-5235 – 2013-05-15 01:31:53

+0

我同意。有一個典型的雞與雞蛋問題:配置文件我的性質可能會定義其他屬性。他們是否也應該遞歸地激活其他配置文件?如果配置文件已被激活,但新的屬性定義指示將其停用,該怎麼辦? – 2013-06-06 09:57:41

+0

這與OP的示例不起作用的原因不一致(正如@moritz所解釋的) – flungo 2016-04-14 15:42:59

5

怎麼樣使用激活這樣

<profile> 
     <id>gwt</id> 
     <activation> 
      <file> 
       <exists>uses-gwt.marker</exists> 
      </file> 
     </activation> 

,並添加文件「使用-gwt.marker」源代碼控制,旁邊的pom.xml。這爲所有開發人員提供了相同的狀態,並允許面向方面的pom。我們在父pom中使用這種技術,並將hte標記文件放在孩子svn中。不理想,但有效。

0

如前面的答案中所述,配置文件激活僅適用於系統屬性。

但是,有了一點創造力,您可以使用pom屬性實現類似的結果(條件插件執行)。爲了實現這個目標,使用插件的相位標籤要有條件地執行:

<project> 

    ... 

    <properties> 
     <run.it>none</run.it> 
     <!-- <run.it>compile</run.it> --> 
     <!-- <run.it>package</run.it> --> 
    </properties> 

    ... 

    <build> 

     ... 

     <plugins> 
      <plugin> 
      <artifactId>your-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>${run.it}</phase> 
       </execution> 
      </executions> 

      </plugin> 
     </plugins> 

     ... 

    </build> 

</project> 

唯一的區別是,你必須使用相名稱,而不是真/假。但是您可以更改屬性或將其作爲屬性自由覆蓋。

相關問題