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

237 lines
8.4 KiB
PowerShell

# PowerShell script for building and deploying arm_am-pom to JBoss EAP 8.0
# 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
# Configuration from config.json
$JBOSS_HOME = $config.jboss.jbossHome
$DEPLOYMENT_DIR = $config.jboss.deploymentDir
$PROJECT_DIR = $config.ear.projectDir
$MAVEN_PROFILE = $config.ear.mavenProfile
$JAVA_HOME = $config.jboss.javaHome
$ASSET_GUI_DIR = $config.assetGui.projectDir
$ASSET_GUI_TARGET = $config.assetGui.targetDir
# Store the original directory
$originalDirectory = Get-Location
# Function to check if JBoss is running
function Test-JBossRunning {
$jbossProcess = Get-CimInstance Win32_Process -Filter "Name = 'java.exe'" |
Where-Object { $_.CommandLine -like "*jboss.home.dir=$JBOSS_HOME*" }
return $null -ne $jbossProcess
}
Write-Host "Starting build and deploy process..." -ForegroundColor Green
Write-Host "Using configuration:" -ForegroundColor Yellow
Write-Host " Java Home: $JAVA_HOME" -ForegroundColor Yellow
Write-Host " JBoss Home: $JBOSS_HOME" -ForegroundColor Yellow
Write-Host " Project Directory: $PROJECT_DIR" -ForegroundColor Yellow
Write-Host " Maven Profile: $MAVEN_PROFILE" -ForegroundColor Yellow
Write-Host " Asset GUI Directory: $ASSET_GUI_DIR" -ForegroundColor Yellow
# Check if Java exists
if (-not (Test-Path $JAVA_HOME)) {
Write-Host "Error: Java not found at $JAVA_HOME" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Set JAVA_HOME for the build
$env:JAVA_HOME = $JAVA_HOME
# Check if JBoss directory exists
if (-not (Test-Path $JBOSS_HOME)) {
Write-Host "Error: JBoss directory not found at $JBOSS_HOME" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Check if project directories exist
if (-not (Test-Path $PROJECT_DIR)) {
Write-Host "Error: Project directory not found at $PROJECT_DIR" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Check Asset GUI directory and package.json
$skipAssetGui = $false
if (-not (Test-Path $ASSET_GUI_DIR)) {
Write-Host "Warning: Asset GUI directory not found at $ASSET_GUI_DIR" -ForegroundColor Yellow
Write-Host "Skipping Asset GUI build..." -ForegroundColor Yellow
$skipAssetGui = $true
}
elseif (-not (Test-Path (Join-Path $ASSET_GUI_DIR "package.json"))) {
Write-Host "Warning: package.json not found in Asset GUI directory" -ForegroundColor Yellow
Write-Host "Skipping Asset GUI build..." -ForegroundColor Yellow
$skipAssetGui = $true
}
if (-not $skipAssetGui) {
# Build and deploy Asset GUI
Write-Host "Building Asset GUI..." -ForegroundColor Yellow
Set-Location $ASSET_GUI_DIR
# Run npm install and build for Asset GUI
Write-Host "Installing Asset GUI dependencies..." -ForegroundColor Yellow
npm install
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: npm install failed! Skipping Asset GUI build..." -ForegroundColor Yellow
$skipAssetGui = $true
}
if (-not $skipAssetGui) {
Write-Host "Building Asset GUI..." -ForegroundColor Yellow
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: Asset GUI build failed! Continuing with EAR build..." -ForegroundColor Yellow
$skipAssetGui = $true
}
else {
# Create Asset GUI target directory if it doesn't exist
if (-not (Test-Path $ASSET_GUI_TARGET)) {
New-Item -ItemType Directory -Path $ASSET_GUI_TARGET -Force
}
# Copy Asset GUI build to target directory
Write-Host "Copying Asset GUI build to target directory..." -ForegroundColor Yellow
Copy-Item -Path "$ASSET_GUI_DIR\dist\*" -Destination $ASSET_GUI_TARGET -Recurse -Force
}
}
}
# Build EAR project
Write-Host "Building EAR project..." -ForegroundColor Yellow
Set-Location $PROJECT_DIR
# Build parent
Write-Host "Building parent project..." -ForegroundColor Yellow
mvn clean install -N "-P$MAVEN_PROFILE"
if ($LASTEXITCODE -ne 0) {
Write-Host "Maven parent build failed!" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Build EJB module
Write-Host "Building EJB module..." -ForegroundColor Yellow
Set-Location "$PROJECT_DIR\arm_am-ejb"
mvn clean install -DskipTests "-P$MAVEN_PROFILE"
if ($LASTEXITCODE -ne 0) {
Write-Host "Maven EJB module build failed!" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Build WAR module
Write-Host "Building WAR module..." -ForegroundColor Yellow
Set-Location "$PROJECT_DIR\arm_am"
mvn clean install -DskipTests "-P$MAVEN_PROFILE"
if ($LASTEXITCODE -ne 0) {
Write-Host "Maven WAR module build failed!" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Build EAR module
Write-Host "Building EAR module..." -ForegroundColor Yellow
Set-Location "$PROJECT_DIR\arm_am-ear"
mvn clean package -DskipTests "-P$MAVEN_PROFILE"
if ($LASTEXITCODE -ne 0) {
Write-Host "Maven EAR module build failed!" -ForegroundColor Red
Set-Location $originalDirectory
exit 1
}
# Check if build produced the EAR file
$earFile = Get-ChildItem -Path "$PROJECT_DIR\arm_am-ear\target\*.ear" -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $earFile) {
Write-Host "Error: EAR file not found after build" -ForegroundColor Red
Write-Host "Searching in: $PROJECT_DIR\arm_am-ear\target\" -ForegroundColor Yellow
Set-Location $originalDirectory
exit 1
}
# Create deployments directory if it doesn't exist
if (-not (Test-Path $DEPLOYMENT_DIR)) {
New-Item -ItemType Directory -Path $DEPLOYMENT_DIR -Force
}
# Copy EAR file to JBoss deployments directory
Write-Host "Deploying EAR to JBoss..." -ForegroundColor Yellow
Write-Host "Copying from: $($earFile.FullName)" -ForegroundColor Yellow
Write-Host "Copying to: $DEPLOYMENT_DIR" -ForegroundColor Yellow
# Remove existing deployment if present
$deployedEarPath = Join-Path $DEPLOYMENT_DIR $config.ear.deployedFile
if (Test-Path $deployedEarPath) {
Write-Host "Removing existing deployment..." -ForegroundColor Yellow
Remove-Item $deployedEarPath -Force
}
# Copy new EAR file with the configured name
Copy-Item $earFile.FullName $deployedEarPath
# Unpack EAR if configured
if ($config.ear.unpackDir) {
Write-Host "Unpacking EAR to: $($config.ear.unpackDir)" -ForegroundColor Yellow
# Create unpack directory if it doesn't exist
if (-not (Test-Path $config.ear.unpackDir)) {
New-Item -ItemType Directory -Path $config.ear.unpackDir -Force
}
# Clean existing content
Remove-Item "$($config.ear.unpackDir)\*" -Recurse -Force -ErrorAction SilentlyContinue
# Use 7-Zip to unpack the EAR (Java JAR files are ZIP format)
try {
$7zPath = "C:\Program Files\7-Zip\7z.exe"
if (Test-Path $7zPath) {
# Use 7-Zip
Start-Process -FilePath $7zPath -ArgumentList "x", "`"$deployedEarPath`"", "-o`"$($config.ear.unpackDir)`"", "-y" -Wait -NoNewWindow
} else {
# Fallback to jar command from Java
$jarPath = Join-Path $JAVA_HOME "bin\jar.exe"
if (Test-Path $jarPath) {
Set-Location $config.ear.unpackDir
Start-Process -FilePath $jarPath -ArgumentList "xf", "`"$deployedEarPath`"" -Wait -NoNewWindow
} else {
Write-Host "Warning: Could not unpack EAR - neither 7-Zip nor jar command found" -ForegroundColor Yellow
}
}
}
catch {
Write-Host "Warning: Failed to unpack EAR: $_" -ForegroundColor Yellow
}
finally {
Set-Location $originalDirectory
}
}
# Check if JBoss is running
if (Test-JBossRunning) {
Write-Host "JBoss is running - deployment will be automatically picked up" -ForegroundColor Green
} else {
Write-Host "Note: JBoss is not running. Start JBoss to complete deployment" -ForegroundColor Yellow
}
Write-Host "Build and deploy process completed successfully!" -ForegroundColor Green
if (-not $skipAssetGui) {
Write-Host " - Asset GUI deployed to: $ASSET_GUI_TARGET" -ForegroundColor Green
}
Write-Host " - EAR deployed to: $deployedEarPath" -ForegroundColor Green
if ($config.ear.unpackDir) {
Write-Host " - EAR unpacked to: $($config.ear.unpackDir)" -ForegroundColor Green
}
# Restore the original directory
Set-Location $originalDirectory