# unpack_and_offline_build_airgap.ps1 # ============================================================================ # Execution Environment: OFFLINE (Air-gapped, No Internet) # Purpose: Extract bundle and run offline build # ============================================================================ # Windows PowerShell Only # Version: 3.1 # # IMPORTANT: This script automatically: # 1. Extracts the archive # 2. Sets GRADLE_USER_HOME to project local cache # 3. Configures settings.gradle for offline resolution # 4. Runs build with --offline flag # ============================================================================ $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" # ============================================================================ # Configuration # ============================================================================ $WRAPPER_SEED_PATH = "wrapper_jar_seed" $OFFLINE_HOME_NAME = "_offline_gradle_home" Write-Host "" Write-Host "============================================================" -ForegroundColor Cyan Write-Host " Gradle Offline Build Runner" -ForegroundColor Cyan Write-Host " Environment: AIR-GAPPED (No Internet)" -ForegroundColor Cyan Write-Host " Mode: Fully Offline (--offline enforced)" -ForegroundColor Cyan Write-Host "============================================================" -ForegroundColor Cyan Write-Host "" # ============================================================================ # [1/16] Check Current Directory # ============================================================================ Write-Host "==[1/16] Check Current Directory ==" -ForegroundColor Yellow $Start = (Get-Location).Path Write-Host ("PWD: " + $Start) Write-Host "" # ============================================================================ # [2/16] Select Archive # ============================================================================ Write-Host "==[2/16] Select Archive ==" -ForegroundColor Yellow $Archive = $null if ($args.Count -ge 1) { $Archive = $args[0].Trim().Trim('"').Trim("'") } if ([string]::IsNullOrWhiteSpace($Archive)) { $candidates = Get-ChildItem -Path $Start -File -ErrorAction SilentlyContinue | Where-Object { $_.Name -match "\.(tar\.gz|tgz)$" } | Sort-Object LastWriteTime -Descending if (-not $candidates -or $candidates.Count -eq 0) { Write-Host "[ERROR] No archive found" -ForegroundColor Red Get-ChildItem -Path $Start -File | Format-Table Name, Length -AutoSize throw "ERROR: No .tar.gz file found" } $Archive = $candidates[0].FullName Write-Host ("[AUTO] " + (Split-Path $Archive -Leaf)) -ForegroundColor Cyan } else { if (-not [System.IO.Path]::IsPathRooted($Archive)) { $Archive = Join-Path $Start $Archive } Write-Host ("[USER] " + (Split-Path $Archive -Leaf)) -ForegroundColor Cyan } if (-not (Test-Path -LiteralPath $Archive)) { throw "ERROR: Archive not found: $Archive" } $archiveSizeMB = [math]::Round((Get-Item -LiteralPath $Archive).Length / 1MB, 2) Write-Host ("Size: " + $archiveSizeMB + " MB") Write-Host "" # ============================================================================ # [3/16] Check tar.exe # ============================================================================ Write-Host "==[3/16] Check tar.exe ==" -ForegroundColor Yellow $tar = Get-Command tar.exe -ErrorAction SilentlyContinue if (-not $tar) { throw "ERROR: tar.exe not found" } Write-Host "[OK] tar.exe found" -ForegroundColor Green Write-Host "" # ============================================================================ # [4/16] Extract Archive # ============================================================================ Write-Host "==[4/16] Extract Archive ==" -ForegroundColor Yellow Write-Host "[INFO] Extracting..." -ForegroundColor Gray & tar.exe -xzf $Archive -C $Start if ($LASTEXITCODE -ne 0) { throw "ERROR: Extraction failed" } Write-Host "[OK] Extracted" -ForegroundColor Green Write-Host "" # ============================================================================ # [5/16] Unblock Files # ============================================================================ Write-Host "==[5/16] Unblock Files ==" -ForegroundColor Yellow try { Get-ChildItem -Path $Start -Recurse -Force -ErrorAction SilentlyContinue | Unblock-File -ErrorAction SilentlyContinue Write-Host "[OK] Files unblocked" -ForegroundColor Green } catch { Write-Host "[WARN] Unblock failed" -ForegroundColor DarkYellow } Write-Host "" # ============================================================================ # [6/16] Find Project Root # ============================================================================ Write-Host "==[6/16] Find Project Root ==" -ForegroundColor Yellow $gradlewList = Get-ChildItem -Path $Start -Recurse -Filter "gradlew.bat" -File -ErrorAction SilentlyContinue if (-not $gradlewList) { throw "ERROR: gradlew.bat not found" } $gradlew = $gradlewList | Sort-Object { $_.FullName.Length } | Select-Object -First 1 $ProjectDir = $gradlew.Directory.FullName Write-Host ("Project: " + $ProjectDir) -ForegroundColor Cyan Set-Location -LiteralPath $ProjectDir Write-Host "" # ============================================================================ # [7/16] Fix Permissions # ============================================================================ Write-Host "==[7/16] Fix Permissions ==" -ForegroundColor Yellow try { $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name cmd /c "icacls `"$ProjectDir`" /grant `"$currentUser`:(OI)(CI)F`" /t /q" 2>&1 | Out-Null Write-Host "[OK] Permissions fixed" -ForegroundColor Green } catch { Write-Host "[WARN] icacls failed" -ForegroundColor DarkYellow } Write-Host "" # ============================================================================ # [8/16] Verify Wrapper # ============================================================================ Write-Host "==[8/16] Verify Wrapper ==" -ForegroundColor Yellow $WrapperDir = Join-Path $ProjectDir "gradle\wrapper" $WrapperJar = Join-Path $WrapperDir "gradle-wrapper.jar" $WrapperProp = Join-Path $WrapperDir "gradle-wrapper.properties" if (!(Test-Path -LiteralPath $WrapperProp)) { throw "ERROR: gradle-wrapper.properties missing" } if (!(Test-Path -LiteralPath $WrapperJar)) { $SeedJar = Join-Path $ProjectDir "$WRAPPER_SEED_PATH\gradle-wrapper.jar" if (Test-Path -LiteralPath $SeedJar) { New-Item -ItemType Directory -Force -Path $WrapperDir | Out-Null Copy-Item -Force -LiteralPath $SeedJar -Destination $WrapperJar Write-Host "[OK] Injected from seed" -ForegroundColor Green } else { throw "ERROR: wrapper jar missing" } } else { Write-Host "[OK] Wrapper verified" -ForegroundColor Green } Write-Host "" # ============================================================================ # [9/16] Set GRADLE_USER_HOME # ============================================================================ Write-Host "==[9/16] Set GRADLE_USER_HOME ==" -ForegroundColor Yellow $OfflineHome = Join-Path $ProjectDir $OFFLINE_HOME_NAME if (!(Test-Path -LiteralPath $OfflineHome)) { throw "ERROR: _offline_gradle_home not found in archive" } $env:GRADLE_USER_HOME = $OfflineHome Write-Host ("GRADLE_USER_HOME = " + $env:GRADLE_USER_HOME) -ForegroundColor Cyan # Check cache $CachesDir = Join-Path $OfflineHome "caches" if (Test-Path -LiteralPath $CachesDir) { $cacheSize = (Get-ChildItem -Path $CachesDir -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum $cacheSizeMB = [math]::Round($cacheSize / 1MB, 2) Write-Host ("[INFO] Cache size: " + $cacheSizeMB + " MB") -ForegroundColor Cyan } else { Write-Host "[WARN] No cache folder found" -ForegroundColor DarkYellow } Write-Host "" # ============================================================================ # [10/16] Verify settings.gradle # ============================================================================ Write-Host "==[10/16] Verify settings.gradle ==" -ForegroundColor Yellow $settingsFile = $null if (Test-Path -LiteralPath ".\settings.gradle") { $settingsFile = "settings.gradle" } elseif (Test-Path -LiteralPath ".\settings.gradle.kts") { $settingsFile = "settings.gradle.kts" } if ($settingsFile) { $content = Get-Content -LiteralPath ".\$settingsFile" -Raw if ($content -match "mavenLocal\(\)" -and $content -match "pluginManagement") { Write-Host "[OK] settings.gradle configured for offline" -ForegroundColor Green } else { Write-Host "[WARN] settings.gradle may not be configured for offline" -ForegroundColor DarkYellow Write-Host "[INFO] Build may fail if plugins not cached" -ForegroundColor Gray } } Write-Host "" # ============================================================================ # [11/16] Test Gradle # ============================================================================ Write-Host "==[11/16] Test Gradle ==" -ForegroundColor Yellow $gradleWorks = $false try { $output = cmd /c ".\gradlew.bat --offline --version 2>&1" if ($LASTEXITCODE -eq 0) { $gradleWorks = $true Write-Host "[OK] Gradle working in offline mode" -ForegroundColor Green } } catch { } if (-not $gradleWorks) { Write-Host "[WARN] Gradle --version failed" -ForegroundColor DarkYellow } Write-Host "" # ============================================================================ # [12/16] Stop Daemon # ============================================================================ Write-Host "==[12/16] Stop Daemon ==" -ForegroundColor Yellow try { cmd /c ".\gradlew.bat --stop 2>&1" | Out-Null } catch { } Start-Sleep -Seconds 2 Write-Host "[OK] Daemon stopped" -ForegroundColor Green Write-Host "" # ============================================================================ # [13/16] Run Offline Build # ============================================================================ Write-Host "==[13/16] Run Offline Build ==" -ForegroundColor Yellow Write-Host "" Write-Host "============================================================" -ForegroundColor Cyan Write-Host " Building with --offline flag" -ForegroundColor Cyan Write-Host " All dependencies from local cache" -ForegroundColor Cyan Write-Host "============================================================" -ForegroundColor Cyan Write-Host "" $buildSuccess = $false $buildTask = $null # Try bootJar Write-Host "[TRY] --offline bootJar..." -ForegroundColor Gray try { cmd /c ".\gradlew.bat --offline clean bootJar --no-daemon" if ($LASTEXITCODE -eq 0) { $buildSuccess = $true $buildTask = "bootJar" } } catch { } # Try jar if (-not $buildSuccess) { Write-Host "[TRY] --offline jar..." -ForegroundColor Gray try { cmd /c ".\gradlew.bat --offline clean jar --no-daemon" if ($LASTEXITCODE -eq 0) { $buildSuccess = $true $buildTask = "jar" } } catch { } } # Try build if (-not $buildSuccess) { Write-Host "[TRY] --offline build..." -ForegroundColor Gray try { cmd /c ".\gradlew.bat --offline build --no-daemon" if ($LASTEXITCODE -eq 0) { $buildSuccess = $true $buildTask = "build" } } catch { } } Write-Host "" if ($buildSuccess) { Write-Host "============================================================" -ForegroundColor Green Write-Host (" BUILD SUCCESS! (task: " + $buildTask + ")") -ForegroundColor Green Write-Host "============================================================" -ForegroundColor Green } else { Write-Host "============================================================" -ForegroundColor Red Write-Host " BUILD FAILED!" -ForegroundColor Red Write-Host "============================================================" -ForegroundColor Red Write-Host "" Write-Host "Possible causes:" -ForegroundColor Yellow Write-Host " - Dependencies not in cache" -ForegroundColor White Write-Host " - Plugin resolution failed" -ForegroundColor White Write-Host " - Need complete build in online env first" -ForegroundColor White throw "Build failed" } Write-Host "" # ============================================================================ # [14/16] Show Build Output # ============================================================================ Write-Host "==[14/16] Build Output ==" -ForegroundColor Yellow $libsDir = Join-Path $ProjectDir "build\libs" if (Test-Path -LiteralPath $libsDir) { Write-Host "build/libs contents:" -ForegroundColor Cyan Get-ChildItem -LiteralPath $libsDir | Format-Table Name, @{L="Size(KB)";E={[math]::Round($_.Length/1KB,1)}} -AutoSize | Out-Host $mainJar = Get-ChildItem -LiteralPath $libsDir -Filter "*.jar" | Where-Object { $_.Name -notmatch "plain|sources|javadoc" } | Select-Object -First 1 } else { Write-Host "[WARN] build/libs not found" -ForegroundColor DarkYellow } Write-Host "" # ============================================================================ # [15/16] Run Instructions # ============================================================================ Write-Host "==[15/16] Run Instructions ==" -ForegroundColor Yellow Write-Host "" if ($mainJar) { Write-Host "To run the application:" -ForegroundColor Cyan Write-Host (" java -jar build\libs\" + $mainJar.Name) -ForegroundColor White Write-Host "" } Write-Host "To rebuild:" -ForegroundColor Cyan Write-Host ' $env:GRADLE_USER_HOME = ".\\_offline_gradle_home"' -ForegroundColor White Write-Host " .\gradlew.bat --offline bootJar --no-daemon" -ForegroundColor White Write-Host "" # ============================================================================ # [16/16] Complete # ============================================================================ Write-Host "============================================================" -ForegroundColor Green Write-Host " Offline Build Complete!" -ForegroundColor Green Write-Host "============================================================" -ForegroundColor Green Write-Host "" Write-Host ("Project: " + $ProjectDir) -ForegroundColor Cyan Write-Host ""