[MNG-8425] Fix mvnenc init saving invalid master source configuration#12418
[MNG-8425] Fix mvnenc init saving invalid master source configuration#12418Hiteshsai007 wants to merge 2 commits into
Conversation
|
@elharo Thanks for the review! I have addressed your feedback by adding comprehensive unit tests for the configuration saving logic. To accomplish this, I:
Let me know if there's anything else needed before we can merge this! |
elharo
left a comment
There was a problem hiding this comment.
This should be testable through the existing API or maybe an integration test. This approach is a red flag. I would start over by reproducing the issue in a failing test, and then implement a fix.
Reimplemented the fix inline and added an API test using Mockito to mock the interactive ConsolePrompt, directly addressing reviewer feedback.
cd6877a to
67473b7
Compare
|
@elharo Thank you for the feedback. That is a completely fair point—extracting an internal static method purely to make it testable was definitely a smell. I've pushed a new commit that starts over and follows your suggested approach:
All |
gnodet
left a comment
There was a problem hiding this comment.
Thanks for tackling MNG-8425 — the root-cause analysis is spot-on and the fix targets the right place. The prefix-extraction approach is correct for all known MasterSource types. A few things to address before this can be merged:
Missing edge-case test coverage
The single test covers only the happy path (user omits the prefix). Please add tests for:
- User already includes the prefix (e.g., types
env:MY_VAR) — verifies no double-prepending - Different prefix type (e.g., template
system-property:$systemProperty) — verifies it works beyondenv: - Empty prefix (e.g., template
$VAR) — verifies user input is used as-is
Inline fully-qualified class names in test
Several types are referenced by FQCN inline rather than imported at the top of the file (e.g., java.nio.file.Paths, java.util.ArrayList, org.jline.utils.AttributedStyle, java.io.PrintWriter, org.jline.consoleui.prompt.ConfirmResult, org.jline.consoleui.elements.ConfirmChoice). Other tests in impl/maven-cli use proper imports — please follow the same convention.
See the inline comments for the remaining items.
Note: this review does not replace specialized AI review tools (CodeRabbit, Sourcery) or static analyzers (SonarCloud).
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
| final String finalResult = userInput; | ||
| dispatcherConfigResult.put(editable.getKey(), new PromptResultItemIF() { | ||
| @Override | ||
| public String getResult() { | ||
| return finalResult; | ||
| } | ||
| }); |
There was a problem hiding this comment.
PromptResultItemIF is structurally a functional interface (single abstract method getResult(), one default method). The anonymous inner class can be replaced with a lambda, which also eliminates the need for the finalResult intermediate variable:
| final String finalResult = userInput; | |
| dispatcherConfigResult.put(editable.getKey(), new PromptResultItemIF() { | |
| @Override | |
| public String getResult() { | |
| return finalResult; | |
| } | |
| }); | |
| String result = userInput; | |
| dispatcherConfigResult.put(editable.getKey(), () -> result); |
Same simplification applies to the createResult() helper in the test file.
| if (template.contains("$")) { | ||
| String prefix = template.substring(0, template.indexOf('$')); | ||
| if (!prefix.isEmpty() && !userInput.startsWith(prefix)) { | ||
| userInput = prefix + userInput; | ||
| } | ||
| } |
There was a problem hiding this comment.
This template.contains("$") guard is always true here — the editables list (line 127) is already filtered to entries whose result contains $, and template is assigned from editable.getValue().getResult(). The redundant check adds unnecessary nesting. Consider removing it:
| if (template.contains("$")) { | |
| String prefix = template.substring(0, template.indexOf('$')); | |
| if (!prefix.isEmpty() && !userInput.startsWith(prefix)) { | |
| userInput = prefix + userInput; | |
| } | |
| } | |
| String prefix = template.substring(0, template.indexOf('$')); | |
| if (!prefix.isEmpty() && !userInput.startsWith(prefix)) { | |
| userInput = prefix + userInput; | |
| } |
|
|
||
| private PromptResultItemIF createResult(String value) { | ||
| return new PromptResultItemIF() { | ||
| @Override |
There was a problem hiding this comment.
The createResult() helper at the bottom of this file also uses an anonymous inner class where a lambda would suffice:
private PromptResultItemIF createResult(String value) {
return () -> value;
}|
Thanks for the thorough review! I've gone ahead and addressed all the feedback. Here is a summary of the changes made in the latest commit:
Let me know if there's anything else you'd like me to adjust! |
There was a problem hiding this comment.
Pull request overview
This PR fixes the Maven encryption wizard (mvn --enc init) so that editable master-source configuration values persist with their required source prefix (e.g., env:), preventing generation of invalid settings-security4.xml that would later cause mvn --enc encrypt to report “Maven Encryption is not configured.”
Changes:
- Update
Initto automatically prepend the template-derived prefix (text before$) to the user’s edited value when missing. - Add a parameterized unit test covering prefixed and already-prefixed inputs (and a no-prefix template case).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java | Prepends the template prefix (before $) to editable user input when absent, ensuring stored master-source values remain valid. |
| impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnenc/goals/InitTest.java | Adds parameterized coverage validating the prefix-prepend behavior for multiple template/input combinations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
gnodet
left a comment
There was a problem hiding this comment.
All review comments have been addressed — nice work!
- Redundant
template.contains("$")check removed ✅ - Anonymous inner classes replaced with lambdas ✅
- Inline FQCNs converted to proper imports ✅
- Parameterized test now covers: prefix omitted, prefix already present, different prefix type (
system-property:), and empty prefix ($VAR) ✅
The fix is clean and well-tested. Thanks for the thorough follow-up.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
[MNG-8425] Fix mvnenc init saving invalid master source configuration
Fixes #10202
Description
This PR fixes MNG-8425 (#10202) where
mvn --enc initgenerates an invalidsettings-security4.xml, causingmvn --enc encryptto fail with "Maven Encryption is not configured."Root Cause
When the encryption wizard prompts the user to customize an editable value, the template is presented as
env:$VARIABLE_NAME. The user is expected to replace only the$VARIABLE_NAMEportion (e.g., typingMVN_PASSWORD), but theInitgoal saves the raw user input directly — resulting in<value>MVN_PASSWORD</value>instead of the correct<value>env:MVN_PASSWORD</value>.The
MasterDispatcherinplexus-sec-dispatcherthen fails validation because noMasterSourcerecognizes a config string without its required prefix (env:,sys-property:, etc.), producing the error:Configured Source configuration not handled.Fix
In
Init.java, after the user provides their input for an editable field, the code now:$placeholder).This ensures the generated
settings-security4.xmlalways contains well-formed source values likeenv:MVN_PASSWORD.Testing
maven-clipass.mvn --enc initnow writes the correctenv:prefix andmvn --enc encryptsucceeds afterward.Your pull request should address just one issue, without pulling in other changes.
Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
Each commit in the pull request should have a meaningful subject line and body.
Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
Run
mvn verifyto make sure basic checks pass.You have run the Core IT successfully.
I hereby declare this contribution to be licenced under the Apache License Version 2.0, January 2004