Eliminate Maven POM redundancy

狂风中的少年 提交于 2019-12-21 19:44:57

问题


I have a parent POM with the following config

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.2-SNAPSHOT</version>

Each child project which inherits from this parent POM, has a config such as the following:

<parent>
    <groupId>com.amadeus.jcp.ui.skinning.skinning-system</groupId>
    <artifactId>parent</artifactId>
    <version>1.2-SNAPSHOT</version>
</parent>

I want all these project version numbers to stay in synch. At the moment, if I change the parent version to 1.3, I then have to go change all the child project versions to 1.3. Is there any way I can avoid duplicating the version numbers in all the child projects?

I tried replacing the above with

<parent>
    <groupId>com.amadeus.jcp.ui.skinning.skinning-system</groupId>
    <artifactId>parent</artifactId>
</parent>

and

<parent>
    <groupId>com.amadeus.jcp.ui.skinning.skinning-system</groupId>
    <artifactId>parent</artifactId>
    <version>${project.version}</version>
</parent>

But neither of these work. I'm using Maven version 2.1.0.

Thanks, Don


回答1:


I tried replacing the above with (...) and (...) but neither of these work. I'm using Maven version 2.1.0.

Not possible. With Maven 2.x, you must declare the parent element in child module and the parent element must include a hard-coded version (you can't omit it and you can't use a property, see this previous answer and MNG-624).

However, Maven 3.1 will supports versionless parent elements (see this previous answer).

Meanwhile, some plugin can make the maintenance a bit easier like the Maven Release Plugin or the Versions Maven Plugin and its versions:set or versions:update-child-modules goals.




回答2:


You shouldn't define a version... in the child module, but you have to give that in the parent definition...

+--- root
      +--- pom.xml (Parent)
      +--- module1
             +-- pom.xml (child 1)

You should also define a relativePath in your childs as well...

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.2-SNAPSHOT</version>
    <relativePath>../pom.xml</version>
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>

If you done so you should never change the version number by hand in your parent...let the release plugin do the work it will change the version entries in all childs (parents) appropietaley...if you really need to change things sometimes use the maven-version-plugin...



来源:https://stackoverflow.com/questions/3203088/eliminate-maven-pom-redundancy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!