如何通过maven-version-rules.xml仅处理指定前缀的构件?
com.xxx.yyy Artifacts with Maven Versions Plugin Absolutely you can! The Maven Versions plugin lets you focus solely on artifacts with a group ID starting with com.xxx.yyy—you just need to configure it properly, either directly in your pom.xml or via the maven-version-rules.xml file you referenced. The key is to define a rule that matches your target group prefix and set a default rule to ignore all other artifacts.
1. Direct Configuration in pom.xml
You can embed the rules directly into your project's pom.xml. Here's how:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <!-- Use the latest stable version --> <configuration> <!-- Rule to process only artifacts with group ID starting with com.xxx.yyy --> <rules> <rule> <groupIdRegex>^com\.xxx\.yyy.*</groupIdRegex> <!-- Allow all versions (enables normal version check/update for these artifacts) --> <allowedVersions>.*</allowedVersions> </rule> </rules> <!-- Default rule: ignore version checks for all other artifacts --> <ruleSet> <rules> <rule> <groupIdRegex>.*</groupIdRegex> <ignoreVersions>.*</ignoreVersions> </rule> </rules> </ruleSet> </configuration> </plugin> </plugins> </build>
Key Notes:
- The
^com\.xxx\.yyy.*regex ensures we match group IDs that start exactly withcom.xxx.yyy(the backslashes escape the dots, since regex uses.to match any character). - Order matters! The specific target rule comes first, so it takes precedence over the default "ignore all" rule.
- The
<allowedVersions>.*</allowedVersions>tells the plugin to check all valid versions for the targeted artifacts—you can narrow this down to a version range if needed (e.g.,[1.0.0,2.0.0)).
2. Using maven-version-rules.xml
If you prefer to keep rules in a separate file (like you mentioned), create the maven-version-rules.xml in your project root with this content:
<ruleset xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 http://mojo.codehaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd"> <!-- Process only com.xxx.yyy prefixed artifacts --> <rule> <groupIdRegex>^com\.xxx\.yyy.*</groupIdRegex> <allowedVersions>.*</allowedVersions> </rule> <!-- Ignore all other artifacts --> <rule> <groupIdRegex>.*</groupIdRegex> <ignoreVersions>.*</ignoreVersions> </rule> </ruleset>
Then reference this file in your pom.xml's plugin configuration:
<configuration> <rulesUri>file://${basedir}/maven-version-rules.xml</rulesUri> </configuration>
Testing the Setup
Run any Versions plugin goal (like mvn versions:display-dependency-updates) and you’ll see only updates for com.xxx.yyy artifacts listed—all others will be skipped entirely.
内容的提问来源于stack exchange,提问作者Marx

