仕様の説明
以下の処理を行います。
- アプリログフォルダを掃除 (.logを0バイト化して.log以外のファイルを削除する) (設定でOFFにできます)
- Antの
ant.batでAntビルドを実行し、終了を待つ - Tomcatの
startup.batを実行する - ポーリングで疎通確認を行う (Tomcatの起動完了を待つ)
- Tomcatの起動が完了したらビープ音で知らせる (設定でOFFにできます)
- 起動が完了した旨をダイアログで表示する
準備:PowerShellのポリシー設定をしていない場合はしておく
PowerHellを開いて以下を実行
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
PowerShellのソースコードを作成
まずはant-tomcat-watch.ps1みたいな感じのファイルを作ります。
その中に以下のソースコードを書き込んで、
「設定」の部分を自分の環境に合わせて修正して、
エンコードをUFT-8 with BOMにして保存! (Shift_JISでもいいけど)
$ErrorActionPreference = "Stop"
# ===== 設定 =====
# クリア対象のログディレクトリ (クリア処理不要なら変数宣言ごとコメントアウト)
$appLogDir = "H:\applog\"
# antのbinフォルダ
$antBin = "C:\path\to\ant\bin";
# build.xmlのパス
$buildFile = "C:\path\to\your\project\build.xml"
# Tomcatのbinフォルダ
$tomcatBin = "C:\path\to\tomcat\bin"
# 疎通待ちの最大時間(秒)
$timeoutSec = 600
# Tomcat疎通確認用のURL
$watchUrl = "http://localhost:8080/"
# Tomcat起動完了後にビープ音を鳴らすかどうか ($trueなら鳴らす、$falseなら鳴らさない)
$doBeep = $true
# ===== 1) *.log以外を削除して *.logを0バイト化 =====
if ($null -ne $appLogDir -and $appLogDir.Trim().Length -gt 0) {
try {
Get-ChildItem -Path $appLogDir -File -Recurse |
Where-Object { $_.Extension -ne ".log" } |
ForEach-Object {
try {
Remove-Item -Path $_.FullName -Force -ErrorAction Stop
} catch {
Write-Host "削除失敗: $($_.FullName)"
}
}
} catch {
Write-Host "*.log以外の削除処理で例外が発生しました。"
}
try {
Get-ChildItem -Path $appLogDir -Filter "*.log" -File -Recurse | ForEach-Object {
try {
Clear-Content -Path $_.FullName -ErrorAction Stop
} catch {
Write-Host "0バイト化失敗: $($_.FullName)"
}
}
} catch {
Write-Host "*.logの0バイト化処理で例外が発生しました。"
}
}
# ===== 2) Antビルド(defaultターゲット)=====
& (Join-Path -Path $antBin -ChildPath "ant.bat") -f $buildFile
# ===== Antビルド失敗時 =====
if ($LASTEXITCODE -ne 0) {
# ===== 2-1) ビープ =====
if ($doBeep) {
try {
[console]::beep(988, 120)
Start-Sleep -Milliseconds 100
[console]::beep(988, 120)
Start-Sleep -Milliseconds 100
[console]::beep(988, 120)
} catch {
[System.Media.SystemSounds]::Exclamation.Play()
Start-Sleep -Milliseconds 200
[System.Media.SystemSounds]::Asterisk.Play()
}
}
# ===== 6) 完了ダイアログ(最前面表示)=====
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.TopMost = $true
$form.StartPosition = "CenterScreen"
$form.Size = New-Object System.Drawing.Size(0, 0)
$form.ShowInTaskbar = $false
$form.Opacity = 0
$form.Show()
$form.Activate()
$title = "Tomcat起動完了"
$message = " `n`nTomcatの起動が完了しました。`n`n"
$result = [System.Windows.Forms.MessageBox]::Show(
$form,
$message,
$title,
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
)
$form.Close()
$form.Dispose()
exit 1
}
# ===== 3) Tomcat起動(別プロセス)=====
Start-Process -WorkingDirectory $tomcatBin -FilePath (Join-Path $tomcatBin "startup.bat")
# ===== 4) 疎通待ち(HTTPが返るまで)=====
$sw = [Diagnostics.Stopwatch]::StartNew()
while ($true) {
try {
$resp = Invoke-WebRequest -Uri $watchUrl -UseBasicParsing -TimeoutSec 3
# 200~499を「応答あり」とみなす
if ($resp.StatusCode -ge 200 -and $resp.StatusCode -lt 500) { break }
} catch {
Start-Sleep -Milliseconds 1000
}
if ($sw.Elapsed.TotalSeconds -ge $timeoutSec) {
throw "Timed out waiting Tomcat/URL: $watchUrl"
}
}
# ===== 5) ビープ =====
if ($doBeep) {
try {
[console]::beep(988, 120)
Start-Sleep -Milliseconds 100
[console]::beep(988, 120)
Start-Sleep -Milliseconds 100
[console]::beep(988, 120)
} catch {
[System.Media.SystemSounds]::Exclamation.Play()
Start-Sleep -Milliseconds 200
[System.Media.SystemSounds]::Asterisk.Play()
}
}
# ===== 6) 完了ダイアログ(最前面表示)=====
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.TopMost = $true
$form.StartPosition = "CenterScreen"
$form.Size = New-Object System.Drawing.Size(0, 0)
$form.ShowInTaskbar = $false
$form.Opacity = 0
$form.Show()
$form.Activate()
$title = "Tomcat起動完了"
$message = " `n`nTomcatの起動が完了しました。`n`n"
$result = [System.Windows.Forms.MessageBox]::Show(
$form,
$message,
$title,
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
)
$form.Close()
$form.Dispose()
PowerShellウィンドウを開かずxxx.ps1を一発実行できるようにする
- 作った
ant-tomcat-watch.ps1みたいな感じのファイルのショートカットを作る - ショートカットのプロパティを開く
-
[リンク先]に以下を入力して閉じる
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "ant-tomcat-watch.ps1"
このショートカットを叩けば一発で実行できます。
おまけ:ショートカットにアイコンを付けてタスクバーに突っ込む
Windowsの権限関係でできない場合もありますが、
ショートカットをタスクバーにドロップしてピン留めしておくと便利です。
必要な方はお試しあれ!
(アイコンはここら辺から探すといい感じです)
- Tomcatの中にあるexeとかdllとか
- よくあるやつ:shell32.dll
- 音楽系とか:wmploc.dll