部署 ps1 script:
param
(
$projectDirectory,
$targetDirectory
)
$targetDirectory = Get-Location
function RemovePoundComments {
param([string]$file_content)
$file_content = [Regex]::Escape($file_content)
$file_content = $file_content -replace "(\\#.*?\\r\\n|\\#.*)", ""
$file_content = $file_content -replace "(\\r)?\\n", [Environment]::NewLine
$file_content = $file_content -replace "\\\s{1,}", [Environment]::NewLine
return $file_content
}
$file_content = Get-Content "./.env" -raw
$file_content = RemovePoundComments $file_content
$configuration = ConvertFrom-StringData($file_content)
echo $configuration `r
$projectDirectory = $projectDirectory ?? $configuration.projectDirectory
$targetDirectory = $targetDirectory ?? $configuration.targetDirectory
Set-Location -Path $projectDirectory
Get-Location | Write-Host
echo `r
mvn clean && mvn package -DskipTests
$result=$?
if (!$result) {
echo "打包失败" 2>&1
pause
}
$targetFile = -join($projectDirectory, "\target\*.jar")
echo ({targetFile not found}, {targetFile found})[(Test-Path $targetFile)]
echo ({targetDirectory not found}, {targetDirectory found})[(Test-Path $targetDirectory)]
cp -Path $targetFile -Destination $targetDirectory -Recurse
cd -
dir
ls
pm2 start ./start_app.json
PM2 管理 springboot:
{
"apps": {
"name": "项目名称",
"script": "java",
"exec_mode": "fork",
"error_file": "./log/err.log",
"out_file": "./log/spring-boot.log",
"merge_logs": true,
"min_uptime": "60s",
"max_restarts": 30,
"autorestart": true,
"restart_delay": "60",
"watch": true,
"args": [
"-XX:+UseG1GC",
"-jar",
"<目标 jar 包名称>",
"--spring.profiles.active=local_test"
]
}
}
# 加上注释,测试 powershell 是否创建对象成功
#只能分行注释
projectDirectory=<项目所在路径> # test
targetDirectory="复制到的目标目录"
# test=aaa #test
目录结构:
│ .env
│ deploy.ps1
│ start_app.json
│
└───log
err.log
spring-boot.log