`

项目管理实践【五】自动编译和发布【Using Visual Studio with Source Control System to build and publish website automatically】

 
阅读更多

在上一篇教程项目管理实践【三】每日构建【Daily Build Using CruiseControl.NET and MSBuild】 中,我们讲解了如何使用CCNET+MSBuild来自动编译项目,今天我们讲解一下怎么使用MSBuild+WebDeployment+Robocopy自动编译过和部署ASP.NET网站。
首先安装下面的三个软件:

1.MSBuild.Community.Tasks下载:
http://msbuildtasks.tigris.org/files/documents/3383/28296/MSBuild.Community.Tasks.msi

源代码:
http://msbuildtasks.tigris.org/files/documents/3383/36642/MSBuild.Community.Tasks.v1.2.0.306.zip

2.WebDeployment下载:
http://download.microsoft.com/download/c/c/b/ccb4877f-55f7-4478-8f16-e41886607a0e/WebDeploymentSetup.msi

3.Utility Spotlight Robocopy GUI 下载:【下载后,解压后安装,Vista不用安装】
http://download.microsoft.com/download/f/d/0/fd05def7-68a1-4f71-8546-25c359cc0842/UtilitySpotlight2006_11.exe

安装完成后,就开始今天的教程了。

我们以前面教程中创建的StartKit解决方案为例子,结构如下:

在上图所示的Web项目StartKit上右键点击,然后点击Add Web Deployment Project…,如下图:

弹出下面的窗体,分别输入部署项目名称和项目要放置的位置,如下图:

点击OK按钮后,解决方案的结构如下图:

今天会讲到下面二个方法,上面的步骤一样,从这里开始,下面的步骤有区别。

方法一:使用WebDeployment创建虚拟目录

优点:使用简单

缺点:功能不够强大,只能部署到虚拟目录

右键点击部署项目,点击菜单中的Property Pages,如下图:

在下面的窗体中,点击左侧的Complication,在右侧的Output Folder下的文本框中输入编译后网站文件的输出路径:

然后,点击左侧的Deploment,在右侧选中Create an IIS virtual directory for the output folder前面的CheckBox,在下面的Virtual directory name下的文本框中输入虚拟目录的名字,Replace the existing virtual directory前面的CheckBox根据实际情况确定是否选中,如下图:

点击确定按钮,编译部署项目StartKit.csproj_deploy,编译成功后,我们打开IIS,在默认网站下可以看到虚拟目录StartKit。OK,成功了!

方法二:使用WebDeployment+MSBuild+Robocopy

优点:功能强大

缺点:配置有点麻烦

这个方法不用配置Property Pages,直接右键点击StartKit.csproj_deploy项目文件,在菜单中点击Open Project File打开部署项目文件:

修改部署项目文件为下面的内容:

<!--Microsoft Visual Studio 2008 Web Deployment Project http://go.microsoft.com/fwlink/?LinkID=104956-->

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{00000000-0000-0000-0000-000000000000}</ProjectGuid>
<SourceWebPhysicalPath>../StartKit</SourceWebPhysicalPath>
<SourceWebProject>{96E1A089-3FBB-4909-94F6-172665994449}|StartKit/StartKit.csproj</SourceWebProject>
<SourceWebVirtualPath>/StartKit.csproj</SourceWebVirtualPath>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<ProjectName>StartKit</ProjectName>
<Major>1</Major>
<Minor>0</Minor>
<Revision>0</Revision>
<VSSName>ttzhang</VSSName>
<VSSPassword>123456</VSSPassword>
<FtpName>anonymous</FtpName>
<FtpPassword>anonymous</FtpPassword>
<SmtpServerName>smtp.163.com</SmtpServerName>
<FromAddress>ttzhang@163.com</FromAddress>
<ToAddress>zttc@163.com</ToAddress>
<MailPassword>testmail</MailPassword>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>./Debug</OutputPath>
<EnableUpdateable>true</EnableUpdateable>
<UseMerge>true</UseMerge>
<SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<OutputPath>./Release</OutputPath>
<EnableUpdateable>true</EnableUpdateable>
<UseMerge>true</UseMerge>
<SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
<!--下面的ItemGroup节点可选,这个和项目文件StartKit.csproj中的内容相同-->
<ItemGroup>
<ProjectReference Include="../BLL/BLL.csproj">
<Project>{73A293A1-CDCC-4919-9B05-BA2531ADDB56}</Project>
<Name>BLL</Name>
</ProjectReference>
<ProjectReference Include="../DAL/DAL.csproj">
<Project>{AFF6077D-DD2D-48A0-BFAD-051BD67A6953}</Project>
<Name>DAL</Name>
</ProjectReference>
<ProjectReference Include="../IBLL/IBLL.csproj">
<Project>{620770BB-7A27-4585-9B97-44EEE349121D}</Project>
<Name>IBLL</Name>
</ProjectReference>
<ProjectReference Include="../Model/Model.csproj">
<Project>{EA43EC2E-5890-4431-BD3E-5F6C090DEA3A}</Project>
<Name>Model</Name>
</ProjectReference>
</ItemGroup>
<!--引入MSBuildCommunityTasks-->
<Import Project="$(MSBuildExtensionsPath)/MSBuildCommunityTasks/MSBuild.Community.Tasks.Targets" />
<!--邮件发送-->
<!--<Target Name="EmailTest" >
<Message Text = " Mail sending..."></Message>
<Mail SmtpServer="$(SmtpServerName)"
Subject="Test"
Password="$(MailPassword)"
From ="$(FromAddress)"
To ="$(ToAddress)"
Body="This is a test of the mail task." />
</Target>-->
<!--备份文件到FTP-->
<!--<Target Name="Backup" DependsOnTargets="Zip" >
<FtpUpload UserName="$(FtpName)"
Password="$(FtpPassword)"
LocalFile="$(ZipFileName)"
RemoteUri="ftp://192.168.1.2/SourceBackup/$(ZipFileName)" />
<OnError ExecuteTargets="HandleErrorBackup" />
</Target>-->
<!--备份文件到FTP失败则发送邮件-->
<!--<Target Name="HandleErrorBackup">
<Message Text="Backup failed..............." />
<Mail SmtpServer="$(SmtpServerName)"
To="$(ToAddress)"
From="$(FromAddress)"
Subject="$(ProjectName) Build failed"
Body="Backup Failure: Could not finish Backup ." />
</Target>-->
<!--编译项目-->
<Target Name="BuildProjectReferences">
<MSBuild Projects="@(ProjectReference)" Targets="Build" />
</Target>
<!--生成压缩文件-->
<Target Name="Zip">
<!--时间格式-->
<Time Format="yyyyMMddHHmmss">
<Output TaskParameter="FormattedTime" PropertyName="buildDate"/>
</Time>
<Zip Files="@(ZipFiles)" ZipFileName="StartKit V$(Major)-$(Minor)-$(Revision)-$(buildDate).zip"/>
</Target>
<!--复制文件-->
<Target Name="Copy">
<!--停止IIS服务-->
<ServiceController ServiceName="w3svc" Action="Stop" />
<!--使用Robocopy复制编译后的文件到指定位置 /XD是要忽略的文件夹,/XF要忽略的文件类型-->
<Exec Command="Robocopy Debug c:/inetpub/StartKit /MIR /XD Fckeditor attachments .svn obj doc Test /XF *.zip *.wdproj *.user *.cs *.csproj" IgnoreExitCode="true" />
<!--启动IIS服务-->
<ServiceController ServiceName="w3svc" Action="Start" />
</Target>
<!--引入WebDeployment-->
<Import Project="$(MSBuildExtensionsPath)/Microsoft/WebDeployment/v9.0/Microsoft.WebDeployment.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.WebDeployment.targets.-->
<Target Name="BeforeBuild"></Target>
<Target Name="BeforeMerge"></Target>
<Target Name="AfterMerge"></Target>
<Target Name="AfterBuild">
<!--编译成功后,执行下面的Targets-->
<!—不想生成ZIP文件,可以注释下面ZIPtarget-->
<CallTarget Targets="Zip"/>
<CallTarget Targets="Copy" />
<!--<CallTarget Targets="EmailTest"/>
<CallTarget Targets="Backup" />-->
</Target>
</Project>

编译部署项目成功后,打开C/inetpub/StartKit文件夹,看看是否成功复制过去了呢?好的,我去看看,哈哈,文件果然都在,OK,成功啦!

这时候,在IIS上创建一个虚拟目录或者网站,指向我们部署项目中指定的目录。上一篇我们已经将该项目添加到了CCNET中,所以以后我们每次提交代码后,MSBuild就会编译整个解决方案【当然也会编译部署项目】,如果编译成功,就会自动将最新的程序部署到我们网站上。这样就可以使网站和我们的开发实时保持同步,这只不是唯一的实现方法,其他还有很多可以实现这个功能的方法,大家可以在这里讨论和交流。

补充:
Microsoft Build Engine (MSBuild) Microsoft Visual Studio 的新的生成平台。MSBuild 在如何处理和生成软件方面是完全透明的,使开发人员能够在未安装 Visual Studio 的生成实验室环境中组织和生成产品。通过这几篇教程,我们可以看出,MSBuild的强大功能,如果希望理解更多关于MSBuild的信息,请查看这里http://msdn.microsoft.com/zh-cn/library/ms171451.aspx

如果你对我讲解的这些内容不熟悉,建议你从头开始看这个系列的教程:项目管理实践系列教程

分享到:
评论

相关推荐

    Software Testing using VisualStudio2012

    Learn different testing techniques and features of Visual Studio 2012 with detailed explanations and real-time samples Overview Using Test Manager and managing test cases and test scenarios ...

    Mastering Visual Studio 2019 2nd Edition

    Mastering Visual Studio 2019: Become proficient in .NET Framework and .NET Core by:using advanced coding techniques in Visual Studio, 2nd Edition eBook Details: Paperback: 374 pages Publisher: WOW! ...

    微软Visual Studio 2017社区版安装文件

    •Visual Studio Build Tools now supports installing into a container, and we added support for building Azure, UWP, and additional project types. •You can create build servers without installing all ...

    How to Write and Publish a Scientific Paper

    How to Write and Publish a Scientific Paper 非影印版,可以复制粘贴

    How_to_write_and_publish_a_scientific_paper

    How to write and publish a scientific paper ContentsChapter 1 What Is Scientific Writing? Chapter 2 Origins of Scientific Writing Chapter 3 What Is a Scientific Paper? Chapter 4 How to Prepare the ...

    How to Write & Publish a Scientific Paper

    How to Write & Publish a Scientific Paper,教你怎样发布期刊,硕博士极力推荐

    Developing.Bots.with.Microsoft.Bots.Framework

    Develop Intelligent Bots using Microsoft Bot framework (C# and Node.js), Visual Studio Enterprise & Code, Microsoft Azure and Cognitive Services. This book shows you how to develop great Bots, ...

    Building Android Apps in Python Using Kivy with Android Studio.pdf

    Start building Python-based Android applications using Kivy with Android Studio. Through in-depth examples, this book teaches you everything you need to create your first Android application in Python...

    ComponentOne 2012 V2 Studio for Silverlight(完整安装)

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. § Stunning and ...

    ComponentOne Studio for Silverlight 2012 v3 2/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    ComponetOne Studio for WPF 2013 v1 3/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    ComponentOne Studio for WPF2012 v3 1/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    Radar Systems Analysis and Design Using MatLab

    efforts have been made to publish reliable data and information, but the author and the publisher cannot assume responsibility for the validity of all materials or for the consequences of their use.

    blogdown: Creating Websites with R Markdown

    By default, blogdown uses Hugo, a popular open-source static website generator, which provides a fast and flexible way to build your site content to be shared online. Other website generators like ...

    How To Write And Publish A Scientific Paper

    罗伯特•戴(Robert A....她是得克萨斯A&M大学副教授,撰写了《将科学介绍给大众》、《科学与教学:大学与专业学校老师指南》等书,同时也是科学编辑委员会的刊物《科学编辑》的编辑之一,具有丰富的教学和实践经验。

    ComponentOne Studio for Silverlight 2012 v3 1/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    ComponentOne Studio for Silverlight 2012 v3 3/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    ComponentOne Studio for WPF2012 v3 2/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

    GNU Make自动化编译工具-编译静态库与动态库 示例makefile.zip

    所以像Visual Studio等等IDE都是集成了自动化编译工具的,NMAKE 命令就是MS平台下的自动化编译工具。还有其它平台使用如 Ant、Automake、CMake等等,而GNU社区也有自己的自动化编译工具,它就是 GNU make。很早之前...

    ComponentOne Studio for WPF2012 v3 3/3

    By just setting a few brush properties in Visual Studio you can quickly give a unique look to any C1 control without having to work in Expression Blend or be a professional designer. Stunning and ...

Global site tag (gtag.js) - Google Analytics