2009-05-22 81 views
50

我有一個Maven pom.xml,其中包含一個我希望能夠在命令行上控制的插件。爲自定義Maven 2屬性設置默認值

<plugin> 
    ... 
    <configuration> 
     <param>${myProperty}</param> 
    </configuration> 
    ... 
</plugin> 

所以,如果我有

mvn -DmyProperty=something ... 

運行Maven:雖然我無法弄清楚如何爲我的控件屬性設置默認值一切正常,否則罰款,甚至除了搜索網一後一切都很好,但我希望在沒有-DmyProperty=...開關的情況下爲myProperty指定一個特定值。如何才能做到這一點?

回答

43

老問題,但我認爲最簡單的答案不存在。您可以在<build>/<properties>中或在如下所示的配置文件中定義屬性默認值。當您在命令行上提供屬性值-DmyProperty=anotherValue時,它將覆蓋來自POM的定義。我希望我能解釋..

<profile> 
    ... 
    <properties> 
     <myProperty>defaultValue</myProperty>    
    </properties> 
    ... 
     <configuration> 
      <param>${myProperty}</param> 
     </configuration> 
    ... 
</profile> 
1

這可能會爲你工作:

<profiles> 
    <profile> 
    <id>default</id> 
    <activation> 
     <activeByDefault>true</activeByDefault> 
    </activation> 
    <build> 
    <plugin> 
     <configuration> 
     <param>Foo</param> 
     </configuration> 
    </plugin> 
    </build> 
    ... 
    </profile> 
    <profile> 
    <id>notdefault</id> 
    ... 
    <build> 
     <plugin> 
     <configuration> 
      <param>${myProperty}</param> 
     </configuration> 
    </plugin> 
    </build> 
    ... 
    </profile> 
</profiles> 

這樣,

mvn clean會用 「富」 作爲默認PARAM。在情況下,當你需要重寫,使用mvn -P notdefault -DmyProperty=something

+1

無法此使用激活塊,除非沒有-D性能是在通過激活NODEFAULT簡化一點所有。 – djangofan 2013-07-19 20:02:12

+0

@djangofan你是對的。我試圖讓我的回答在這個問題上取代。 – sal 2013-10-25 00:17:55

25

您可以使用類似如下:

<profile> 
    <id>default</id> 
    <properties> 
     <env>default</env> 
     <myProperty>someValue</myProperty>    
    </properties> 
    <activation> 
     <activeByDefault>true</activeByDefault> 
    </activation> 
</profile> 
+0

對,就這樣做,謝謝! – 2009-05-22 21:13:18

30

泰勒L的方法工作得很好,但你並不需要額外的配置文件。你可以在POM文件中聲明屬性值。

<project> 
    ... 
    <properties> 
    <!-- Sets the location that Apache Cargo will use to install containers when they are downloaded. 
     Executions of the plug-in should append the container name and version to this path. 
     E.g. apache-tomcat-5.5.20 --> 
    <cargo.container.install.dir>${user.home}/.m2/cargo/containers</cargo.container.install.dir> 
    </properties> 
</project> 

如果您希望每個用戶能夠設置自己的默認值,您還可以在用戶settings.xml文件中設置屬性。我們使用這種方法來隱藏CI服務器用於常規開發人員的一些插件的憑證。

2

akostadinov解決方案共同使用的偉大工程......但如果需要的財產,由反應器組件在解決依賴階段使用(很早就在MVN POM層次處理。 ..)您應該使用配置文件「無激活」測試機制來確保可選命令行提供的值始終優先考慮在pom.xml中提供的值。而這無論深度如何都是你的pom等級。

要做到這一點,在父pom.xml中添加這種輪廓:

<profiles> 
    <profile> 
     <id>my.property</id> 
     <activation> 
     <property> 
      <name>!my.property</name> 
     </property> 
     </activation> 
     <properties> 
     <my.property>${an.other.property} or a_static_value</my.property>    
     </properties> 
    </profile> 
    </profiles>