[
https://issues.apache.org/jira/browse/MNG-7541?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17764872#comment-17764872
]
ASF GitHub Bot commented on MNG-7541:
-------------------------------------
Giovds commented on code in PR #982:
URL: https://github.com/apache/maven/pull/982#discussion_r1324860640
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$Host.UI.RawUI.WindowTitle = (Get-Variable MyInvocation -Scope
Script).Value.InvocationName
Review 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.
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
Review 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
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
Review Comment:
Should, at least for `3.9.x`, it still support the `mavenrc_pre.ps1` and
`mavenrc_post.ps1`
[convention](https://maven.apache.org/ref/3.9.4/apache-maven/), as they are
also still supported (with deprecation warning) in `4.x`. @michael-o
@slachiewicz ?
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
+
+# check if maven command exists
+if (-not (Test-path "$MAVEN_HOME\bin\mvn.ps1")) {
+ Write-Error -Message "maven command (\bin\mvn.ps1) cannot be found"
-ErrorAction Stop
+}
+# ==== END VALIDATION ====
+
+$CLASSWORLDS_CONF = "$MAVEN_HOME\bin\m2.conf"
+
+# Find the project basedir, i.e., the directory that contains the directory
".mvn".
+# Fallback to current working directory if not found.
+
+$WDIR = Get-Location
+
+# Look for the --file switch and start the search for the .mvn directory from
the specified
+# POM location, if supplied.
+
+$i = 0
+$file_flag_found = $false
+foreach ($arg in $args) {
+ if (($arg -ceq "-f") -or ($arg -ceq "--file")) {
+ $file_flag_found = $true
+ break
+ }
+ $i += 1
+}
+
+function IsNotRoot {
+ param (
+ [String] $path
+ )
+
+ return -not $path.endsWith(":\")
Review Comment:
I'm not sure if it can/will be run on a share. _If_ that happens I think
`:\` is not sufficient, as a share starts with `\\share\some\path`.
I believe `$path` is always a folder in this script, so we could use
`(Get-Item $path).Root` instead.
_I've never tried how this behaves on a share though... I would expect it to
return `\\share\` as it normally returns the drive e.g. `C:\`._
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
Review Comment:
Small detail, but kind of required. Either `JAVA_HOME` or a `java` command
has to be present otherwise it throws an error.
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
+
+# check if maven command exists
+if (-not (Test-path "$MAVEN_HOME\bin\mvn.ps1")) {
+ Write-Error -Message "maven command (\bin\mvn.ps1) cannot be found"
-ErrorAction Stop
+}
+# ==== END VALIDATION ====
+
+$CLASSWORLDS_CONF = "$MAVEN_HOME\bin\m2.conf"
+
+# Find the project basedir, i.e., the directory that contains the directory
".mvn".
+# Fallback to current working directory if not found.
+
+$WDIR = Get-Location
+
+# Look for the --file switch and start the search for the .mvn directory from
the specified
+# POM location, if supplied.
+
+$i = 0
+$file_flag_found = $false
+foreach ($arg in $args) {
Review 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.*
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
+
+# check if maven command exists
+if (-not (Test-path "$MAVEN_HOME\bin\mvn.ps1")) {
+ Write-Error -Message "maven command (\bin\mvn.ps1) cannot be found"
-ErrorAction Stop
+}
+# ==== END VALIDATION ====
+
+$CLASSWORLDS_CONF = "$MAVEN_HOME\bin\m2.conf"
+
+# Find the project basedir, i.e., the directory that contains the directory
".mvn".
+# Fallback to current working directory if not found.
+
+$WDIR = Get-Location
+
+# Look for the --file switch and start the search for the .mvn directory from
the specified
+# POM location, if supplied.
+
+$i = 0
+$file_flag_found = $false
+foreach ($arg in $args) {
+ if (($arg -ceq "-f") -or ($arg -ceq "--file")) {
+ $file_flag_found = $true
+ break
+ }
+ $i += 1
+}
+
+function IsNotRoot {
+ param (
+ [String] $path
+ )
+
+ return -not $path.endsWith(":\")
+}
+
+function RetrieveContentsJvmConfig {
+ param (
+ [String] $path
+ )
+
+ $jvm_location = $path + "\.mvn\jvm.config"
+
+ if (Test-Path $jvm_location) {
+ return $env:MAVEN_OPTS + (Get-Content $jvm_location).Replace("`n",
"").Replace("`r", "");
Review Comment:
Is this to replace windows line endings (`\r\n`)? I think this won't work as
it tries to replace `'n` as literal 2 character combination, never tried it
though.
I would suggest the following:
`Get-Content` is an array of objects. You can simply `-Join (Get-Content
$filePath)` and it will be put into 1 string as arguments.
It might also need to have an additional space in front?
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
Review Comment:
I personally prefer to use `Throw` over `Write-Error -ErrorAction Stop` as
it will result in a more clouded `$Error` object.

##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
+
+# check if maven command exists
+if (-not (Test-path "$MAVEN_HOME\bin\mvn.ps1")) {
+ Write-Error -Message "maven command (\bin\mvn.ps1) cannot be found"
-ErrorAction Stop
+}
+# ==== END VALIDATION ====
+
+$CLASSWORLDS_CONF = "$MAVEN_HOME\bin\m2.conf"
+
+# Find the project basedir, i.e., the directory that contains the directory
".mvn".
+# Fallback to current working directory if not found.
+
+$WDIR = Get-Location
+
+# Look for the --file switch and start the search for the .mvn directory from
the specified
+# POM location, if supplied.
+
+$i = 0
+$file_flag_found = $false
+foreach ($arg in $args) {
+ if (($arg -ceq "-f") -or ($arg -ceq "--file")) {
+ $file_flag_found = $true
+ break
+ }
+ $i += 1
+}
+
+function IsNotRoot {
+ param (
+ [String] $path
+ )
+
+ return -not $path.endsWith(":\")
+}
+
+function RetrieveContentsJvmConfig {
+ param (
+ [String] $path
+ )
+
+ $jvm_location = $path + "\.mvn\jvm.config"
+
+ if (Test-Path $jvm_location) {
+ return $env:MAVEN_OPTS + (Get-Content $jvm_location).Replace("`n",
"").Replace("`r", "");
+ }
+ return $env:MAVEN_OPTS;
+}
+
+$basedir = ""
+
+if ($file_flag_found) {
+ # we need to assess if the file exists
+ # and then search for the maven project base dir.
+ $pom_file_reference = $args[$i + 1]
+
+ if (Test-Path $pom_file_reference) {
+ $basedir = (Get-Item $pom_file_reference).DirectoryName
+ }
+ else {
+ $pom_file_error = "POM file " + $pom_file_reference + " specified the
-f/--file command-line argument does not exist"
+ Write-Error -Message $pom_file_error -ErrorAction Stop
+ }
+}
+else {
+ # if file flag is not found, then the pom.xml is relative to the working dir
+ # and the jvm.config can be found in the maven project base dir.
+
+ $basedir = $WDIR
+
+ while (IsNotRoot($WDIR.Path)) {
Review Comment:
If we do not find `.mvn` directory it will use the root directory as
`$basedir` and as `-Dmaven.multiModuleProjectDirectory`?
I'm not entirely sure what the
[`.cmd`](https://github.com/apache/maven/blob/0c8b7dfc605ce906f6c73db0aeab1735759da890/apache-maven/src/bin/mvn.cmd#L153)
does though, I'm not as familiar with `.cmd`.
##########
apache-maven/src/bin/mvnDebug.ps1:
##########
@@ -0,0 +1,42 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Debug Script
+
+Environment Variable Prerequisites
+
+JAVA_HOME (Optional) Points to a Java installation.
+MAVEN_OPTS (Optional) Java runtime options used when Maven is
executed.
+MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+MAVEN_DEBUG_ADDRESS (Optional) Set the debug address. Default value is
localhost:8000
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$Host.UI.RawUI.WindowTitle = $MyInvocation.MyCommand
+
+if (-not $env:MAVEN_DEBUG_ADDRESS ) {
+ $env:MAVEN_DEBUG_ADDRESS = "localhost:8000"
+}
+
+$env:MAVEN_DEBUG_OPTS = "-Xdebug
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=$env:MAVEN_DEBUG_ADDRESS"
Review Comment:
I'm not as familiar with `.cmd` files, but I believe the
[current](https://github.com/apache/maven/blob/maven-3.9.x/apache-maven/src/bin/mvnDebug.cmd)
`.cmd` just makes it port `8000` (no `localhost`).
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
+
+# check if maven command exists
+if (-not (Test-path "$MAVEN_HOME\bin\mvn.ps1")) {
+ Write-Error -Message "maven command (\bin\mvn.ps1) cannot be found"
-ErrorAction Stop
+}
+# ==== END VALIDATION ====
+
+$CLASSWORLDS_CONF = "$MAVEN_HOME\bin\m2.conf"
+
+# Find the project basedir, i.e., the directory that contains the directory
".mvn".
+# Fallback to current working directory if not found.
+
+$WDIR = Get-Location
+
+# Look for the --file switch and start the search for the .mvn directory from
the specified
+# POM location, if supplied.
+
+$i = 0
+$file_flag_found = $false
+foreach ($arg in $args) {
+ if (($arg -ceq "-f") -or ($arg -ceq "--file")) {
+ $file_flag_found = $true
+ break
+ }
+ $i += 1
+}
+
+function IsNotRoot {
+ param (
+ [String] $path
+ )
+
+ return -not $path.endsWith(":\")
+}
+
+function RetrieveContentsJvmConfig {
+ param (
+ [String] $path
+ )
+
+ $jvm_location = $path + "\.mvn\jvm.config"
+
+ if (Test-Path $jvm_location) {
+ return $env:MAVEN_OPTS + (Get-Content $jvm_location).Replace("`n",
"").Replace("`r", "");
+ }
+ return $env:MAVEN_OPTS;
+}
+
+$basedir = ""
+
+if ($file_flag_found) {
+ # we need to assess if the file exists
+ # and then search for the maven project base dir.
+ $pom_file_reference = $args[$i + 1]
+
+ if (Test-Path $pom_file_reference) {
+ $basedir = (Get-Item $pom_file_reference).DirectoryName
+ }
+ else {
+ $pom_file_error = "POM file " + $pom_file_reference + " specified the
-f/--file command-line argument does not exist"
+ Write-Error -Message $pom_file_error -ErrorAction Stop
+ }
+}
+else {
+ # if file flag is not found, then the pom.xml is relative to the working dir
+ # and the jvm.config can be found in the maven project base dir.
+
+ $basedir = $WDIR
+
+ while (IsNotRoot($WDIR.Path)) {
+ if (Test-Path "$WDIR\.mvn") {
+ $basedir = $WDIR
+ break
+ }
+
+ if ($WDIR) {
+ $WDIR = Split-Path $WDIR
+ }
+ else {
+ break
+ }
+ }
+}
+
+$MAVEN_OPTS = (RetrieveContentsJvmConfig $basedir)
+
+$LAUNCHER_JAR = Get-Item $MAVEN_HOME"\boot\plexus-classworlds*.jar"
Review Comment:
If there are multiple jars (as the `*` suggests), I think this will fail as
the separator would be a space.
(Note the
[`.cmd`](https://github.com/apache/maven/blob/0c8b7dfc605ce906f6c73db0aeab1735759da890/apache-maven/src/bin/mvn.cmd#L169C1-L170C1)
has an extra `-` and does not filter on `.jar`)
##########
apache-maven/src/bin/mvn.ps1:
##########
@@ -0,0 +1,158 @@
+<#
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+
+-----------------------------------------------------------------------------
+Apache Maven Startup Script
+
+Environment Variable Prerequisites
+
+ JAVA_HOME (Optional) Points to a Java installation.
+ MAVEN_ARGS (Optional) Arguments passed to Maven before CLI arguments.
+ MAVEN_OPTS (Optional) Java runtime options used when Maven is executed.
+ MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files.
+-----------------------------------------------------------------------------
+#>
+
+# set title
+$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 }
+ if (Test-Path -Path $env:USERPROFILE\mavenrc.ps1 -PathType Leaf) {
&$env:USERPROFILE"\mavenrc.ps1" $args }
+}
+
+if (-not (Test-path $env:JAVA_HOME)) {
+ $JAVACMD = (get-command java).Source
+ if (-not $JAVACMD) {
+ 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."
+ }
+}
+else {
+ $JAVACMD = $env:JAVA_HOME + "\bin\java.exe"
+}
+
+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
+}
+
+# check mvn home
+$MAVEN_HOME = (Get-Item $PSScriptRoot"\..")
Review 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).
> Native support for PowerShell to start Maven
> --------------------------------------------
>
> Key: MNG-7541
> URL: https://issues.apache.org/jira/browse/MNG-7541
> Project: Maven
> Issue Type: Improvement
> Components: Command Line
> Affects Versions: 3.8.3
> Environment: windows 10 / 11
> Reporter: Jurrian Fahner
> Priority: Trivial
> Labels: Script, Windows10, Windows11
>
> Maven has two files in the bin dir:
> ||command||its use||
> |mvn|POSIX shell|
> |mvn.cmd|cmd.exe|
> On windows there are two ways to write scripts, by using cmd.exe or using
> powershell.
> If you enter mvn in powershell it will look for `mvn.ps1` on the PATH first.
> If it doesn't find anything it will execute `mvn.cmd` as fall-back.
> When running maven for starting a server for development purposes and you do
> ctrl-c to exit the server it will ask the question: Terminate batch job (Y/N)?
> As far as I know it is default behaviour of cmd.exe.
> Well if I don't want to terminate, I wouldn't press ctrl-c. ;)
> It is not the case (as far as I know that Microsoft is going to deprecate
> cmd.exe in favor of powershell:
> [https://devblogs.microsoft.com/commandline/rumors-of-cmds-death-have-been-greatly-exaggerated/]
> Allthough I think it would be a good move for maven to have also a powershell
> script as well... It is possible to integrate elegant support for native help
> in powershell, `get-help mvn`.
> But it also increases the maintenance effort as well. I don't know whether
> this cost outweigh the benefits, though...
> By the way I would happy to contribute if it is appreciated.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)