Toilal Toilal - 1 month ago 29
Java Question

Update versions contained in README on maven release

I'm maintaining an opensource project on github that contains

README.md
file with install instructions containing the current version of the project
1.0.0-SNAPSHOT
.

When releasing a new version to maven central, is it possible to automatically update this version number contained in
README.md
?

Releases are performed with
maven-release-plugin
, versions in pom.xml are well updated, but I can't find anything in the docs to update this external file properly.

Example:

README.md
file currently at
1.0.0-SNAPSHOT
. It lies outside of maven sources/resources, but it's managed on git. on
mvn release:prepare
, it should be updated to
1.0.0
, and maven should then commit/push the change before tagging the new release. Then, on
mvn release:perform
it should go to the next development version
1.0.1-SNAPSHOT
.

(Link to the project)

Answer

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.