0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

『Antビルド⇒Tomcat起動⇒起動監視⇒起動完了をビープ音とダイアログで知らせる』という一連タスクを行うバッチを作る

0
Last updated at Posted at 2026-02-21

仕様の説明

以下の処理を行います。

  1. アプリログフォルダを掃除 (.logを0バイト化して.log以外のファイルを削除する) (設定でOFFにできます)
  2. Antのant.batでAntビルドを実行し、終了を待つ
  3. Tomcatのstartup.batを実行する
  4. ポーリングで疎通確認を行う (Tomcatの起動完了を待つ)
  5. Tomcatの起動が完了したらビープ音で知らせる (設定でOFFにできます)
  6. 起動が完了した旨をダイアログで表示する

準備: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を一発実行できるようにする

  1. 作ったant-tomcat-watch.ps1みたいな感じのファイルのショートカットを作る
  2. ショートカットのプロパティを開く
  3. [リンク先]に以下を入力して閉じる
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "ant-tomcat-watch.ps1"

このショートカットを叩けば一発で実行できます。

おまけ:ショートカットにアイコンを付けてタスクバーに突っ込む

Windowsの権限関係でできない場合もありますが、
ショートカットをタスクバーにドロップしてピン留めしておくと便利です。
必要な方はお試しあれ!

(アイコンはここら辺から探すといい感じです)

  • Tomcatの中にあるexeとかdllとか
  • よくあるやつ:shell32.dll
  • 音楽系とか:wmploc.dll
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?