mvnw.cmd 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <# : batch portion
  2. @REM ----------------------------------------------------------------------------
  3. @REM Licensed to the Apache Software Foundation (ASF) under one
  4. @REM or more contributor license agreements. See the NOTICE file
  5. @REM distributed with this work for additional information
  6. @REM regarding copyright ownership. The ASF licenses this file
  7. @REM to you under the Apache License, Version 2.0 (the
  8. @REM "License"); you may not use this file except in compliance
  9. @REM with the License. You may obtain a copy of the License at
  10. @REM
  11. @REM https://www.apache.org/licenses/LICENSE-2.0
  12. @REM
  13. @REM Unless required by applicable law or agreed to in writing,
  14. @REM software distributed under the License is distributed on an
  15. @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. @REM KIND, either express or implied. See the License for the
  17. @REM specific language governing permissions and limitations
  18. @REM under the License.
  19. @REM ----------------------------------------------------------------------------
  20. @REM ----------------------------------------------------------------------------
  21. @REM Apache Maven Wrapper startup batch script, version 3.3.2
  22. @REM
  23. @REM Optional ENV vars
  24. @REM MVNW_REPOURL - repo url base for downloading maven distribution
  25. @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
  26. @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
  27. @REM ----------------------------------------------------------------------------
  28. @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
  29. @SET __MVNW_CMD__=
  30. @SET __MVNW_ERROR__=
  31. @SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
  32. @SET PSModulePath=
  33. @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
  34. IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
  35. )
  36. @SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
  37. @SET __MVNW_PSMODULEP_SAVE=
  38. @SET __MVNW_ARG0_NAME__=
  39. @SET MVNW_USERNAME=
  40. @SET MVNW_PASSWORD=
  41. @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*)
  42. @echo Cannot start maven from wrapper >&2 && exit /b 1
  43. @GOTO :EOF
  44. : end batch / begin powershell #>
  45. $ErrorActionPreference = "Stop"
  46. if ($env:MVNW_VERBOSE -eq "true") {
  47. $VerbosePreference = "Continue"
  48. }
  49. # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
  50. $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
  51. if (!$distributionUrl) {
  52. Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
  53. }
  54. switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
  55. "maven-mvnd-*" {
  56. $USE_MVND = $true
  57. $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
  58. $MVN_CMD = "mvnd.cmd"
  59. break
  60. }
  61. default {
  62. $USE_MVND = $false
  63. $MVN_CMD = $script -replace '^mvnw','mvn'
  64. break
  65. }
  66. }
  67. # apply MVNW_REPOURL and calculate MAVEN_HOME
  68. # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
  69. if ($env:MVNW_REPOURL) {
  70. $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" }
  71. $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')"
  72. }
  73. $distributionUrlName = $distributionUrl -replace '^.*/',''
  74. $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
  75. $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain"
  76. if ($env:MAVEN_USER_HOME) {
  77. $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain"
  78. }
  79. $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
  80. $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
  81. if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
  82. Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
  83. Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
  84. exit $?
  85. }
  86. if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
  87. Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
  88. }
  89. # prepare tmp dir
  90. $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
  91. $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
  92. $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
  93. trap {
  94. if ($TMP_DOWNLOAD_DIR.Exists) {
  95. try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
  96. catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
  97. }
  98. }
  99. New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
  100. # Download and Install Apache Maven
  101. Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
  102. Write-Verbose "Downloading from: $distributionUrl"
  103. Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
  104. $webclient = New-Object System.Net.WebClient
  105. if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
  106. $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
  107. }
  108. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  109. $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
  110. # If specified, validate the SHA-256 sum of the Maven distribution zip file
  111. $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
  112. if ($distributionSha256Sum) {
  113. if ($USE_MVND) {
  114. Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
  115. }
  116. Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
  117. if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
  118. Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
  119. }
  120. }
  121. # unzip and move
  122. Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
  123. Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null
  124. try {
  125. Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
  126. } catch {
  127. if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
  128. Write-Error "fail to move MAVEN_HOME"
  129. }
  130. } finally {
  131. try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
  132. catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
  133. }
  134. Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"