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

Eem*_*ola 60 maven-2 properties

我有一个带有插件的Maven pom.xml,我希望能够在命令行上进行控制.一切都运行不错,除非搜索网络一段时间后我无法弄清楚如何设置我的控件属性的默认值:

<plugin>
    ...
    <configuration>
        <param>${myProperty}</param>
    </configuration>
    ...
</plugin>
Run Code Online (Sandbox Code Playgroud)

所以如果我用Maven运行

mvn -DmyProperty=something ...
Run Code Online (Sandbox Code Playgroud)

一切都很好,但我想在没有-DmyProperty=...开关的情况下为myProperty分配一个特定的值.如何才能做到这一点?

ako*_*nov 64

老问题,但我认为最简单的答案不存在.您可以<build>/<properties>在配置文件中或配置文件中定义属性默认值,如下所示.当您在命令行上提供属性值时-DmyProperty=anotherValue,它将覆盖POM中的定义.我希望我能够解释..

<profile>
    ...
    <properties>
        <myProperty>defaultValue</myProperty>            
    </properties>
    ...
       <configuration>
          <param>${myProperty}</param>
       </configuration>
    ...
</profile>
Run Code Online (Sandbox Code Playgroud)


Dav*_*eri 36

泰勒的方法很好,但你不需要额外的配置文件.您只需在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>
Run Code Online (Sandbox Code Playgroud)

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


Tay*_*ese 26

您可以使用以下内容:

<profile>
    <id>default</id>
    <properties>
        <env>default</env>
        <myProperty>someValue</myProperty>            
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>
Run Code Online (Sandbox Code Playgroud)


小智 6

@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>
Run Code Online (Sandbox Code Playgroud)