Maven version with a property

后端 未结 8 958
不思量自难忘°
不思量自难忘° 2020-12-03 00:21

I have big Maven (Tycho) project witch about 400 plug-ins.

We have specified version of application in each POM file.

Is there a way how to specify the versi

相关标签:
8条回答
  • 2020-12-03 01:01

    See the Maven - Users forum 'version' contains an expression but should be a constant. Better way to add a new version?:

    here is why this is a bad plan.

    the pom that gets deployed will not have the property value resolved, so anyone depending on that pom will pick up the dependency as being the string uninterpolated with the ${ } and much hilarity will ensue in your build process.

    in maven 2.1.0 and/or 2.2.0 an attempt was made to deploy poms with resolved properties... this broke more than expected, which is why those two versions are not recommended, 2.2.1 being the recommended 2.x version.

    0 讨论(0)
  • 2020-12-03 01:03

    With a Maven version of 3.5 or higher, you should be able to use a placeholder (e.g. ${revision}) in the parent section and inside the rest of the pom, you can use ${project.version}.

    Actually, you can also omit project properties outside of parent which are the same, as they will be inherited. The result would look something like this:

    <project>
        <parent>
        <artifactId>build.parent</artifactId>
        <groupId>company</groupId>
        <relativePath>../build.parent/pom.xml</relativePath>
        <version>${revision}</version>  <!-- use placeholder -->
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <artifactId>artifact</artifactId>
        <!-- no 'version', no 'groupId'; inherited from parent -->
        <packaging>eclipse-plugin</packaging>
    
        ...
    </project>
    

    For more information, especially on how to resolve the placeholder during publishing, see Maven CI Friendly Versions | Multi Module Setup.

    0 讨论(0)
  • 2020-12-03 01:03

    If you're using Maven 3, one option to work around this problem is to use the versions plugin http://www.mojohaus.org/versions-maven-plugin/

    Specifically the commands,

    mvn versions:set -DnewVersion=2.0-RELEASE
    mvn versions:commit
    

    This will update the parent and child poms to 2.0-RELEASE. You can run this as a build step before.

    Unlike the release plugin, it doesn't try to talk to your source control

    0 讨论(0)
  • 2020-12-03 01:11

    The correct answer is this (example version):

    • In parent pom.xml you should have (not inside properties):

      <version>0.0.1-SNAPSHOT</version>
      
    • In all child modules you should have:

      <parent>
          <groupId>com.vvirlan</groupId>
          <artifactId>grafiti</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </parent>
      

    So it is hardcoded.

    Now, to update the version you do this:

    mvn versions:set -DnewVersion=0.0.2-SNAPSHOT
    mvn versions:commit # Necessary to remove the backup file pom.xml
    

    and all your 400 modules will have the parent version updated.

    0 讨论(0)
  • 2020-12-03 01:13

    If you have a parent project you can set the version in the parent pom and in the children you can reference sibling libs with the ${project.version} or ${version} properties.

    If you want to avoid to repeat the version of the parent in each children: you can do this:

    <modelVersion>4.0.0</modelVersion>
    <groupId>company</groupId>
    <artifactId>build.parent</artifactId>
    <version>${my.version}</version>
    <packaging>pom</packaging>
    
    <properties>
    <my.version>1.1.2-SNAPSHOT</my.version>
    </properties>
    

    And then in your children pom you have to do:

        <parent>
          <artifactId>build.parent</artifactId>
          <groupId>company</groupId>
          <relativePath>../build.parent/pom.xml</relativePath>
          <version>${my.version}</version>
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <groupId>company</groupId>
        <artifactId>artifact</artifactId>
        <packaging>eclipse-plugin</packaging>
    
        <dependencies>
            <dependency> 
               <groupId>company</groupId>
               <artifactId>otherartifact</artifactId>   
               <version>${my.version}</version>
    or
               <version>${project.version}</version>
            </dependency>
        </dependencies>
    

    hth

    0 讨论(0)
  • 2020-12-03 01:21

    Using a property for the version generates the following warning:

    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for xxx.yyy.sandbox:Sandbox:war:0.1.0-SNAPSHOT
    [WARNING] 'version' contains an expression but should be a constant. @ xxx.yyy.sandbox:Sandbox:${my.version}, C:\Users\xxx\development\gwtsandbox\pom.xml, line 8, column 14
    [WARNING]
    [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
    [WARNING]
    [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
    [WARNING]
    

    If your problem is that you have to change the version in multiple places because you are switching versions, then the correct thing to do is to use the Maven Release Plugin that will do this for you automatically.

    0 讨论(0)
提交回复
热议问题