代码改变世界

ASP.NET MVC 使用MSBuild生成的几个注意事项

2013-11-18 00:55  阮萤  阅读(1608)  评论(2编辑  收藏  举报

做新项目,当时参考NopCommerce的结构,后台Nop.Admin是一个独立的Area Web Site,但部署的时候发现,使用一键发布,Admin Area会丢失。

 

研究了下NopCommerce的做法,使用MSBuild组织文件结构,关键的地方在下面:

 

<!--Now lets publish Nop.Admin-->
<MSBuild Projects="$(RootFolder)\Presentation\Nop.Web\Administration\Nop.Admin.csproj"
  Targets="ResolveReferences;_CopyWebApplication"
  Properties="WebProjectOutputDir=$(DeployFolder)\$(DeployPrefix)\Administration\;
  OutDir=$(DeployFolder)\$(DeployPrefix)\Administration\bin\;Configuration=$(Configuration)" />


<!--Now lets move the Nop.Admin/bin/ to the root bin/-->
<CreateItem Include="$(DeployFolder)\$(DeployPrefix)\Administration\bin\*.dll">
  <Output TaskParameter="Include" ItemName="CompileOutput" />
</CreateItem>
<Copy SourceFiles="@(CompileOutput)"
  DestinationFolder="$(DeployFolder)\$(DeployPrefix)\bin\" />


<!--Lets delete the bin in Nop.Admin-->
<RemoveDir Directories="$(DeployFolder)\$(DeployPrefix)\Administration\bin\" />
        
<!--Lets delete the packages.config files-->
<Delete Files="$(DeployFolder)\$(DeployPrefix)\Administration\packages.config;
  $(DeployFolder)\$(DeployPrefix)\packages.config"/>

 

然后发布的时候可以使用Web.Release.config替换Web.config

<!--Transform support-->
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll"/>

<!--Transform root web.config file-->
<TransformXml Condition="Exists('$(WebFolder)\Web.$(Configuration).config')" Source="$(WebFolder)\Web.config" Transform="$(WebFolder)\Web.$(Configuration).config" Destination="$(DeployFolder)\$(DeployPrefix)\Web.config" />

使用Settings.Release.txt替换Settings.txt

<!--Lets Copy \App_Data\Settings.Release.txt To \App_Data\Settings.txt -->
<Copy Condition="Exists('$(WebFolder)\App_Data\Settings.$(Configuration).txt')"
  SourceFiles="$(WebFolder)\App_Data\Settings.$(Configuration).txt"
    DestinationFiles="$(DeployFolder)\$(DeployPrefix)\App_Data\Settings.txt"
SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
<!--Lets delete the Settings.Release.txt file-->
<Delete Condition="Exists('$(DeployFolder)\$(DeployPrefix)\App_Data\Settings.$(Configuration).txt')" 
    Files="$(DeployFolder)\$(DeployPrefix)\App_Data\Settings.$(Configuration).txt"/>

MSBuild配置文件,可下载参考。

 

装了Win8+VS2013后,生成时会提示:

error MSB4019: 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”。请确认 <Import> 声明中的路径正确,且磁盘上存在该文件。

解决方法:

在命令行使用"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"替换”%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe“

或者从别的开发机上复制一份WebApplications

参考文章:

http://blogs.msdn.com/b/visualstudio/archive/2013/07/24/msbuild-is-now-part-of-visual-studio.aspx

 

 

MSBuild学习资料:

http://technet.microsoft.com/zh-cn/library/0k6kkbsd