【问题标题】:Setting default values for custom Maven 2 properties为自定义 Maven 2 属性设置默认值
【发布时间】:2010-10-28 07:25:05
【问题描述】:

我有一个带有插件的 Maven pom.xml,我希望能够在命令行上控制它。一切正常,除了即使在网上搜索了一段时间后我也无法弄清楚如何为我的控件属性设置默认值:

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

所以如果我使用 Maven 运行

mvn -DmyProperty=something ...

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

【问题讨论】:

    标签: maven-2 properties


    【解决方案1】:

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

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

    【讨论】:

    • 我认为配置文件在某些​​情况下可能不是最简单的解决方案。在下面查看 DavidValeri 的答案。
    • @SimonTower,配置文件仅用作示例。如果您使用-D,那么无论属性是否在配置文件中定义,都将被覆盖。
    【解决方案2】:

    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>
    

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

    【讨论】:

    • 这比在我的情况下使用配置文件更简单。谢谢!
    【解决方案3】:

    您可以使用以下内容:

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

    【讨论】:

      【解决方案4】:

      @akostadinov 的解决方案非常适合常见用途...但是如果反应器组件在依赖关系解析阶段(在 mvn pom 层次结构处理的早期...)应使用所需的属性,则应使用配置文件“none激活”测试机制,以确保可选命令行提供的值始终优先考虑 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>
      

      【讨论】:

      • 您能否展示一个示例 pom,其中正常的 -D 不起作用?
      【解决方案5】:

      这可能对你有用:

      <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 将使用“foo”作为您的默认参数。如果需要覆盖,请使用mvn -P notdefault -DmyProperty=something

      【讨论】:

      • 这不能通过使用激活块来激活 nodefault 来简化一点,除非根本没有传递 -D 属性。
      • @djangofan 你是对的。我试图让我的答案替代问题。
      【解决方案6】:

      我采用了sal 的方法,但有点扁平化。

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

      现在你有两个选择:

      1. 使用默认值:MVN 安装(所有 $version 将替换为 LATEST)

      2. 使用自己的值:MVN install -P!默认 -Dversion=0.9(所有 $version 都是 0.9)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        相关资源
        最近更新 更多