Last active
September 10, 2023 09:22
-
-
Save hunandy14/0f3377e468ac85a589818295e3c3c27e to your computer and use it in GitHub Desktop.
基於官網最新版自動爬蟲安裝最新版本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 檢查鏈接是否有效 | |
function Test-URI { | |
param ( | |
[Parameter(Position=0, Mandatory)] | |
[string] $Uri | |
) | |
try { | |
# 嘗試發送 HEAD 請求 | |
$response = Invoke-WebRequest -Uri $Uri -Method Head -TimeoutSec 10 | |
# 檢查 HTTP 狀態碼是否為 200 | |
if ($response -and $response.StatusCode -eq 200) { | |
return $true | |
} else { | |
# Write-Warning "Received HTTP status code $($response.StatusCode). The link might not be valid." | |
return $false | |
} | |
} catch { | |
# Write-Warning "Error checking the link: $_" | |
return $null | |
} | |
} # Test-URI "https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.zip" | |
# 下載檔案 | |
function Get-RemoteFile { | |
param ( | |
[Parameter(Position=0, Mandatory)] | |
[string] $Uri, | |
[Parameter(Position=1)] | |
[string] $Destination = $env:TEMP, | |
[Parameter()] | |
[switch] $ShowDetail, | |
[switch] $Force | |
) | |
# 檢查鏈接是否有效 | |
if (!(Test-URI $Uri)) { | |
Write-Error "The provided link ($Uri) might not be valid." -ErrorAction Stop | |
} else { | |
# 從下載鏈接獲取原始檔名 | |
$filename = [System.IO.Path]::GetFileName((New-Object System.Uri($Uri)).AbsolutePath) | |
# 組合檔名與目標目錄 | |
$downloadPath = Join-Path $Destination $filename | |
# 提示信息 | |
if ($ShowDetail) { | |
Write-Host "Downloading from: " -NoNewline | |
Write-Host "$Uri" -ForegroundColor Green | |
Write-Host " Saving to: " -NoNewline | |
Write-Host "$downloadPath" -ForegroundColor Green | |
} | |
# 使用 Start-BitsTransfer 下載檔案 | |
if ((Test-Path $downloadPath) -and !$Force) { | |
Write-Host "*The file '$downloadPath' already exists at the destination. Use -Force to overwrite and download again." -ForegroundColor DarkGray | |
} else { | |
Start-BitsTransfer -Source $Uri -Destination $downloadPath -ErrorAction Stop | |
} | |
# 回傳下載的位置 | |
return $downloadPath | |
} | |
} # Get-RemoteFile "https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.zip" | |
# 新增環境變數 | |
function Add-EnvPath { | |
param ( | |
[Parameter(Position=0, Mandatory)] | |
[string] $Path, | |
[switch] $ShowDetail, | |
[ValidateSet("Machine", "User", "Auto")] | |
[string] $EnvironmentVariableTarget = "Machine" | |
) | |
# 初始化 | |
[IO.Directory]::SetCurrentDirectory(((Get-Location -PSProvider FileSystem).ProviderPath)) | |
$Path = [IO.Path]::GetFullPath($Path) | |
$CheckPath = $false | |
# 判斷管理權限 | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
$EnvironmentVariableTarget = "Machine" | |
} else { $EnvironmentVariableTarget = "User" } | |
# 檢查路徑是否有效 | |
if (-not (Test-Path $Path)) { Write-Error "The provided path is not valid." -ErrorAction Stop } | |
# 獲取當前的 PATH | |
$currentPath = [Environment]::GetEnvironmentVariable("PATH", $EnvironmentVariableTarget) | |
# 創建一個正則表達式,用於檢查新路徑是否已經在 PATH 中 | |
$escapedPath = [regex]::Escape($Path) | |
$pathExists = $currentPath -match "(^|;)$escapedPath(;|$)" | |
if (-not $pathExists) { | |
try { # 更新 PATH 環境變數 | |
$updatedPath = ($currentPath.TrimEnd(";") + ";$Path") | |
[Environment]::SetEnvironmentVariable("PATH", $updatedPath, $EnvironmentVariableTarget) | |
$env:Path = [Environment]::GetEnvironmentVariable("PATH", $EnvironmentVariableTarget) | |
$CheckPath = ($env:Path -match "(^|;)$escapedPath(;|$)") | |
} catch { Write-Error $_ -ErrorAction Stop } | |
} else { $CheckPath = $true } | |
# 顯是新增的內容 | |
if ($ShowDetail) { | |
$highlightedPath = $currentPath.Replace($Path, "`e[32m$Path`e[0m") | |
Write-Host "Updated PATH:: " -ForegroundColor Cyan -NoNewline | |
Write-Host $highlightedPath | |
} | |
# 回傳結果 | |
return $CheckPath | |
} # Add-EnvPath "Z:\App\apache-maven-3.9.4\bin" | |
# 安裝 Maven | |
function Install-Maven { | |
param ( | |
[Parameter(Position=0, Mandatory)] | |
[string] $Path | |
) | |
# 請求 Maven 下載頁面 | |
$response = Invoke-WebRequest -Uri "https://maven.apache.org/download.cgi" | |
# 使用正則表達式查找下載鏈接 | |
$downloadLink = ($response.Links -match "apache-maven-\d+\.\d+\.\d+-bin\.zip")[0].href | |
# 下載檔案 | |
$downloadPath = Get-RemoteFile $downloadLink | |
# 解壓縮檔案 | |
Expand-Archive -Path $downloadPath -DestinationPath $Path -Force | |
# 取得解壓縮後的 Maven 目錄名稱 | |
$mavenDir = Join-Path $Path ($downloadPath -replace ".+\\apache-maven-(\d+\.\d+\.\d+)-bin\.zip$", 'apache-maven-$1') | |
$mavenBin = Join-Path $mavenDir "bin" | |
# 設置環境變數 | |
Add-EnvPath $mavenBin -EnvironmentVariableTarget Auto |Out-Null | |
# 測試安裝 | |
$msg = $(mvn -v 2>&1) -join '' | |
if ($msg -match 'JAVA_HOME') { | |
Write-Warning $msg | |
Write-Host " To resolve this issue, please refer to the following link for installation instructions:" | |
Write-Host " https://www.oracle.com/java/technologies/downloads/#jdk20-windows" -ForegroundColor DarkGreen | |
} | |
} # Install-Maven "C:\App" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment