Repository navigation
[#8909] Implement powershell mvn command - #982
JurrianFahner wants to merge 5 commits into
Conversation
|
Core IT tests fails currently on It doesn't seem to be related to the changes made in this pull request. |
|
After pulling the latest version of the core-its, it was running fine. So the core-its ran successfully. |
|
@slachiewicz see here the pull request concerning the 3.9.x branch |
|
I wonder if it would make sense to remove the |
That will likely break things so maybe that should be targeted for v4 |
Yes, I had not seen this PR was targeting 3.9.x specifically. |
I'd require this to be on master first and then backported...at most |
|
@michael-o Should we remove |
No, since the PR is only about addtion of a PS-based solution. No more, no less. |
|
@michael-o @slachiewicz What do we need to do to merge this merge request? |
|
@michael-o Can I do something to make this pull request merged? |
|
Not now, ATM. I need to consult also a colleague who's proficient in PowerShell. |
Is there some progress? |
I hope to pick this up when my round with reporting stack is complete for next milestone. I guess somewere end of month. Please also raise your voice on dev@ to drag more attraction to this topic. |
Is there some progress? |
|
For the time being, I won't work on this since I don't have the capacity nor decent PowerShell scripting skill set. My knowledge in PS humble. |
|
@michael-o I understand your reserves, certainly if your see your PS knowledge as humble (I don't see myself as an expert either). Please let me know what is needed, then I can specifically ask for input/help on the mailing list. |
Giovds
left a comment
There was a problem hiding this comment.
I would love some powershell support.
| #> | ||
|
|
||
| # set title | ||
| $Host.UI.RawUI.WindowTitle = (Get-Variable MyInvocation -Scope Script).Value.InvocationName |
There was a problem hiding this comment.
It would be nice to reset the title, otherwise your shell will keep showing some-path\mvn.p1 long after maven is done unless you open a new window. It could also act as a visual indication maven is done if you have multiple tabs open.
There was a problem hiding this comment.
@Giovds It is only part of mvnDebug, it shouldn't be part of mvn.ps1 IMHO. I like your idea, but it shouldn't be part of this PR, but in a separate issue.
For now I'll remove this from mvn.ps1.
| } | ||
|
|
||
| if (-not (Test-path $env:JAVA_HOME)) { | ||
| $JAVACMD = (get-command java).Source |
There was a problem hiding this comment.
I'm pretty sure Get-Command throws an exception if a command is not found. This would hard exit here and not show the clearer error on line 42
There was a problem hiding this comment.
I agree! We need to do a try-catch here.
| $Host.UI.RawUI.WindowTitle = (Get-Variable MyInvocation -Scope Script).Value.InvocationName | ||
|
|
||
| if (-not $env:MAVEN_SKIP_RC) { | ||
| if (Test-Path -Path $env:PROGRAMDATA\mavenrc.ps1 -PathType Leaf) { &$env:PROGRAMDATA"\mavenrc.ps1" $args } |
There was a problem hiding this comment.
Should, at least for 3.9.x, it still support the mavenrc_pre.ps1 and mavenrc_post.ps1 convention, as they are also still supported (with deprecation warning) in 4.x. @michael-o @slachiewicz ?
There was a problem hiding this comment.
I wouldn't do it. New script, new behavior.
|
|
||
| $i = 0 | ||
| $file_flag_found = $false | ||
| foreach ($arg in $args) { |
There was a problem hiding this comment.
You could consider using named parameters here.
If we start the script with param ($file, $f) we can use auto completion from the command line. We could also get rid of the loop and the index $i. Throughout the script you can just reference $file and/or $f.
Do note it will behave a little different on line 156 as the $args does not contain the named parameters. Also mvnDebug.ps1 needs a different invocation.
There was a problem hiding this comment.
The nice thing about the choice I made is that the command line parameters are consistent through all shells. With using named parameters, the parameters will have only one -.
It would also require changing CLIManager, the changes would become bigger than I anticipated.
| } | ||
|
|
||
| # check mvn home | ||
| $MAVEN_HOME = (Get-Item $PSScriptRoot"\..") |
There was a problem hiding this comment.
You could consider using Join-Path so that we don't have to worry about manually inserting / or \ and the direction they are pointing. To go up a directory you could use (Get-Item $path).Parent (only works on directories).
|
|
||
| $MAVEN_OPTS = (RetrieveContentsJvmConfig $basedir) | ||
|
|
||
| $LAUNCHER_JAR = Get-Item $MAVEN_HOME"\boot\plexus-classworlds*.jar" |
There was a problem hiding this comment.
If there are multiple jars (as the * suggests), I think this will fail as the separator would be a space.
(Note the .cmd has an extra - and does not filter on .jar)
There was a problem hiding this comment.
No, there is one jar. This is to be sure it can find the classworlds jar (even when the version changes).
There was a problem hiding this comment.
No there is only one jar. The * is there if the version changes, the script will stil work.
70a7ad5 to
d4a9acc
Compare
Wouldn't it not be better to get master reviewed first and then retrofit the approach in master to the 3.9.x branch? Please be aware that there will be two different approaches in the component.xml, which makes reviewing a little bit harder. |
True, but the content of the script won't change, only its arrangement. |
Ok, in that case we are aligned. I'll update the master branch with the improvements on this branch. When the master is merged, I'll retrofit the changes into this branch. @Giovds Are you available for reviewing the master PR? I first need to apply the changes on this branch to the master (#878). |
|
Resolve #8909 |
gnodet
left a comment
There was a problem hiding this comment.
AI Review — PR #982: [MNG-7541] Implement powershell mvn command (3.9.x)
This PR adds mvn.ps1 and mvnDebug.ps1 PowerShell scripts for Maven 3.9.x. It has been stalled since September 2023 with outstanding CHANGES_REQUESTED from committer michael-o and unresolved inline comments from Giovds.
Bugs
1. Syntax error — unbalanced parentheses — The IsNotRoot function has mismatched parentheses on both lines:
$root_path_base_name = ((get-item $path).Root.BaseName # 2 open, 1 close
$path_base_name = ((get-item $path).BaseName # 2 open, 1 closeThis is a parse error — the script will fail to load. Fix: use single opening paren.
2. Test-Path on null $env:JAVA_HOME — When JAVA_HOME is not set, Test-Path $null throws a ParameterBindingValidationException rather than returning $false. The fallback logic to find java on PATH won't work.
3. $MAVEN_ARGS vs $env:MAVEN_ARGS — Line 149 references $MAVEN_ARGS (local variable, never assigned) instead of $env:MAVEN_ARGS. The MAVEN_ARGS environment variable will be silently ignored.
4. $MAVEN_DEBUG_OPTS scope mismatch — mvnDebug.ps1 sets $env:MAVEN_DEBUG_OPTS (environment variable) but mvn.ps1 reads $MAVEN_DEBUG_OPTS (local variable). Debug options are silently dropped when using mvnDebug.ps1.
Other Issues
- Path construction: Uses string concatenation (
$env:JAVA_HOME + "\bin\java.exe") instead ofJoin-Path. Fragile with paths containing spaces. - Wildcard classpath:
Get-Item $MAVEN_HOME"\boot\plexus-classworlds*.jar"may return an array if multiple jars exist. - Debug address: Comment says
localhost:8000but code defaults to"8000"(listens on all interfaces on Java 9+). - Missing exit code propagation: No
exit $LASTEXITCODEafter Java process invocation.
Process Note
Committer michael-o requested that PR #878 (targeting master) be merged first and this PR be a backport. Both PRs share the same IsNotRoot syntax error. Several inline review comments from Giovds also remain unresolved.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
Reviewed 3 PRs: apache#982 (PowerShell 3.9.x, syntax bugs), apache#878 (PowerShell master, needs Maven 4 rewrite), #33 (qualityManagement, oldest PR at 11 years). 45 reviews posted total. Only 3 truly unreviewed PRs remain: apache#2277, apache#1774, #71 (all large). Co-Authored-By: Claude Opus 4.6 <[email protected]>
| return $root_path_base_name -ne $path_base_name | ||
| } | ||
|
|
||
| function RetrieveContentsJvmConfig { |
There was a problem hiding this comment.
test comment on line 92
| } catch { | ||
| Write-Error -ErrorAction Stop -Message "The $env:JAVA_HOME has not been set, JAVA_HOME environment variable is not defined correctly, so Apache Maven cannot be started." | ||
| } | ||
| } |
| if (-not (Test-Path $JAVACMD)) { | ||
| Write-Error -Message "The JAVA_HOME environment variable is not defined correctly, so Apache Maven cannot be started." | ||
| $ERROR_MESSAGE = "JAVA_HOME is set to " + $env:JAVA_HOME + ", but " + $JAVACMD + " does not exist." | ||
| Write-Error -ErrorAction Stop -Message $ERROR_MESSAGE |
| return $root_path_base_name -ne $path_base_name | ||
| } | ||
|
|
||
| function RetrieveContentsJvmConfig { |
| } | ||
|
|
||
| function RetrieveContentsJvmConfig { | ||
| param ( |
| "-Dmaven.home=$MAVEN_HOME" ` | ||
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` | ||
| "-Dmaven.multiModuleProjectDirectory=$basedir" ` | ||
| $LAUNCHER_CLASS ` |
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` | ||
| "-Dmaven.multiModuleProjectDirectory=$basedir" ` | ||
| $LAUNCHER_CLASS ` | ||
| $MAVEN_ARGS ` |
gnodet-bot
left a comment
There was a problem hiding this comment.
Review — [MNG-7541] PowerShell mvn command (3.9.x)
This PR has several bugs that will cause incorrect runtime behavior or parse failures. Most critically, the IsNotRoot function has a syntax error that prevents the script from loading at all.
Note also that PR #878 (master) — the same feature targeting master — already fixes several of the issues below (Join-Path for path construction, "localhost:8000" as debug default, $env:MAVEN_ARGS). @michael-o's process objection still stands: that PR should be finalized first, then this 3.9.x PR updated to match.
This review was generated by an AI agent, Hermès on behalf of @gnodet.
| $root_path_base_name = ((get-item $path).Root.BaseName | ||
| $path_base_name = ((get-item $path).BaseName |
There was a problem hiding this comment.
⛔ Parse error — unbalanced parentheses. Both lines have two opening parens and one closing paren. PowerShell will refuse to parse this script entirely — IsNotRoot never loads, the .mvn directory traversal never executes, and $basedir is never set correctly.
| $root_path_base_name = ((get-item $path).Root.BaseName | |
| $path_base_name = ((get-item $path).BaseName | |
| $root_path_base_name = (Get-Item $path).Root.BaseName | |
| $path_base_name = (Get-Item $path).BaseName |
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` | ||
| "-Dmaven.multiModuleProjectDirectory=$basedir" ` | ||
| $LAUNCHER_CLASS ` | ||
| $MAVEN_ARGS ` |
There was a problem hiding this comment.
⛔ $MAVEN_ARGS is an unset local variable — MAVEN_ARGS environment variable is silently ignored. The correct form is $env:MAVEN_ARGS. PR #878 (run.ps1) already has this fix.
| $MAVEN_ARGS ` | |
| $env:MAVEN_ARGS ` |
|
|
||
| & $JAVACMD ` | ||
| $MAVEN_OPTS ` | ||
| $MAVEN_DEBUG_OPTS ` |
There was a problem hiding this comment.
⛔ Scope mismatch with mvnDebug.ps1. mvnDebug.ps1 sets $env:MAVEN_DEBUG_OPTS (an environment variable), but this line reads $MAVEN_DEBUG_OPTS (a local PowerShell variable never assigned in mvn.ps1). Debug JVM flags are silently dropped when launching via mvnDebug.ps1.
| $MAVEN_DEBUG_OPTS ` | |
| $env:MAVEN_DEBUG_OPTS ` |
| if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) { &$env:USERPROFILE"\mavenrc.ps1" $args } | ||
| } | ||
|
|
||
| if (-not (Test-path $env:JAVA_HOME)) { |
There was a problem hiding this comment.
Test-Path on potentially null $env:JAVA_HOME. When JAVA_HOME is not set, $env:JAVA_HOME is $null. On PowerShell 5.x, Test-Path -Path $null throws ParameterBindingValidationException instead of returning $false, bypassing the Get-Command java fallback. Guard with an explicit null/empty check:
| if (-not (Test-path $env:JAVA_HOME)) { | |
| if (-not $env:JAVA_HOME -or -not (Test-Path $env:JAVA_HOME)) { |
| } | ||
| } | ||
| else { | ||
| $JAVACMD = $env:JAVA_HOME + "\bin\java.exe" |
There was a problem hiding this comment.
Join-Path instead of string concatenation. PR #878 already uses Join-Path $env:JAVA_HOME bin java.exe, which is idiomatic PowerShell.
| $JAVACMD = $env:JAVA_HOME + "\bin\java.exe" | |
| $JAVACMD = Join-Path $env:JAVA_HOME bin java.exe |
| "-Dmaven.multiModuleProjectDirectory=$basedir" ` | ||
| $LAUNCHER_CLASS ` | ||
| $MAVEN_ARGS ` | ||
| $args |
There was a problem hiding this comment.
$LASTEXITCODE after the & $JAVACMD invocation, but it is not propagated to the caller. Any script or CI pipeline invoking mvn.ps1 will receive exit code 0 even when Maven fails.
| $args | |
| $args | |
| exit $LASTEXITCODE |
| $Host.UI.RawUI.WindowTitle = $MyInvocation.MyCommand | ||
|
|
||
| if (-not $env:MAVEN_DEBUG_ADDRESS ) { | ||
| $env:MAVEN_DEBUG_ADDRESS = "8000" |
There was a problem hiding this comment.
MAVEN_DEBUG_ADDRESS = "8000" (bare port, no hostname) causes the JVM debug agent to listen on all network interfaces on Java 9+, exposing the debug port on non-loopback interfaces. The header comment says the default is localhost:8000 — the code disagrees. PR #878 correctly uses "localhost:8000".
| $env:MAVEN_DEBUG_ADDRESS = "8000" | |
| $env:MAVEN_DEBUG_ADDRESS = "localhost:8000" |
|
|
||
| $env:MAVEN_DEBUG_OPTS = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=$env:MAVEN_DEBUG_ADDRESS" | ||
|
|
||
| $mvnCmd = $PSScriptRoot + "\mvn.ps1" |
There was a problem hiding this comment.
💡 Use Join-Path for path construction. Consistent with the fix needed in mvn.ps1 and already done in PR #878.
| $mvnCmd = $PSScriptRoot + "\mvn.ps1" | |
| $mvnCmd = Join-Path -Path $PSScriptRoot -ChildPath "mvn.ps1" |
gnodet-bot
left a comment
There was a problem hiding this comment.
Additional Findings — [MNG-7541] PowerShell mvn command
The previous review identified 4 critical bugs (syntax error, Test-Path null, $MAVEN_ARGS scope, $MAVEN_DEBUG_OPTS scope) that remain unaddressed. Below are additional issues found by comparing mvn.ps1 against the existing mvn.cmd.
This review was generated by an AI agent, Hermès on behalf of @gnodet.
|
|
||
| & $JAVACMD ` | ||
| $MAVEN_OPTS ` | ||
| $MAVEN_DEBUG_OPTS ` | ||
| -classpath $LAUNCHER_JAR ` | ||
| "-Dclassworlds.conf=$CLASSWORLDS_CONF" ` | ||
| "-Dmaven.home=$MAVEN_HOME" ` | ||
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` |
There was a problem hiding this comment.
🔴 Critical: JVM options passed as a single string argument instead of individual args.
$MAVEN_OPTS is a string like "-Xmx512m -Dfoo=bar". When PowerShell invokes & $JAVACMD $MAVEN_OPTS, the entire string is passed as one argument to java.exe, not split into individual flags. Java will fail with Unrecognized option.
Same problem applies to $MAVEN_DEBUG_OPTS.
Fix: split the string into an array before passing it.
| & $JAVACMD ` | |
| $MAVEN_OPTS ` | |
| $MAVEN_DEBUG_OPTS ` | |
| -classpath $LAUNCHER_JAR ` | |
| "-Dclassworlds.conf=$CLASSWORLDS_CONF" ` | |
| "-Dmaven.home=$MAVEN_HOME" ` | |
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` | |
| $MAVEN_OPTS_ARRAY = if ($MAVEN_OPTS) { $MAVEN_OPTS.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries) } else { @() } | |
| $MAVEN_DEBUG_ARRAY = if ($env:MAVEN_DEBUG_OPTS) { $env:MAVEN_DEBUG_OPTS.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries) } else { @() } | |
| & $JAVACMD ` | |
| @MAVEN_OPTS_ARRAY ` | |
| @MAVEN_DEBUG_ARRAY ` | |
| -classpath $LAUNCHER_JAR ` | |
| "-Dclassworlds.conf=$CLASSWORLDS_CONF" ` | |
| "-Dmaven.home=$MAVEN_HOME" ` | |
| "-Dlibrary.jansi.path=$MAVEN_HOME\lib\jansi-native" ` | |
| "-Dmaven.multiModuleProjectDirectory=$basedir" ` | |
| $LAUNCHER_CLASS ` | |
| $env:MAVEN_ARGS ` | |
| $args |
(Note: this also fixes the $MAVEN_ARGS → $env:MAVEN_ARGS and $MAVEN_DEBUG_OPTS → $env:MAVEN_DEBUG_OPTS scope issues from the previous review.)
| # Find the project basedir, i.e., the directory that contains the directory ".mvn". | ||
| # Fallback to current working directory if not found. | ||
|
|
||
| $WDIR = Get-Location |
There was a problem hiding this comment.
MAVEN_BASEDIR support. mvn.cmd checks %MAVEN_BASEDIR% to allow users/CI to override project basedir detection entirely (skips the .mvn directory walk). This script ignores $env:MAVEN_BASEDIR.
Add before the basedir detection block:
if ($env:MAVEN_BASEDIR) {
$basedir = $env:MAVEN_BASEDIR
} else {
# ... existing detection logic
}| # and then search for the maven project base dir. | ||
| $pom_file_reference = $args[$i + 1] | ||
|
|
||
| if (Test-Path $pom_file_reference) { |
There was a problem hiding this comment.
-f <directory> is mishandled. mvn.cmd accepts both files and directories as -f arguments. Here, (Get-Item $pom_file_reference).DirectoryName only works for files — for a directory, .DirectoryName returns the parent directory, so the basedir will be wrong.
| # and then search for the maven project base dir. | |
| $pom_file_reference = $args[$i + 1] | |
| if (Test-Path $pom_file_reference) { | |
| if (Test-Path $pom_file_reference) { | |
| $item = Get-Item $pom_file_reference | |
| if ($item.PSIsContainer) { | |
| $basedir = $item.FullName | |
| } else { | |
| $basedir = $item.DirectoryName | |
| } |
|
|
||
| $jvm_location = $path + "\.mvn\jvm.config" | ||
|
|
||
| if (Test-Path $jvm_location) { | ||
| return $env:MAVEN_OPTS + " " + -Join (Get-Content $jvm_location) | ||
| } | ||
| return $env:MAVEN_OPTS | ||
| } |
There was a problem hiding this comment.
${MAVEN_PROJECTBASEDIR} substitution in jvm.config. mvn.cmd replaces ${MAVEN_PROJECTBASEDIR} and $MAVEN_PROJECTBASEDIR placeholders in each line of .mvn/jvm.config. This function reads the file as-is, so projects relying on this substitution will get literal placeholder strings passed to the JVM.
| $jvm_location = $path + "\.mvn\jvm.config" | |
| if (Test-Path $jvm_location) { | |
| return $env:MAVEN_OPTS + " " + -Join (Get-Content $jvm_location) | |
| } | |
| return $env:MAVEN_OPTS | |
| } | |
| function RetrieveContentsJvmConfig { | |
| param ( | |
| [String] $path | |
| ) | |
| $jvm_location = Join-Path $path ".mvn\jvm.config" | |
| if (Test-Path $jvm_location) { | |
| $lines = Get-Content $jvm_location | ForEach-Object { | |
| $_ -replace '\$\{MAVEN_PROJECTBASEDIR\}', $path -replace '\$MAVEN_PROJECTBASEDIR', $path | |
| } | |
| return ($env:MAVEN_OPTS + " " + ($lines -join " ")).Trim() | |
| } | |
| return $env:MAVEN_OPTS | |
| } |
| $MAVEN_ARGS ` | ||
| $args | ||
|
|
||
|
|
There was a problem hiding this comment.
--enable-native-access=ALL-UNNAMED probe (MNG-8248). mvn.cmd probes whether the JVM supports --enable-native-access and adds it to avoid illegal-access warnings on Java 17+. This script has no equivalent.
Add before the java invocation:
$INTERNAL_OPTS = @()
try {
& $JAVACMD --enable-native-access=ALL-UNNAMED -version 2>&1 | Out-Null
$INTERNAL_OPTS += '--enable-native-access=ALL-UNNAMED'
} catch { }Then include @INTERNAL_OPTS in the java invocation.
| if (Test-Path -Path $env:PROGRAMDATA\mavenrc.ps1 -PathType Leaf) { &$env:PROGRAMDATA"\mavenrc.ps1" $args } | ||
| if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) { &$env:USERPROFILE"\mavenrc.ps1" $args } |
There was a problem hiding this comment.
💡 mavenrc.ps1 invocation syntax. &$env:PROGRAMDATA"\mavenrc.ps1" concatenates the call operator & with the variable, which doesn't work as intended. Should be:
| if (Test-Path -Path $env:PROGRAMDATA\mavenrc.ps1 -PathType Leaf) { &$env:PROGRAMDATA"\mavenrc.ps1" $args } | |
| if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) { &$env:USERPROFILE"\mavenrc.ps1" $args } | |
| if (Test-Path -Path "$env:PROGRAMDATA\mavenrc.ps1" -PathType Leaf) { & "$env:PROGRAMDATA\mavenrc.ps1" $args } | |
| if (Test-Path -Path "$env:USERPROFILE\mavenrc.ps1" -PathType Leaf) { & "$env:USERPROFILE\mavenrc.ps1" $args } |
Following this checklist to help us incorporate your
contribution quickly and easily:
for the change (usually before you start working on it). Trivial changes like typos do not
require a JIRA issue. Your pull request should address just this issue, without
pulling in other changes.
[MNG-XXX] SUMMARY,where you replace
MNG-XXXandSUMMARYwith the appropriate JIRA issue.[MNG-XXX] SUMMARY.Best practice is to use the JIRA issue title in both the pull request title and in the first line of the commit message.
mvn clean verifyto make sure basic checks pass. A more thorough check willbe performed on your pull request automatically.
If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.
I hereby declare this contribution to be licenced under the Apache License Version 2.0, January 2004
In any other case, please file an Apache Individual Contributor License Agreement.