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

MSBuild-使用不同参数多次调用目标,msbuild,target,Msbuild,Target,我正在尝试通过MSBuild将web.configs转换为构建过程的一部分,这很好;但在同一解决方案中处理多个web.config会导致问题 我们目前使用的代码提取web.config特定信息并将其传递给转换目标,这两个操作都捆绑在DependsOnTargets目标中: <Target Name="ExtractWebConfigParams_1"> <!-- Get webConfig1 info --> </Target> <Target N

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

我们目前使用的代码提取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,因此每个web.config都必须有一个提取、转换和依赖目标


我看不到使用多个提取目标的方法,但有人知道是否有一种方法可以使用不同的参数调用转换目标,而不是每次都创建一个全新的目标?

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

我有一个“zip-up网站”的常见逻辑,我将在下面发布。 我的例子是关于压缩一个asp.net网站,但是封装了关于忽略哪些文件的规则(例如(.csproj)。就像“图片”目录一样,我们的目录很大,所以我不想每次都把它压缩起来

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

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

一个警告。如果sub.proj文件与Main.proj文件位于同一目录之外的任何位置,则相对目录在sub.proj文件中不起作用。
Ala,您不能将目录属性设置为“\bin\”之类的值,在调用子项目文件并传递完整文件夹名称之前,您必须先找出完整路径。这个例子是“c:\myfolder\mysolution\myproject1\bin”。。。阿卡,不管怎样

要放入“outside”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”的文件的代码:


如果您不想将规则封装到单独的文件中,那么您可能要查找以下内容:

但是,我发现持续的“条件检查”很烦人,这就是为什么我选择了上面描述的“按文件”方法

我将把他的例子复制/粘贴到这里,以防他的博客崩溃。 还记得“gotdotnet.com”吗


是
公共网站设置
服务器
释放
VS
公共客户端设置
客户
释放

感谢您的详细回复,但这不是我想要的。我们有一个类似的设置来压缩构建文件,它都包含在一个.msbuild文件中,这就是我需要做的。我想做的就是用不同的参数多次调用一个目标,在遇到MSBuild之前,我一直认为这是任何编程语言的基础…我的响应被追加。使用“标识”或编写单独的文件…就我所知,这些都像是您的选项。标识非常有效,我仍然认为应该有一种更简单的机制,在MSBuild中使用不同的参数调用目标;无论如何,感谢您的输入GranadCoder!
<?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>
<?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>