Update versions contained in README on maven release

You can use the maven-resources-plugin for this like mentioned in the comments.

I didn't try it but the configuration should look something like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.0.1</version>
  <executions>
    <execution>
      <id>readme-md</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.basedir}</outputDirectory>
        <resources>                                        
          <resource>
            <directory>${project.basedir}</directory>
            <includes>
              <include>README.md</include>
            </includes>
            <filtering>true</filtering>
          </resource>
        </resources>
        <encoding>UTF-8</encoding>
      </configuration>            
    </execution>
  </executions>
</plugin>

And in your README.md where you want the version you put the placeholder ${project.version}.

The two features which where combined here are copy-resources and filtering.

We tell the plugin to copy resources from the directory ${project.basedir}, which maven resolves to the root directory, again to the root directory but only include files matching README.md.

The filtering option replaces all placeholders with variables which can be defined as system properties, project properties, filter resources defined in the pom.xml or on command line. In this case we use the project property version.

Though the comments are right mentioning eventually inconsistency in case the release went wrong. You could overcome this by explicitly calling mvn resources:resources after your mvn release:perform was successful. I hope this helps.


Building onto Marvins answer,
you can keep your README.md in your usual place and include just a snippet of text.

The trick is that although Markdown does not allow to include text, it allows to include an svg-image and an svg-image can contain text that can be selected and copied in all browsers:

<svg xmlns="http://www.w3.org/2000/svg"
     xml:lang="en-GB" width="auto" height="7em">
    <style>
        svg {
            font-family: monospace;
            font-size: medium;
        }
    </style>

    <text x="0" y="1em">
        <tspan x="1em" dy="1em">&lt;dependency&gt;</tspan>
        <tspan x="3em" dy="1em">&lt;groupId&gt;${project.groupId}&lt;/groupId&gt;</tspan>
        <tspan x="3em" dy="1em">&lt;artifactId&gt;${project.artifactId}&lt;/artifactId&gt;</tspan>
        <tspan x="3em" dy="1em">&lt;version&gt;${project.version}&lt;/version&gt;</tspan>
        <tspan x="1em" dy="1em">&lt;/dependency&gt;</tspan>
    </text>
    
</svg>

This will render as:

<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>${project.artifactId}</artifactId>
  <version>${project.version}</version>
</dependency>

(It will become a one-liner on paste, but that's okay. pom.xml are usually auto-formatted anyway)

You put that image into a directory called "templates" and a copy into a "docimages"-directory.

Now you can add your image to the README.md using a normal image-reference:
![mvn dependency](docimages/version.svg)
Note you're referencing the one in your "docimages" directory, not the one in your "templates" dir.

Now you just need a job in your pom.xml that copies and filters the image's source code:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.0.1</version>
  <executions>
    <execution>
      <id>readme-md</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.basedir}/docimages</outputDirectory>
        <resources>                                        
          <resource>
            <directory>${project.basedir/templates}</directory>
            <includes>
              <include>version.svg</include>
            </includes>
            <filtering>true</filtering>
          </resource>
        </resources>
        <encoding>UTF-8</encoding>
      </configuration>            
    </execution>
  </executions>
</plugin>

"What's the benefit over filtering all of the readme?" You might ask.
Simple: A README.md can be large and can contain all kinds of variables as part of the documentation that you don't want to substitute. This will only substitute the variables in the SVG-file.

Tags:

Java

Maven