您可以编写一个单独的 .msbuild (.proj) 文件作为“可重用逻辑”。
我有一个“压缩网站”的常见逻辑,我将在下面发布。我的示例是关于压缩一个 asp.net 网站,但封装了有关要忽略哪些文件的规则(例如 .csproj)......并且还有一些用于忽略某些文件的“钩子”。就像“图像”目录一样,我们的目录很大,所以我不想每次都把它压缩起来。
我的示例与您的需求没有直接关系。它的想法很重要。将所有逻辑封装到一个文件中,并将参数传递给它。
我将 .proj 文件包含在 Main.proj 文件中。然后传递参数给它。
一个警告。如果子 .proj 文件位于与 Main.proj 文件相同的目录之外的任何位置,则相对目录在子 .proj 文件中不起作用。
唉,您不能将目录属性设置为“.\bin\”之类的东西,您必须在调用子项目文件并传递完整文件夹名称之前找出完整路径。这个例子是 "c:\myfolder\mysolution\myproject1\bin" ... aka,不管 f
放入“外部” Main.proj 文件的代码:
<Target Name="ZipItUpUsingCommonLogic">
<Message Text=" " />
<Message Text=" About to Call External MSBUILD File " />
<Message Text=" " />
<MSBuild Projects="..\..\CommonLogicMsBuildStuff\WebSiteZippingCommonLogic.proj" Targets="WebSiteZippingAllTargetsWrapper" Properties="WebSiteFolderFullPath=c:\workstuff\mywebsolution;OutputFolderFullPath=c:\workstuff\buildoutputs;WebSiteZipFileNameNonConfig=MyNonConfigFiles$(Configuration).zip;WebSiteZipFileNameConfigFiles=MyWebSiteConfigFiles$(Configuration).zip;RevisionNumber=333;IgnoreFolder1=c:\workstuff\mywebsolution\images" />
</Target>
名为“WebSiteZippingCommonLogic.proj”的文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="WebSiteZippingAllTargetsWrapper">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks" />
<!-- There was an issue with the xsl/document(path) function......this help address the issue. -->
<Target Name="WebSiteZippingAllTargetsWrapper">
<CallTarget Targets="ShowParameters" />
<CallTarget Targets="ValidateParameters" />
<CallTarget Targets="ZipTheWebSite" />
</Target>
<Target Name="ValidateParameters">
<Error Text="The WebSiteFolderFullPath property was not passed in correctly." Condition="'$(WebSiteFolderFullPath)' == ''" />
<Error Text="The OutputFolderFullPath property was not passed in correctly." Condition="'$(OutputFolderFullPath)' == ''" />
<Error Text="The WebSiteZipFileNameNonConfig property was not passed in correctly." Condition="'$(WebSiteZipFileNameNonConfig)' == ''" />
<Error Text="The WebSiteZipFileNameConfigFiles property was not passed in correctly." Condition="'$(WebSiteZipFileNameConfigFiles)' == ''" />
<!--<Error Text="The RevisionNumber property was not passed in correctly." Condition="'$(RevisionNumber)' == ''" />-->
</Target>
<Target Name="ShowParameters">
<Message Text=" WebSiteFolderFullPath = $(WebSiteFolderFullPath)" />
<Message Text=" OutputFolderFullPath = $(OutputFolderFullPath)" />
<Message Text=" WebSiteZipFileNameNonConfig = $(WebSiteZipFileNameNonConfig)" />
<Message Text=" WebSiteZipFileNameConfigFiles = $(WebSiteZipFileNameConfigFiles)" />
<Message Text=" IgnoreFolder1 = $(IgnoreFolder1)" />
<Message Text=" IgnoreFolder2 = $(IgnoreFolder2)" />
<Message Text=" IgnoreFolder3 = $(IgnoreFolder3)" />
<Message Text=" " />
<Message Text=" " />
</Target>
<Target Name="ZipTheWebSite" DependsOnTargets="ValidateParameters">
<ItemGroup>
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\**\*.sln" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\**\*.vbproj" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\**\*.csproj" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\**\*.config" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\.svn\**\*.*" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\obj\**\*.*" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)\**\.svn\**" />
<WebSiteExcludeFiles Include="$(WebSiteFolderFullPath)**\.svn\**\*.*" />
<WebSiteExcludeFiles Include="$(IgnoreFolder1)\**\*.*" Condition="'$(IgnoreFolder1)' != ''" />
<WebSiteExcludeFiles Include="$(IgnoreFolder2)\**\*.*" Condition="'$(IgnoreFolder2)' != ''" />
<WebSiteExcludeFiles Include="$(IgnoreFolder3)\**\*.*" Condition="'$(IgnoreFolder3)' != ''" />
</ItemGroup>
<ItemGroup>
<WebSiteNonConfigIncludeFiles Include="$(WebSiteFolderFullPath)\**\*.*" Exclude="@(WebSiteExcludeFiles)">
</WebSiteNonConfigIncludeFiles>
</ItemGroup>
<MSBuild.Community.Tasks.Zip Files="@(WebSiteNonConfigIncludeFiles)" ZipFileName="$(OutputFolderFullPath)\$(WebSiteZipFileNameNonConfig)" WorkingDirectory="$(WebSiteFolderFullPath)\" />
<ItemGroup>
<WebSiteConfigIncludeFiles Include="$(WebSiteFolderFullPath)\**\*.config">
</WebSiteConfigIncludeFiles>
</ItemGroup>
<MSBuild.Community.Tasks.Zip Files="@(WebSiteConfigIncludeFiles)" ZipFileName="$(OutputFolderFullPath)\$(WebSiteZipFileNameConfigFiles)" WorkingDirectory="$(WebSiteFolderFullPath)\" />
<Message Text=" " />
<Message Text=" " />
</Target>
</Project>
如果您不想将规则封装到单独的文件中,那么您可能正在寻找这个:
http://sstjean.blogspot.com/2006/09/how-to-get-msbuild-to-run-complete.html
但是,我发现不断的“条件检查”很烦人,这就是我使用上面描述的“按文件”方法的原因。
我将在此处复制/粘贴他的示例,以防万一他的博客出现故障。还记得“gotdotnet.com”吗?
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Package Include="CommonWebSetup.ism">
<PackagerType>IS</PackagerType>
<SetupProjFolder>CommonWebSetup</SetupProjFolder>
<ISProductConfig>Server</ISProductConfig>
<ISReleaseConfig>Release</ISReleaseConfig>
</Package>
<Package Include="CommonClientSetup.vdproj">
<PackagerType>VS</PackagerType>
<SetupProjFolder>CommonClientSetup</SetupProjFolder>
<ISProductConfig>Client</ISProductConfig>
<ISReleaseConfig>Release</ISReleaseConfig>
</Package>
</ItemGroup>
<Target Name="Test" Outputs="%(Package.Identity)" >
<Message Text="Removing read-only flag for %(Package.Identity)" Importance="High" />
<Message Text="Setting Environment variable for %(Package.Identity)" Importance="High" />
<Message Condition=" '%(Package.PackagerType)' == 'IS' "
Text="Running InstallShield for %(Package.Identity)" Importance="High" />
<Message Condition=" '%(Package.PackagerType)' == 'VS' "
Text="Running DevEnv.exe for %(Package.Identity)" Importance="High" />
</Target>