Maven 动态更改属性(运行时)

Shi*_*ude 5 properties maven

我遇到了以下问题:我希望能够使用插件即时更改 Maven 属性。例如,如果我配置了一个属性,例如:

<properties>
   <someProperty>value</someProperty>
</properties>
Run Code Online (Sandbox Code Playgroud)

我想有一种方法在 maven 运行期间将其更改为“value-2”(不是之前!例如,我不想在命令行中使用 -DsomeProperty=value-2)

我将尝试解释我的用例以澄清:我的 pom.xml 设置了一些默认属性。但是,我正在运行一个任务,该任务创建一个带有 name=value 对的属性文件以匹配我的属性(例如,如果我的 pom.xml 中有诸如 someProperty=value 之类的属性,则 run.properties 文件具有 someProperty=value- 2 财产)。在某些 maven 运行期间,我想将我的属性文件的位置传递给它,并让它更改我的 pom.xml 中的默认属性。我曾尝试使用“properties-maven-plugin”来实现这一目标,但这似乎只有在我没有在 pom.xml 本身中配置属性时才有效。也就是说,如果我的 pom.xml 中的部分没有“someProperty”属性,则更改成功。如果我有的话

有什么建议?提前致谢

Gui*_*ont 2

我不知道我是否完全理解你的问题,但你可以尝试Groovy Maven 插件

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <id>add-dynamic-properties</id>
        <phase>initialize</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>
            if (someCondition) {
                project.properties.myDynamicProperty = 'myDynamicValue'
            }
          </source>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

然后,只需使用${myDynamicProperty}.

希望这可以帮助。