赞
踩
E: cd E:\app\tomcat\ # 预先设定要操作的文件地址、端口变量 $sourceFile = "E:\app\tomcat\update\myProject.war" $targetFile = "E:\app\tomcat\webapps\ROOT.war" $dateTime = Get-Date -Format 'yyyyMMddHHmmss' $renameFile = "E:\app\tomcat\update\myProject_$dateTime.war" $port = ":8080" # 先判断要发布的文件是否存在 if (test-path $sourceFile) { Copy-Item $sourceFile $targetFile Write-Host $sourceFile' has success copy to '$targetFile } else { Write-Host $sourceFile 'Files is not Exist!' exit } # 按端口找到tomcat(java)进程ID(pid) $str = netstat -ano|findstr $port $list = $str.Split('\n'); for ($i=0;$i -lt $list.Length; $i++) { $item_list = [System.Text.RegularExpressions.Regex]::Split($list.Get($i).Trim(), '\s+') for ($j=0;$j -lt $item_list.length; $j++) { if ($list.Get($i).contains($port)) { $p_id = $item_list.Get(4) Write-Host '$p_id'+$p_id break; } } } if($p_id -eq $null) { Write-Output $port 'port is free' } else { Write-Output $port 'port is be used:' Get-Process -id $p_id Stop-Process -id $p_id Write-Host 'kill '$port' process success' } #清空webapps目录和work目录 Function emptyDir($TargetFolder) { $Files = get-childitem $TargetFolder -force Foreach ($File in $Files) { $FilePath=$File.FullName Remove-Item -Path $FilePath -Recurse -Force } return 1; } $isEmpty1 = emptyDir webapps $isEmpty2 = emptyDir work # 移动部署war到部署目录 if($isEmpty1 -eq "1" -and $isEmpty2 -eq "1") { Copy-Item $sourceFile $targetFile Write-Host $sourceFile' has success copy to '$targetFile } sleep -Seconds 5 # 启动tomcat .\bin\startup.bat sleep -Seconds 5 # 后续操作:修改暂存目录中的发布包名称 rename-Item $sourceFile -NewName $renameFile Write-Host $sourceFile ' has success renamed '$renameFile exit
注:该脚本在win10操作系统powershell5.1环境下测试可用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。