用MSBuild自动发布Silverlight xap
最近刚做完一个大型Silverlight战略性项目。在这个项目中,我主要负责了一个核心模块的设计和开发工作。一边写复杂的界面导航,一边写服务的核心代码,同时还要负责安排身边其他同事的工作,并Review他们的代码。另外,还要负责发布项目到测试环境,可把我累坏了,不过,让我也学会了很多东西,一是学会了抗压,二是学会了如何带身边的人。:)
当初,发布项目到测试环境,全是手工操作。效率不高,而且很枯燥。当时是没办法,来不及写工具了。后来测试后期,我闲下来了,就想通过自动化的方式来发布项目到测试环境。因此有了对MSBuild的第一个应用。
下面的过程可以发布Silverlight xap到测试环境:
第一步:编写Project.proj文件,描述你要做那些编译任务。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Main">
<PropertyGroup>
<!--vsts path of silverlight project-->
<VstsProjectPath>$/Vsts/Team/SourceCode/SilverlightPortal</VstsProjectPath>
<!--workspace of vsts path-->
<WorkspacePath>D:\VstsSourceCode\Team\SourceCode\SilverlightPortal</WorkspacePath>
<TFCommand>C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe</TFCommand>
<TestEnvPublishPath>\\127.0.0.1\SilverlightPortal</TestEnvPublishPath>
</PropertyGroup>
<!--Step1:Get latest source code from vsts-->
<Target Name="GetLatest">
<Exec Command="'$(TFCommand)' $(VstsProjectPath) /v:T /force /recursive"
WorkingDirectory="$(WorkspacePath)"
ContinueOnError="false"/>
</Target>
<!--Step2:Build silverlight project-->
<Target Name="Build">
<MSBuild Projects="$(WorkspacePath)\SilverlightPortal.csproj"
Targets="Rebuild"
Properties="Configuration=Release"
ContinueOnError="false"/>
</Target>
<!--Step3:Publish output xap-->
<Target Name="Publish">
<Exec Command="net use $(TestEnvPublishPath) /user:TestEnv\TestUser TestUserPassword"
ContinueOnError="false"/>
<!--copy output xap to network shared directory-->
<Exec Command="copy $(WorkspacePath)\Bin\Release\SilverlightPortal.xap $(TestEnvPublishPath)\ClientBin\SilverlightPortal.xap"
ContinueOnError="false"/>
<Exec Command="notepad $(TestEnvPublishPath)\Web.config"
ContinueOnError="false"/>
</Target>
<Target Name="Main">
<CallTarget Targets="GetLatest" ContinueOnError="false"/>
<CallTarget Targets="Build" ContinueOnError="false"/>
<CallTarget Targets="Publish"/>
</Target>
</Project>
DefaultTargets="Main">
<PropertyGroup>
<!--vsts path of silverlight project-->
<VstsProjectPath>$/Vsts/Team/SourceCode/SilverlightPortal</VstsProjectPath>
<!--workspace of vsts path-->
<WorkspacePath>D:\VstsSourceCode\Team\SourceCode\SilverlightPortal</WorkspacePath>
<TFCommand>C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe</TFCommand>
<TestEnvPublishPath>\\127.0.0.1\SilverlightPortal</TestEnvPublishPath>
</PropertyGroup>
<!--Step1:Get latest source code from vsts-->
<Target Name="GetLatest">
<Exec Command="'$(TFCommand)' $(VstsProjectPath) /v:T /force /recursive"
WorkingDirectory="$(WorkspacePath)"
ContinueOnError="false"/>
</Target>
<!--Step2:Build silverlight project-->
<Target Name="Build">
<MSBuild Projects="$(WorkspacePath)\SilverlightPortal.csproj"
Targets="Rebuild"
Properties="Configuration=Release"
ContinueOnError="false"/>
</Target>
<!--Step3:Publish output xap-->
<Target Name="Publish">
<Exec Command="net use $(TestEnvPublishPath) /user:TestEnv\TestUser TestUserPassword"
ContinueOnError="false"/>
<!--copy output xap to network shared directory-->
<Exec Command="copy $(WorkspacePath)\Bin\Release\SilverlightPortal.xap $(TestEnvPublishPath)\ClientBin\SilverlightPortal.xap"
ContinueOnError="false"/>
<Exec Command="notepad $(TestEnvPublishPath)\Web.config"
ContinueOnError="false"/>
</Target>
<Target Name="Main">
<CallTarget Targets="GetLatest" ContinueOnError="false"/>
<CallTarget Targets="Build" ContinueOnError="false"/>
<CallTarget Targets="Publish"/>
</Target>
</Project>
第二步:编写PublishXap.bat文件,用Project.proj文件做为输入调用msbuild.exe执行编译任务。
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe Project.proj
pause
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe Project.proj
pause
注意,需要你创建一个目录,目录中放Project.proj文件和PublishXap.bat文件。双击bat文件即可执行构建。
——致力于微软企业解决方案、项目管理及技术培训
posted on 2010-09-05 23:08 James.H.Fu 阅读(2631) 评论(4) 编辑 收藏 举报