.NET 部署-03Web Deployment项目-05自定义Web Deployment项目
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>
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的.NET 部署-03Web Deployment项目-05自定义Web Deployment项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web Control 开发系列(三)
- 下一篇: 详解Android实现全屏正确方法