您的位置:首页 > 其它

.NET 部署-03Web Deployment项目-05自定义Web Deployment项目

2010-11-08 00:20 519 查看
Web Deployment项目是一些MSBuild项目文件。Web Deployment项目文件的扩展名为.wdproj。你用Web Deployment项目属性页设置的所有属性都会被保存在.wdproj文件,作为MSBuild属性或项。你可以看一下MSDN关于MSBuild资料。

通过编辑Web Deployment项目文件,你可以自定义生成过程。在解决方案浏览器(Solution Explorer)中,右键点击Web Deployment项目,然后选择“打开项目文件(Open Project File)”。

在这个文件,你会发现每个配置都有一个属性组,即<PropertyGroup>。如下所示:

<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>../WebSite2</SourceWebPhysicalPath>    <SourceWebProject>{49522426-B68E-43B3-A8C7-C8357BA2569D}|C:/MyCode/WebSite2</SourceWebProject>    <SourceWebVirtualPath>/WebSite2</SourceWebVirtualPath>    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>  </PropertyGroup>  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">    <DebugSymbols>true</DebugSymbols>    <OutputPath>./Debug</OutputPath>    <EnableUpdateable>true</EnableUpdateable>    <UseMerge>true</UseMerge>    <SingleAssemblyName>WebSite2_deploy</SingleAssemblyName>  </PropertyGroup>  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">    <DebugSymbols>false</DebugSymbols>    <OutputPath>./Release</OutputPath>    <EnableUpdateable>true</EnableUpdateable>    <UseMerge>true</UseMerge>    <SingleAssemblyName>WebSite2_deploy</SingleAssemblyName>  </PropertyGroup>  <ItemGroup>  </ItemGroup>  <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">  </Target>  --></Project>


在接近上面文件的底部,你会看到一个注释块,它包含四个MSBuild目标,即“BeforeBuild”、“BeforeMerge”、“AfterMerge”和“AfterBuild”:

你可以覆盖这些目标,添加自定义生成任务。例如,一个名为“Upload”,用来上传的文件夹,该文件夹必须存在,才能正常工作。为了保证这个文件夹在预编译Web站点中存在,你可以添加下面目标和任务:

<Target Name="AfterBuild"><MakeDir Directories="$(TargetDir)/Upload" /></Target>


MSBuild包含许多预定义的任务,例如“MakeDir”。MSBuild也提供创建自己的任务。你可以看一下MSDN关于“How To: Write a Task”资料。

除了覆盖目标和创建自定义任务,你可以向包含在Web Deployment项目中的MSBuild任务添加属性和项。例如,通过向Web Deployment项目添加“<ItemGroup>”节,你可以从生成过程中排除测试文件夹和图片文件夹。如下所示:

<ItemGroup><ExcludeFromBuild Include="$(SourceWebPhysicalPath)/Test/**/*.*"/><ExcludeFromBuild Include="$(SourceWebPhysicalPath)/Images/**/*.*"/></ItemGroup>


如果你在Web 站点项目中有测试代码,不应该包含在演示和发布生成中,那么就很有用了。

默认情况,ASP.NET 合并工具应用程序集(assembly)属性,从由编译器生成的App_Code程序集到合并的程序集。这些属性是定义在App_Code 文件夹中的AssemblyInfo.cs 和 AssemblyInfo.vb 文件。但是,你可以在Web Deployment项目中定义程序集的属性。如果程序集的属性在配置之间改变,那么这个就很有用。例如,当你在Web Deployment项目属性页定义程序集版本(Assembly Version)和文件版本(File Version),那么<ItemGroup>节本将被添加到Web Deployment项目文件中。如下所示:

<ItemGroup><AssemblyAttributes Include="AssemblyVersion"><value>3.0.0.0</value></AssebmlyAttributes><AssebmlyAttributes Include="AssemblyFileVersion"><value>3.0.0.0</value></AssebmlyAttributes></ItemGroup>


<ItemGroup>节也可以包含其他程序集的属性,比如,程序集标题,程序集描述,公司和版权等,这些属性会在合并的程序集中。如下所示,定义在<ItemGroup>的其他程序集属性。如下所示:

<ItemGroup></AssebmlyAttributes><AssemblyAttributes Include="AssemblyTitle"><value>MyCompany MyWeb</value></AssemblyAttributes><AssemblyAttributes Include="AssemblyDescription"><value>Corporate Site</value></AssemblyAttributes><AssemblyAttributes Include="AssemblyCompany"><value>MyCompany</value></AssemblyAttributes><AssemblyAttributes Include="AssemblyCopyright"><value>Copyright © MyCompany 2005</value></AssemblyAttributes></ItemGroup>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: