VS打包之 MSBuild Community Tasks 使用介绍

本文重点介绍Windows下,在不调用外部工具的前提下,对目标文件和相关的资源文件进行打包。

Windows鸡肋的命令行使得 VS 构建后的打包发布很是头疼,最简单的打包也没有内建命令,更没类似于 Java 下 Maven 的助攻。唯一找到的 .NET MAVEN PLUGIN 还需要依赖 dotnet core 这个外援,惨呐惨!

如果需要在 build 完成后做一些打包之类的处理,一种方式是在 VS 的 post-build 中编写脚本,另外一种方式是使用 MSBuild Community Tasks 这个 Nuget 包,Github 地址:https://github.com/loresoft/msbuildtasks。MSBuild Community Tasks是一个开源的封装了MSBuild Task的库,支持诸如FTP上传,发送邮件,移动文件,运行NUnit测试,Svn操作,Zip压缩及解压缩等,详细支持的Task详见官方文档:Community Tasks 。

Zip 打包

当前目录结构如下:

| Hello.csproj
| packages.config
| Hello.sln
├─bin
│  ├─Debug
│  └─Release
├─obj
├─packages
│  └─MSBuildTasks.1.5.0.235
│      │  MSBuildTasks.1.5.0.235.nupkg
│      │  
│      ├─build
│      │      MSBuildTasks.targets
│      │      
│      └─tools
│              MSBuild.Community.Tasks.dll
│              MSBuild.Community.Tasks.Targets
│              MSBuild.Community.Tasks.xml
│              MSBuild.Community.Tasks.xsd
│              
├─scripts
│  │  Web.config
│  ├─includes
│  │      advanced.html
│  ├─src
│  │      createTestFiles.txt
│  ├─testfiles
│  │      image.png
│  └─ui
│      ├─images
│      │     icon.png
│      └─js
│            jquery.js
└─src
        HellowWord.cs

安装 MSBuild Community Tasks

安装方式参考官方文档。安装完 MSBuild Community Tasks 之后,packages.config 中会多出一条配置信息:

  

同时 Hello.csproj 会增加如下信息:

  
  
    
      This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
    
    
  

在上述配置后面追加打包的配置,例如:




    
  
  
  


















在 Release 模式下构建成功后,将生成的目标文件和scripts目录下的所有文件打包到 kits/Hello.zip 文件中,并保持目录结构,zip中文件结构

Hello
| Web.config
├─lib
│  Hello.dll
├─includes
│      advanced.html
├─src
│      createTestFiles.txt
├─testfiles
│      image.png
└─ui
   ├─images
   │   icon.png
   └─js
       jquery.js

Reference

Zip 参考:

  • 官方Zip示例

  • Zip其他参数及含义 - 参考代码注释

Task参考:

  • RemoveDir Task 文档

  • Copy Task 文档

  • 所有可用Task浏览

Other Example:

techstay/MSBuildExample

你可能感兴趣的:(VS打包之 MSBuild Community Tasks 使用介绍)