Effortlessly Update Maven Versions Across Hundreds of Modules
This guide shows two reliable Maven techniques—using the versions-maven-plugin and a placeholder with the flatten-maven-plugin—to safely update version numbers across dozens of modules without manual errors or risky global replacements.
Hello everyone, I'm sharing two practical tricks for quickly and safely updating Maven version numbers across many modules, avoiding the pitfalls of manual or blind global replace.
1
Use the versions-maven-plugin to manage versions.
Add the following plugin definition to your parent pom.xml :
<code><build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build></code>Then run a single command to set a new version for all modules:
<code>mvn versions:set -DnewVersion=1.2.1</code>If you need to revert the change, use:
<code>mvn versions:revert</code>Finally, commit the changes with:
<code>mvn versions:commit</code>This approach updates the parent reference and all child module versions consistently.
2
The second method relies on a placeholder managed in the parent POM.
Define a property such as ${revision} in the parent POM:
<code><properties>
<revision>2.8.5</revision>
</properties></code>Use the placeholder in child modules (see image). To make Maven replace the placeholder during build, add the flatten-maven-plugin :
<code><plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.7</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin></code>Now changing the revision value in the parent POM updates all modules automatically.
This pattern is used in my open‑source projects and greatly reduces the risk of version‑mismatch errors.
3
In everyday development, eliminating repetitive, error‑prone tasks boosts efficiency. If you found these tips helpful, consider starring the repository.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.