首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

PowerShell兑现从SVN检出JavaWeb项目并编译

2012-09-01 
PowerShell实现从SVN检出JavaWeb项目并编译用powershell实现从svn服务器检出javaweb项目并自动编译和部署

PowerShell实现从SVN检出JavaWeb项目并编译
用powershell实现从svn服务器检出javaweb项目并自动编译和部署到tomcat,编译结果通过邮件发送相应开发人员
构建脚本:
#dailyBuild.ps1
#初始化配置
Write-Host "正在进行初始化配置……"
#每日构建工作目录配置
$buildHome = "F:\dailyBuild"
#SVN相关配置
$svnHome = "C:\Program Files\VisualSVN Server\bin"
$svnUserName = "tianwei"
$svnPsw = "tianwei"
#tomcat相关配置
$tomcatHome = "F:\tomcat-6.0.20"
#邮件相关配置
$smtpServer = "EX2K7.tfsp.cn"
$smtpUser = "tianwei@tianfusoftwarepark.com"
$smtpPassword = "tfsptianwei"
#收件人地址
$MailToAddress = @()
$MailToAddress += "tianwei@tianfusoftwarepark.com"
$MailToAddress += "hetao@tianfusoftwarepark.com"
#邮件内容
$MailContent = "`r`n"
#邮件附件
$AttachFiles = @()
#项目相关配置@{项目名=svn路径;
#项目名=svn路径}
$projectList = @{"OSPMS"="http://192.168.1.221:88/svn/Component_Libraries/OSPMS/12_Source Code/trunk/OSPMS"}
#从SVN服务器检出项目
function checkOut ([string]$projectName,[string]$projectUrl)
{
Write-Host "正在从svn服务器检出项目:$projectName ……"
Set-Location -path $svnHome
$projectHome = $buildHome+""+$projectName
./svn.exe export $projectUrl $projectHome -q --force --username $svnUserName --password $svnPsw
}
#编译项目
function compileProject ([string]$projectName)
{
Write-Host "正在编译项目:$projectName ……"
#配置编译需要的相关参数
$projectHome = $buildHome+""+$projectName
$sourcePath = $projectHome+"\WEB-INF\src"
$targetPath = $projectHome+"\WEB-INF\classes"
$projectLibPath = $projectHome+"\WEB-INF\lib"
#编译项目,拷贝源代码到项目classes目录
Copy-Item $sourcePath\* $targetPath -recurse
#构造classPath
$CLASSPATH = "%CLASSPATH%;"+$targetPath
$libList = Get-ChildItem -path $projectLibPath -include *.jar -Recurse
Foreach($file in $libList)
{
   $CLASSPATH = $CLASSPATH + ";"+$file
}
$otherLibList = Get-ChildItem -path $tomcatHome\lib -include *.jar -Recurse
Foreach($file in $otherLibList)
{
   $CLASSPATH = $CLASSPATH + ";"+$file
}
#遍历文件夹,获得需要编译的文件列表
$fileList = Get-ChildItem -path $targetPath -include *.java -Recurse
#开始编译
Foreach($file in $fileList)
{
javac -classpath $CLASSPATH $file -encoding utf-8 -g:{source,lines} -Xlint:unchecked -Xstdout $buildHome\tmp.txt
$pro = Get-ItemProperty -path $buildHome\tmp.txt
if($pro.Length -gt 0)
{
Get-Content -Path $buildHome\tmp.txt | add-content -path $buildHome\$projectName.txt
add-content -path $buildHome\$projectName.txt -value "==============================================================="
}
}
}

#部署项目
function deployProject ([string]$projectName)
{
Write-Host "正在部署项目:$projectName ……"
Remove-Item -path $tomcatHome\work\Catalina\localhost\$projectName -recurse -force
Remove-Item -path $tomcatHome\webapps\$projectName -recurse -force
Remove-Item -path $tomcatHome\webapps\$projectName.war -recurse -force
$projectHome = $buildHome+""+$projectName
Set-Location $projectHome
jar cf $buildHome\$projectName.war *
Copy-Item $buildHome\$projectName.war $tomcatHome\webapps -recurse
}
#发送邮件
function sendMail
{
Write-Host "正在发送编译结果给相关人员 ……"
#创建 mail message
$mail = New-Object System.Net.Mail.MailMessage
#设置 addresses
$mail.From = New-Object System.Net.Mail.MailAddress($smtpUser)
Foreach ($element in $MailtoAddress)
{
$mail.To.Add($element)
}
#设置邮件内容
$date = Get-Date
$mail.Subject = "$date 构建结果";
$mail.Priority = "High"
$MailContent = $MailContent + "详情请查看附件。"
$mail.Body = $MailContent
Foreach ($element in $AttachFiles)
{
$attachment = new-Object System.Net.Mail.Attachment($element)
$mail.Attachments.Add($attachment)
}
#发送邮件
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail)
}
#构建
function doBuild
{
Write-Host "清理构建现场……"
#清理编译现场
Remove-Item -path $buildHome\* -recurse -force
#停止tomcat
Write-Host "停止tomcat……"
Set-Location $tomcatHome\bin
cmd /C shutdown.bat
#开始构建
Write-Host "开始构建……"
foreach($key in $projectList.Keys)
{
#从svn检出项目
checkOut $key $projectList[$key]
#编译项目
compileProject $key
#判断编译是否通过
if(Get-Content -Path $buildHome\$key.txt | select-string -pattern "错误" -Quiet)
{
Write-Host "$key 编译未通过!"
$MailContent = $MailContent + $key + ":编译未通过!`r`n`n"
}
else
{
Write-Host "$key 编译通过!"
$MailContent = $MailContent + $key + ":编译通过!`r`n`n"
deployProject $key
}
$AttachFiles += $buildHome+""+$key+".txt"
}
Write-Host "构建结束……"
#发送邮件
sendMail
Write-Host "启动tomcat"
#启动tomcat
Set-Location $tomcatHome\bin
cmd /C startup.bat
}
#执行构建
doBuild

热点排行