test-adv-jboss8/unpack_ear.ps1
2025-04-01 11:50:10 +02:00

93 lines
3.5 KiB
PowerShell

# PowerShell script to unpack EAR file
# Read configuration from config.json
$configPath = Join-Path $PSScriptRoot "config.json"
if (-not (Test-Path $configPath)) {
Write-Host "Error: config.json not found at $configPath" -ForegroundColor Red
exit 1
}
$config = Get-Content $configPath -Raw | ConvertFrom-Json
# Get paths from config
$sourceFile = Join-Path $config.jboss.jbossHome "standalone\deployments\adv360-ear.ear"
$destinationPath = $config.ear.unpackDir
Write-Host "Using configuration:" -ForegroundColor Yellow
Write-Host " Source EAR: $sourceFile" -ForegroundColor Yellow
Write-Host " Destination: $destinationPath" -ForegroundColor Yellow
# Verify source file exists
if (-not (Test-Path -Path $sourceFile)) {
Write-Host "Error: Source EAR file not found at $sourceFile" -ForegroundColor Red
exit 1
}
# Remove destination directory if it exists
if (Test-Path -Path $destinationPath) {
Remove-Item -Path $destinationPath -Recurse -Force
Write-Host "Removed existing directory: $destinationPath" -ForegroundColor Yellow
}
# Create destination directory
if (-not (Test-Path -Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath -Force | Out-Null
Write-Host "Created destination directory: $destinationPath" -ForegroundColor Yellow
}
# Try using 7-Zip first, fall back to zip method if not available
$7zPath = "C:\Program Files\7-Zip\7z.exe"
if (Test-Path $7zPath) {
Write-Host "Using 7-Zip to extract EAR..." -ForegroundColor Yellow
Start-Process -FilePath $7zPath -ArgumentList "x", "`"$sourceFile`"", "-o`"$destinationPath`"", "-y" -Wait -NoNewWindow
} else {
Write-Host "7-Zip not found, using PowerShell Expand-Archive..." -ForegroundColor Yellow
# Rename .ear to .zip temporarily to use Expand-Archive
$tempZipPath = $sourceFile -replace '\.ear$', '.zip'
Copy-Item -Path $sourceFile -Destination $tempZipPath
# Extract the EAR contents
Expand-Archive -Path $tempZipPath -DestinationPath $destinationPath -Force
# Clean up temporary zip file
Remove-Item -Path $tempZipPath
}
Write-Host "EAR file extracted to $destinationPath" -ForegroundColor Green
# Function to unpack archive files (war/ejb)
function Expand-JavaArchive {
param (
[string]$archivePath,
[string]$destinationPath
)
$tempZipPath = $archivePath -replace '\.(war|jar)$', '.zip'
Copy-Item -Path $archivePath -Destination $tempZipPath
# Create extraction directory
if (-not (Test-Path -Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath -Force
}
# Try using 7-Zip first, fall back to zip method if not available
if (Test-Path $7zPath) {
Write-Host "Using 7-Zip to extract $archivePath..." -ForegroundColor Yellow
Start-Process -FilePath $7zPath -ArgumentList "x", "`"$tempZipPath`"", "-o`"$destinationPath`"", "-y" -Wait -NoNewWindow
} else {
Write-Host "7-Zip not found, using PowerShell Expand-Archive..." -ForegroundColor Yellow
# Extract contents
Expand-Archive -Path $tempZipPath -DestinationPath $destinationPath -Force
}
# Clean up temporary zip file
Remove-Item -Path $tempZipPath
Write-Host "Extracted $archivePath to $destinationPath" -ForegroundColor Green
}
# Find and extract WAR and EJB files
Get-ChildItem -Path $destinationPath -Recurse -Include "*.war", "*-ejbx.jar" | ForEach-Object {
$extractPath = Join-Path (Split-Path -Parent $_.FullName) ($_.BaseName + "_unpacked")
Expand-JavaArchive -archivePath $_.FullName -destinationPath $extractPath
}