Maven 2 设置自定义Maven 2属性的默认值

Maven 2 设置自定义Maven 2属性的默认值,maven-2,properties,Maven 2,Properties,我有一个带插件的Maven pom.xml,我希望能够在命令行上控制它。在其他方面一切正常,除了搜索网络一段时间后,我不知道如何为我的控件属性设置默认值: <plugin> ... <configuration> <param>${myProperty}</param> </configuration> ... </plugin> 一切都很好,但我希望在不使用-DmyProp

我有一个带插件的Maven pom.xml,我希望能够在命令行上控制它。在其他方面一切正常,除了搜索网络一段时间后,我不知道如何为我的控件属性设置默认值:

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

一切都很好,但我希望在不使用
-DmyProperty=…
开关的情况下为myProperty指定一个特定的值。如何做到这一点?

这可能适合您:

<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>

违约
真的
福
...
不违约
...
${myProperty}
...
这样,


mvn clean
将使用“foo”作为默认参数。在需要覆盖的情况下,使用
mvn-P notdefault-DmyProperty=something

您可以使用如下内容:

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

违约
违约
一些价值
真的

Taylor 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>

...
${user.home}/.m2/货物/集装箱

如果希望每个用户能够设置自己的默认值,还可以在user settings.xml文件中设置属性。我们使用这种方法来隐藏CI服务器用于常规开发人员的某些插件的凭据。

您可以在
/
或下面所示的配置文件中定义属性默认值。当在命令行上为属性值提供
-DmyProperty=anotherValue
时,它将覆盖POM中的定义。也就是说,POM中属性值的所有定义仅设置为属性的默认值

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

...
默认值
...
${myProperty}
...

@akostadinov的解决方案非常适合普通使用。。。但是,如果reactor组件在依赖项解析阶段(在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>

我的财产
!我的财产
${an.other.property}或\u静态\u值

我采用了萨尔的方法,但稍微平缓了一点

<profiles>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
     <plugin>
       <configuration>
        <version>LATEST</version>
       </configuration>
     </plugin>
    </build>
  </profile>
</profiles>

违约
真的
最新的
现在您有两个选项:

  • 使用默认值:MVN安装(所有$version将替换为最新版本)

  • 使用自己的值:MVN安装-p!默认值-Dversion=0.9(所有$version将为0.9)


  • 除非传递了no-D属性,否则不能使用激活块激活nodefault来简化这一过程。@djangofan你说得对。我试图用我的答案来代替这个问题。你能给我举一个普通的
    -D
    不起作用的pom例子吗?这比在我的例子中使用配置文件要简单。谢谢我认为在某些情况下,配置文件可能不是最简单的解决方案。请查看下面大卫瓦莱里的答案。@SimonTower,配置文件仅用作示例。如果使用
    -D
    ,则无论属性是否在配置文件中定义,都将覆盖该属性。
    <profiles>
      <profile>
        <id>default</id>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <build>
         <plugin>
           <configuration>
            <version>LATEST</version>
           </configuration>
         </plugin>
        </build>
      </profile>
    </profiles>