如何更新Maven POM中的属性?

chr*_*ke- 26 pom.xml maven versions-maven-plugin

我有两个顶级Maven项目,backend并且frontend以各自的速度推进版本.由于每个模块都有多个模块,因此我dependencyManagement在父/聚合POM 中的部分中定义了我的依赖项版本,并使用属性作为版本号.

我想用版本号干净地更新属性frontend,最好随意更新,但我可以忍受要求实时上游版本匹配.我尝试过使用versions:update-property,但这个目标似乎完全不起作用; 无论是否真的有匹配的上游版本,我得到这个调试输出:

$ mvn versions:update-property -Dproperty=frontend.version -DnewVersion=0.13.2  -DautoLinkItems=false -X
...
[DEBUG] Searching for properties associated with builders
[DEBUG] Property ${frontend.version}
[DEBUG] Property ${frontend.version}: Looks like this property is not associated with any dependency...
[DEBUG] Property ${frontend.version}: Set of valid available versions is [0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.9.4, 0.9.5, 0.10.0, 0.10.1, 0.11.0, 0.12.0, 0.13.0, 0.13.1, 0.13.2, 0.13.3]
[DEBUG] Property ${frontend.version}: Restricting results to 0.13.2
[DEBUG] Property ${frontend.version}: Current winner is: null
[DEBUG] Property ${frontend.version}: Searching reactor for a valid version...
[DEBUG] Property ${frontend.version}: Set of valid available versions from the reactor is []
[INFO] Property ${frontend.version}: Leaving unchanged as 0.13.1
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我已经指定了-DautoLinkItems=false,这似乎没有效果; versions-maven-plugin仍会扫描我的所有POM以查找匹配的依赖项,然后退出并退出.我也试着设置searchReactorfalse在插件配置该属性.看来插件(1)错误地扫描依赖关系,即使我明确地说要忽略它们,(2)甚至过滤掉明确的特定匹配.

有没有一种简单的方法可以将Maven属性条目重写为特定值,或者强制versions-maven-plugin执行我所说的而不验证版本号或使用其他目标?我宁愿避免使用sed不懂XML 的工具(正如我在类似问题中看到的那样推荐),但我可以使用简单的XPath操作.

Geo*_*sty 21

有没有一种简单的方法可以将Maven属性条目重写为特定值

从版本2.5我们可以使用set-property(文档):

mvn versions:set-property -Dproperty=your.property -DnewVersion=arbitrary_value

如文档所述,set-property目标不会对您指定的值执行任何"完整性检查",因此它应始终有效,但您应谨慎使用.

  • 非常感谢,该文档太荒谬了。如果我没看错,但没有在插件http://www.mojohaus.org/versions-maven-plugin/index.html的文档页面上看到,就没有提及设置属性-还是我盲目? (2认同)

dre*_*our 15

newVersion参数记录严密(与此插件的大部分内容一样).通过检查集成测试,我发现它需要Maven版本范围而不是简单的版本号.此外,它不允许您提供任何值 - 它必须是Maven可以解析的有效值.如果被调用,参数会更好constrainRange

对于将来的其他人,请试试这个:

mvn versions:update-property -Dproperty=frontend.version -DnewVersion=[0.13.2]  
Run Code Online (Sandbox Code Playgroud)

如果需要更新到快照,请确保将该属性设置allowSnapshots为true

mvn versions:update-property -Dproperty=frontend.version -DnewVersion=[0.13.2] -DallowSnapshots=true
Run Code Online (Sandbox Code Playgroud)


arg*_*ype 5

How to update property in existing POM:

Try to use filtering in maven-resource-plugin:

  1. specify version in property file;
  2. add custom filter with path to this file (in child pom.xml, where dependency should be injected);
  3. update version in property file;
  4. run build.

Advantages:

  • it should work;
  • version is specified only once;
  • property file could be added under version control;
  • process-resources is one of the first maven lifecycle steps.

Disadvantages:

  • well, pom.xml still uses placeholder;
  • additional work to automatically update property file from initial build (too complicated, I suppose there should be easier solutions).

How to provide propery on build time:

You could specify any property by build parameter.

For example, I have property in my pom.xml like:

<properties>
    <build.date>TODAY</build.date>
</properties>
Run Code Online (Sandbox Code Playgroud)

To change it during build I simply use parameter:

mvn compile -Dbuild.date=10.10.2010
Run Code Online (Sandbox Code Playgroud)

I'm pretty sure it will work for version as well. Also, properties from top level projects are inherited by childs.


归档时间:

查看次数:

22800 次

最近记录:

7 年,10 月 前