Skip to content

[3.10.x] Accept the ? optional-profile prefix in -P - #13480

Merged
cstamas merged 2 commits into
maven-3.10.xfrom
agent/optional-profiles-3.10
Oct 20, 2026
Merged

cstamas merged 2 commits into
maven-3.10.xfrom
agent/optional-profiles-3.10

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #13479. Maven 4 reads -P ?id as an optional profile (MNG-7051); Maven 3.10 read ?id as a profile literally named ?id and left id off, so a build passing -P ?run-its to both Maven 3 and 4 silently skipped the profile on Maven 3. The ? is now removed after the +, - and ! prefixes, in the same order as Maven 4, so ?id, +?id, -?id and !?id work; a token that is empty after the prefixes (-P ?, -P -?) is ignored. A missing optional profile still logs the existing "could not be activated" warning, now with the plain id: Maven 3 does not fail on missing profiles, and dropping the warning would need the optional flag on MavenExecutionRequest. The parsing moves into a package-private performProfileActivation, as in Maven 4, so it can be unit tested.

Each token is trimmed before its prefixes are read, as Maven 4 does once #13502 fixes its + check, which read the untrimmed token; empty tokens such as a,,b stay ignored, as in both lines.

All -P sources (the command line, .mvn/maven.config and MAVEN_ARGS) go through this method. maven-3.9.x keeps the old parsing, since that line takes serious bug fixes only.

Verified: a 3.10.1-SNAPSHOT build activates the profile with -P ?run-its where 3.10.0 does not, deactivates it with !?run-its, and succeeds with -P ?missing; mvn -pl maven-embedder test → 43 tests pass, including testOptionalProfiles and testDegenerateProfileTokens.


Maven 4 reads -P ?id as an optional profile; Maven 3 read it as a profile named ?id and left id off. The prefix is now removed after the +, - and ! prefixes, in the same order as Maven 4.
@slachiewicz slachiewicz added mvn3 enhancement New feature or request labels Oct 13, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review October 13, 2026 15:44
@slachiewicz slachiewicz added this to the 3.10.1 milestone Oct 13, 2026

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new performProfileActivation method is missing an isEmpty() guard after stripping all prefixes. Degenerate inputs like -P ?, -P -?, or -P ! strip down to an empty string that is silently inserted into the active/inactive profile list. Maven 4's equivalent method guards each substring call with !profileId.isEmpty() before adding — this backport should do the same.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on my earlier review — both issues have been addressed.

isEmpty() guard — ✅ addressed. The new commit adds if (profileId.isEmpty()) { continue; } after the ? stripping, which correctly skips empty-string tokens for all degenerate inputs (-P ?, -P -?, -P !?, bare -P -, etc.).

testDegenerateProfileTokens — ✅ addressed. The new test exercises ?,!,+,-,+?,-?,!?, and asserts both active and inactive lists remain empty, which directly validates the guard.

Pre-fix validation: static trace confirms testDegenerateProfileTokens was failing before the fix — token ? strips to "", without the guard activeProfiles.add("") is called, and assertTrue(active.isEmpty()) fails.

LGTM.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet

gnodet commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@slachiewicz why is the difference with the trim kept on purpose ?

@slachiewicz

Copy link
Copy Markdown
Member Author

@slachiewicz why is the difference with the trim kept on purpose ?

The 3.x line has always trimmed each token before reading its +, - or ! prefix; the old code removed by the PR does nextToken().trim(). Maven 4 trims too, and its -/! check reads the trimmed id. Only its + check reads the raw token (token.charAt(0) == '+'), and that looks like a slip rather than a design choice. With -P "a, +b", Maven 4 tries to activate a profile literally named +b. Master has the same check twice in MavenInvoker.java (lines 405 and 432), presumably once for profiles and once for projects. Matching Maven 4 would mean copying that slip into 3.10.

The trim isn’t new: 3.x has always trimmed each token before reading its prefix (the removed nextToken().trim()), and Maven 4 trims too. The difference is only in Maven 4’s + check, which reads token.charAt(0) instead of the trimmed profileId, so -P "a, +b" asks for a profile named +b there while -P "a, -b" deactivates b.

That looks like a slip rather than intent; I can open a PR to make MavenInvoker check profileId on master and 4.0.x so both lines agree.

@gnodet gnodet added the bug Something isn't working label Oct 14, 2026
@gnodet

gnodet commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

@slachiewicz why is the difference with the trim kept on purpose ?

The 3.x line has always trimmed each token before reading its +, - or ! prefix; the old code removed by the PR does nextToken().trim(). Maven 4 trims too, and its -/! check reads the trimmed id. Only its + check reads the raw token (token.charAt(0) == '+'), and that looks like a slip rather than a design choice. With -P "a, +b", Maven 4 tries to activate a profile literally named +b. Master has the same check twice in MavenInvoker.java (lines 405 and 432), presumably once for profiles and once for projects. Matching Maven 4 would mean copying that slip into 3.10.

The trim isn’t new: 3.x has always trimmed each token before reading its prefix (the removed nextToken().trim()), and Maven 4 trims too. The difference is only in Maven 4’s + check, which reads token.charAt(0) instead of the trimmed profileId, so -P "a, +b" asks for a profile named +b there while -P "a, -b" deactivates b.

That looks like a slip rather than intent; I can open a PR to make MavenInvoker check profileId on master and 4.0.x so both lines agree.

Yes, that sounds like a good idea.

@gnodet gnodet changed the title Accept the ? optional-profile prefix in -P [3.10.x] Accept the ? optional-profile prefix in -P Oct 19, 2026
@cstamas cstamas linked an issue Oct 20, 2026 that may be closed by this pull request
@cstamas
cstamas merged commit b6d62df into maven-3.10.x Oct 20, 2026
21 of 22 checks passed
@cstamas
cstamas deleted the agent/optional-profiles-3.10 branch October 20, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request mvn3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accept the ? optional-profile prefix on Maven 3.10

5 participants