如何解决versions-maven-plugin的use-latest-releases未更新依赖版本致构建失败?
First, let's break down why your build is failing: When you run mvn clean package, Maven tries to resolve dependencies before any custom plugin goals (like use-latest-releases) execute. Your placeholder [anyVersionNumber] isn't a valid Maven version expression, so Maven can't locate the artifact at all. The versions-maven-plugin never got a chance to update the version because you didn't trigger its goal properly.
Here's how to correctly use the plugin to update your dependencies to the latest non-SNAPSHOT releases:
1. Configure the Plugin in Your pom.xml
You have two options: run the plugin manually, or bind it to a Maven lifecycle stage so it runs automatically before dependency resolution.
Option A: Manual Execution (Great for Testing)
Add the plugin without lifecycle binding to run it on demand:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <!-- Use the latest stable plugin version --> <configuration> <!-- Optional: Restrict updates to specific dependencies (e.g., your org's artifacts) --> <includes> <include>your.group.id:*</include> </includes> <!-- Default is true, but explicit to confirm we skip SNAPSHOTs --> <excludeSnapshots>true</excludeSnapshots> </configuration> </plugin> </plugins> </build>
Option B: Auto-Execute Before Dependency Resolution
Bind the plugin to the validate stage (which runs before Maven resolves dependencies) so it updates versions automatically when you run your build:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <executions> <execution> <id>update-to-latest-releases</id> <phase>validate</phase> <!-- Runs early, before dependency resolution --> <goals> <goal>use-latest-releases</goal> </goals> <configuration> <excludeSnapshots>true</excludeSnapshots> <!-- Add includes/excludes as needed --> </configuration> </execution> </executions> </plugin> </plugins> </build>
2. Fix Your Dependency Version Placeholder
Don't use [anyVersionNumber] — Maven can't parse this. Instead, use either:
- A specific older version (the plugin will overwrite this with the latest release):
<dependency> <groupId>your.group.id</groupId> <artifactId>some-artifact</artifactId> <version>1.0.0</version> <!-- Old version to be updated --> </dependency> - A valid version range (the plugin will still replace it with the latest concrete release):
<dependency> <groupId>your.group.id</groupId> <artifactId>some-artifact</artifactId> <version>[1.0,)</version> <!-- Range for 1.0 and above --> </dependency>
3. Run the Correct Commands
If Using Manual Execution:
First, run the plugin to update your pom.xml directly:
mvn versions:use-latest-releases
This will replace dependency versions with the latest non-SNAPSHOT releases. Then run your build:
mvn clean package
If Using Auto-Execute (Lifecycle Binding):
Just run your build command directly — the plugin will automatically update versions during the validate phase before dependencies are resolved:
mvn clean package
Key Notes:
- The
use-latest-releasesgoal only considers non-SNAPSHOT versions by default, so you won't accidentally pull in unstable builds. - To preview changes without modifying your
pom.xml, usemvn versions:display-latest-releasesfirst — it shows available updates without making edits. - Ensure your Maven
settings.xmlis configured to access all necessary repositories (public Maven Central, internal repos, etc.) where your dependencies are hosted.
内容的提问来源于stack exchange,提问作者Sam




