MSBuild - 使用不同的参数多次调用目标

时间:2013-10-14 10:52:14

标签: msbuild target

我正在尝试通过MSBuild将web.configs转换为构建过程的一部分,这很好;但是在同一个解决方案中处理多个web.configs会导致问题。

我们目前使用的代码提取web.config特定信息并将其传递给转换目标,这两个操作都捆绑在DependsOnTargets目标中:

<Target Name="ExtractWebConfigParams_1">
  <!-- Get webConfig1 info -->
</Target>

<Target Name="TransformWebConfig_1">
  <TransformXml Source="%(webConfig1).Web.config"
                Transform="%(webConfig1).Web.Stage.config"
                Destination="%(webConfig1).Web.config"
                StackTrace="$(StackTraceEnabled)" />
</Target>

<Target Name="ExtractWebConfigParams_2">
  <!-- Get webConfig2 info -->
</Target>

<Target Name="TransformWebConfig_2">
  <TransformXml Source="%(webConfig2).Web.config"
                Transform="%(webConfig2).Web.Stage.config"
                Destination="(webConfig2).Web.config"
                StackTrace="$(StackTraceEnabled)" />
</Target>

<Target
    Name="Transform_1"
    DependsOnTargets="ExtractWebConfigParams_1;                                                                                                     
                      TransformWebConfig_1;">
</Target>   

<Target
    Name="Transform_2"
    DependsOnTargets="ExtractWebConfigParams_2;                                                                                                     
                      TransformWebConfig_2;">
</Target>   

我们的解决方案最多可包含5个不同的web.config,因此每个都必须有一个提取,转换和DependsOnTargets目标。

我看不到使用多个提取目标的方法但是有没有人知道是否有办法用不同的参数调用转换目标而不是每次都制作一个全新的目标?

1 个答案:

答案 0 :(得分:3)

您可以将单独的.msbuild(.proj)文件写为“可重用逻辑”。

我有一个“拉链网站”常用逻辑我将在下面发布。 我的例子是关于压缩asp.net网站,但是封装了关于忽略哪些文件的规则(例如.csproj).......并且还有一些忽略某些文件的“钩子”。就像“images”目录一样,我们的目录非常庞大,所以我不想每次都把它压缩起来。

我的例子与你的需求没有直接关系。它的想法很重要。 将所有逻辑封装到一个文件中,并将参数传递给它。

我将.proj文件包含在Main.proj文件中。然后将参数传递给它。

ONE CAVEAT。如果相对目录位于与Main.proj文件相同的目录之外的任何位置,则它们不能在子.proj文件中工作。
Ala,您不能将目录属性设置为“。\ bin \”之类的东西,您必须在调用子proj文件并传递完整文件夹名称之前找出完整路径。这个例子是“c:\ myfolder \ mysolution \ myproject1 \ bin”......也就是f,无论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

然而,我发现常常的“条件检查”很烦人,这就是为什么我选择了上面描述的“by file”方法。

我要在这里复制/粘贴他的例子,以防他的博客发生故障。 还记得“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>