2009-05-22 82 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>