1. 查找最高版本的MsBuildTools。
  2. 清理缓存垃圾。
  3. 还原NuGet包。
  4. 构建解决方案。
  5. 按项目发布程序到本地。
  6. 按项目ZIP打包。
<#
.NOTES
===========================================================================
Created with: Visual Studio 2019
Created on: 2019/8/26 11:31
Created by: Allen
Organization: Allen
Filename: build_full.ps1
===========================================================================
.DESCRIPTION
ERP devops scripts.
#> # 声明全局变量
# 定义方法开始时输出的前景色
$LogForegroundColorStart = "Green"
# 定义方法结束时输出的前景色
$LogForegroundColorEnd = "Cyan"
# 定义方法输出警告时的前景色
$LogForegroundColorWarning = "DarkYellow" Write-Host "`nPublish parameters initializing..." -Foreground $LogForegroundColorStart # Set user level's parameters
# 定义当前脚本文件的绝对全路径
$CurrentScriptFilePath = $MyInvocation.MyCommand.Definition
# 定义当前脚本文件的所在的文件夹路径
$ScriptWorkPath = Split-Path -Parent $CurrentScriptFilePath
# 定义当前脚本文件的所在的解决方案文件夹路径
$SolutionPath = Split-Path -Parent $ScriptWorkPath
# 定义当前编译工作的文件夹路径
$BuildPath = "$SolutionPath\Builds"
# 定义要编译的解决方案名称
$SolutionName = "ERP"
# 定义要编译的解决方案文件名称
$SolutionFile = "$SolutionPath\$SolutionName.sln"
# 定义要发布的Web项目名称列表
$WebProjectNames = "API1", "API2", "API3"
# 定义要发布的Windows项目名称列表
$WinProjectNames = , "Job" # 只有一个元素的字符串数组,前面加 , 定义 表示
# 定义要打包的项目名称列表
$PackageNames = "API1", "API2", "API3", "Job" # NuGet parameters
# 定义NuGet文件夹路径
$NuGetPath = "$SolutionPath\.nuget"
# 定义NuGet.exe文件路径
$NuGetExe = "$NuGetPath\NuGet.exe"
# 定义是否执行NuGet包还原: Y|N
$IfUseNuGetReStore = "Y" # value is Y or N
# 定义是否更新NuGet.exe主程序: Y|N
$IfUpdateNuGetTool = "N" # value is Y or N # Set system level's parameters
# 定义编译时要使用的解决方案配置: Debug|Release
$Configuration = "Debug"
# 定义编译时要输出的日志等级: quiet|minimal|normal|detailed|diagnostic
$LogLevel = "normal"
# 定义要构建的.NET Framework版本
# Note: That the MSBuild tool version and VisualStudio version and the TargetFramework version have dependencies
$TargetFrameworkVersion = "4.5"
# 定义MsBuild.exe(Microsoft Build Tools)文件路径
$MSBuildExe = ""
# 定义MsBuild.exe(Microsoft Build Tools)版本
$MsBuildToolsVersion = "" # 从高往低版本逐个找,优先使用最高版本的Microsoft Build Tools
Function Set-Msbuild-Tools
{
# Visual Studio 2017 以上版本独立安装的 Build Tools
if (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\*\BuildTools\MSBuild\*\Bin\MSBuild.exe")
{
if (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"
$script:MsBuildToolsVersion = "Current"
}
elseif (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe"
$script:MsBuildToolsVersion = "15.0"
}
}
# Visual Studio 2019 内置的 Build Tools
elseif (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\*\MSBuild\Current\Bin\MSBuild.exe")
{
if (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
}
elseif (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe"
}
else
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"
}
$script:MsBuildToolsVersion = "Current"
}
# Visual Studio 2017 内置的 Build Tools
elseif (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\*\MSBuild\15.0\Bin\MSBuild.exe")
{
if (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"
}
elseif (Test-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"
}
else
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
}
$script:MsBuildToolsVersion = "15.0"
}
# Visual Studio 2015 以下版本独立安装的 Build Tools
elseif (Test-Path "${env:ProgramFiles(x86)}\MSBuild\*\Bin\MSBuild.exe")
{
if (Test-Path "${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe"
$script:MsBuildToolsVersion = "14.0"
}
elseif (Test-Path "${env:ProgramFiles(x86)}\MSBuild\12.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\MSBuild\12.0\Bin\MSBuild.exe"
$script:MsBuildToolsVersion = "12.0"
}
elseif (Test-Path "${env:ProgramFiles(x86)}\MSBuild\10.0\Bin\MSBuild.exe")
{
$script:MSBuildExe = "${env:ProgramFiles(x86)}\MSBuild\10.0\Bin\MSBuild.exe"
$script:MsBuildToolsVersion = "10.0"
}
} # 如果上面的都找不到,则尝试使用系统内置的.NET Framework版本
if ($script:MSBuildExe -eq "")
{
if (Test-Path "${env:SystemRoot}\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe")
{
$script:MSBuildExe = "${env:SystemRoot}\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
$script:MsBuildToolsVersion = "4.0"
}
elseif (Test-Path "${env:SystemRoot}\Microsoft.NET\Framework\v3.5\MSBuild.exe")
{
$script:MSBuildExe = "${env:SystemRoot}\Microsoft.NET\Framework\v3.5\MSBuild.exe"
$script:MsBuildToolsVersion = "3.5"
}
else
{
$script:MSBuildExe = "${env:SystemRoot}\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe"
$script:MsBuildToolsVersion = "2.0"
}
}
}
Set-Msbuild-Tools
Write-Host "`nCurrent Microsoft Build Tools Path: $MSBuildExe" -Foreground $LogForegroundColorStart
Write-Host "Current Microsoft Build Tools Version: $MsBuildToolsVersion" -Foreground $LogForegroundColorEnd # 定义脚本执行时间/ZIP打包时间
$ExecuteDate = Get-Date
# 定义脚本执行时的最近一条Git Log Commit Id前7位
$GitLogLatestCommitId = (git rev-list Head -n 1 --abbrev-commit)
# 定义脚本日志输出文件路径
$LogFile = "$BuildPath\build_$($ExecuteDate.ToString("yyyyMMddHHmmss")).log" Write-Host "`nPublish parameters initialize completed." -Foreground $LogForegroundColorEnd # 开始脚本日志记录
# 注意:脚本执行过程不能中断,否则会导致log file被线程占用,下次执行时会无法删除导致报错
Start-Transcript -Path $LogFile # 清理缓存文件,垃圾文件
Function Clean-Caches
{
# 清理log文件
Write-Host "`nLog files cleaning..." -Foreground $LogForegroundColorStart
Get-ChildItem -Path "$BuildPath\" -Exclude $LogFile -Include "*.log", "*.zip" -Recurse -Force | Where-Object { $_.FullName -ne $LogFile } | Remove-Item -Force
Write-Host "Log files clean completed." -Foreground $LogForegroundColorEnd # 清理项目编译后缓存文件夹
Write-Host "`nProject build cache files cleaning..." -Foreground $LogForegroundColorStart
foreach ($packageName in $PackageNames)
{
$cacheFolder = "$BuildPath\$packageName"
Remove-Item -Path $cacheFolder -Recurse -Force -ErrorAction "SilentlyContinue"
}
# 清理解决方案
&$MSBuildExe $SolutionFile /t:Clean
Write-Host "Project build cache files clean completed." -Foreground $LogForegroundColorEnd
}
Clean-Caches # 还原NuGet包
Function ReStore-Package
{
if ($IfUseNuGetReStore -eq "Y")
{
if ($IfUpdateNuGetTool -eq "Y")
{
Write-Host "`nNuGet tools Start updating..." -Foreground $LogForegroundColorStart
&$NuGetExe update -Self
Write-Host "NuGet tools update completed." -Foreground $LogForegroundColorEnd
}
else
{
Write-Host "Not update NuGet tools." -Foreground $LogForegroundColorEnd
} Write-Host "`nNuGet Start ReStoreing..." -Foreground $LogForegroundColorStart
&$NuGetExe restore $SolutionFile
Write-Host "NuGet ReStore completed." -Foreground $LogForegroundColorEnd
}
else
{
Write-Host "`nNot use NuGet tools." -Foreground $LogForegroundColorStart
}
}
ReStore-Package # 构建解决方案
Function Build-Solution
{
Write-Host "`nStart solution building..." -Foreground $LogForegroundColorStart
&$MSBuildExe $SolutionFile "/t:Build" "/toolsversion:$MsBuildToolsVersion" "/verbosity:$LogLevel" "/logger:FileLogger,Microsoft.Build;logfile="""$BuildPath\$($SolutionName)_Build_$($Configuration)_$($ExecuteDate.ToString("yyyyMMddHHmmss")).log"""" "/p:Configuration=$Configuration"
Write-Host "Build solution completed." -Foreground $LogForegroundColorEnd
}
Build-Solution # 发布项目集
Function Publish-Projects
{
# Win项目发布到本地文件夹
foreach ($projectName in $WinProjectNames)
{
Write-Host "`nStart $projectName project publishing..." -Foreground $LogForegroundColorStart
&$MSBuildExe "$($SolutionPath)\$($projectName)\$($projectName).csproj" "/t:Rebuild" "/toolsversion:$MsBuildToolsVersion" "/verbosity:$LogLevel" "/logger:FileLogger,Microsoft.Build;logfile="""$BuildPath\$($projectName)_Publish_$($Configuration)_$($ExecuteDate.ToString("yyyyMMddHHmmss")).log"""" "/p:Configuration=$Configuration" "/p:OutputPath=$($BuildPath)\$($projectName)"
Write-Host "Publish $projectName project completed." -Foreground $LogForegroundColorEnd
} # Web项目发布到本地文件夹
foreach ($projectName in $WebProjectNames)
{
Write-Host "`nStart $projectName project publishing..." -Foreground $LogForegroundColorStart
&$MSBuildExe "$($SolutionPath)\$($projectName)\$($projectName).csproj" "/t:WebPublish" "/verbosity:$LogLevel" "/logger:FileLogger,Microsoft.Build;logfile="""$BuildPath\$($projectName)_Publish_$($Configuration)_$($ExecuteDate.ToString("yyyyMMddHHmmss")).log"""" "/p:Configuration=$Configuration" "/p:WebPublishMethod=FileSystem" "/p:DeleteExistingFiles=True" "/p:publishUrl=$($BuildPath)\$($projectName)"
Write-Host "Publish $projectName project completed." -Foreground $LogForegroundColorEnd
}
}
Publish-Projects # ZIP打包
Function Zip-Package
{
foreach ($packageName in $PackageNames)
{
Write-Host "`nStart $packageName project packing..." -Foreground $LogForegroundColorStart $targetFolder = "$BuildPath\$packageName\"
# exclusion rules. Can use wild cards (*)
$exclude = "*.config", "*.bak", "*.log", "*.zip" #, "*.pdb", "*.xml", "bin\*.config"
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $targetFolder -Exclude $exclude # compress
$destination = "$BuildPath\$($packageName)_$($GitLogLatestCommitId)_$($ExecuteDate.ToString("yyyyMMddHHmmss")).zip"
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Optimal Write-Host "Pack $packageName project completed." -Foreground $LogForegroundColorEnd
}
}
Zip-Package # 结束脚本日志记录
Stop-Transcript

[Powershell]使用Msbuild构建基于.NET Framework的WebAPI项目的更多相关文章

  1. 将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3

    在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF:同时还发布了 Visual Studio 2019 预览版 ...

  2. [Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目

    获取要发布的定时计划任务. 禁用和停止定时计划任务. 批量强制结束Job进程. 打印定时计划任务状态. 备份项目文件夹. 发布项目文件夹. 删除部署包. 启用定时计划任务. <# .NOTES ...

  3. Net 项目构建基于Jenkins + Github + Mono 的持续集成环境

    Net 项目构建基于Jenkins + Github + Mono 的持续集成环境 阅读目录 1 安装 2 配置 3 测试 在Redhat enterprise 6.5 的服务器上,为在gutub 上 ...

  4. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  5. step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework

    文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...

  6. maven-bundle-plugin插件, 用maven构建基于osgi的web应用

    maven-bundle-plugin 2.4.0以下版本导出META-INF中的内容到MANIFEST.MF中 今天终于把maven-bundle-plugin不能导出META-INF中的内容到Ex ...

  7. ASP.NET WEB API构建基于REST风格

    使用ASP.NET WEB API构建基于REST风格的服务实战系列教程[开篇] 最近发现web api很火,园内也有各种大神已经在研究,本人在asp.net官网上看到一个系列教程,原文地址:http ...

  8. 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现

    <在ASP.NET Core中使用Apworks快速开发数据服务>一文中,我介绍了如何使用Apworks框架的数据服务来快速构建用于查询和管理数据模型的RESTful API,通过该文的介 ...

  9. Apworks框架实战(六):使用基于Entity Framework的仓储基础结构

    在前面的章节中,我们已经设计了一个简单的领域模型,接下来我们希望能够实现领域模型的持久化及查询.在Apworks中,实现了面向Entity Framework.NHibernate以及MongoDB的 ...

随机推荐

  1. Winforn中设置ZedGraph多条Y轴时曲线刻度不均匀问题解决

    场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...

  2. jdk8 HashMap tableSizeFor

    今天读jdk8  HashMap源码,构造函数中 根据initialCapacity初始化threshold public HashMap(int initialCapacity, float loa ...

  3. 打造游戏金融小程序行业测试标准腾讯WeTest携各专家共探品质未来

    在获客成本不断上升的时代里,产品品质愈发是互联网应用的决胜标准.随着用户需求更加多样,开发者不仅要深挖应用功能,更需要面向业务所在领域,建立全面.专业的测试架构,掌控开发进度.提高开发效率,才能在互联 ...

  4. java--Proreties

    Prorerties /* * Properties,内存与文件信息交互 * 表示了一个持久的属性集 * * 构造方法: * Properties() * * */ //简单使用 创建,添加,遍历, ...

  5. Asp.net管道模型之(HttpModules 和 HttpHandler)

    上一节我们从大概范围介绍了管道模型的整体流程,我们从其中知道管道最重要的两大组件为:HttpModules 跟 HttpHandler.今天我们着重来介绍一下这两大组件 一:asp.net处理管道 从 ...

  6. redis报错: redis.exceptions.ResponseError: value is not an integer or out of range

    问题描述 今天在使用python的redis客户端时碰到了这样的报错:redis.exceptions.ResponseError: value is not an integer or out of ...

  7. docker研究-4 docker镜像制作

    这次实验以centos镜像为基础镜像进行相关docker镜像制作. 1. 下载centos镜像 [root@localhost ~]# docker pull centosUsing default ...

  8. Rust中的Rc--引用计数智能指针

    大部分情况下所有权是非常明确的:可以准确的知道哪个变量拥有某个值.然而,有些情况单个值可能会有多个所有者.例如,在图数据结构中,多个边可能指向相同的结点,而这个结点从概念上讲为所有指向它的边所拥有.结 ...

  9. SDL2学习(一): 显示一张图片

    SDL是一个跨平台的多媒体库,它通过OpenGL和2D视频帧缓冲,提供了针对音频.视频.键盘.鼠标.控制杆及3D硬件的低级别的访问接口.这里使用较新的SDL2库. 1. 配置SDL开发环境 1.1 下 ...

  10. JS高阶---原型链

    [大纲] [主体] 1.创建函数 注意:Object内置原生对象原来就有 2.添加实例方法 3.根据构造函数创建实例对象 原型链寻找 1.本身有在本身找 2.本身没有往摸着隐式原型链往里找 或者再上层 ...