代码之家  ›  专栏  ›  技术社区  ›  Christopher Karper

ItemGroup项范围,或者“为什么MSBuild讨厌我?”

  •  10
  • Christopher Karper  · 技术社区  · 17 年前

    我有一个基于TFS的解决方案。我想更新所有适当文件的版本,我一直在努力完成这项工作。有很多关于如何做的链接,但没有一个适合我,因为有一个小问题。。。范围

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
        <Target Name="DesktopBuild">
            <CallTarget Targets="GetFiles"  />
    
            <Message Text="CSFiles: '@(CSFiles)'" />
        </Target>
    
        <Target Name="GetFiles">
            <ItemGroup>
                <CSFiles Include="**\AssemblyInfo.cs" />
            </ItemGroup>
            <Message Text="CSFiles: '@(CSFiles)'" />
        </Target>
    </Project>
    

    我的树看起来像这样:

    • 测试项目
    • 应用程序(文件夹)
      • main.cs
      • 属性(文件夹)
        • AssemblyInfo.cs

    Microsoft (R) Build Engine Version 3.5.30729.1
    [Microsoft .NET Framework, Version 2.0.50727.3074]
    Copyright (C) Microsoft Corporation 2007. All rights reserved.
    
    Build started 7/6/2009 3:54:10 PM.
    Project "D:\src\test.proj" on node 0 (default targets).
      CSFiles: 'application\Properties\AssemblyInfo.cs'
    DesktopBuild:
      CSFiles: ''
    Done Building Project "D:\src\test.proj" (default targets).
    
    
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:00.04
    

    那么,如何使我的ItemGroup具有全局范围?编译器和TeamBuild使用的所有目标文件都做同样的事情,它们的目标文件似乎都是全局的。。。我不明白为什么这对我不起作用。

    有什么帮助吗?

    4 回复  |  直到 17 年前
        1
  •  9
  •   technophile    17 年前

    您是否尝试过使用DependsOnTarget而不是CallTarget?可能是CallTarget导致了范围问题。

        2
  •  5
  •   Sayed Ibrahim Hashimi    17 年前

    bug 不是范围界定。这个 way to avoid

    赛义德·易卜拉欣·哈希米

    我的书: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build

        3
  •  1
  •   Guillaume Rouchon    16 年前

    http://blog.qetza.net/2009/10/23/scope-of-properties-and-item-in-an-msbuild-script/

    问题是项目似乎有一个全局范围,目标似乎有一个局部范围。进入目标时,复制全局范围;退出目标时,合并回本地范围。因此,CallTarget不会获得修改后的本地作用域值,但DependsOnTargets会,因为第一个目标在进入第二个目标之前退出。

        4
  •  0
  •   David Silva Smith    17 年前

    在我们的TFSBuild.proj中,如果未提供任何版本,我们将版本设置为0.0.0.0:

    <!--Our assembly version. Pass it in from the command prompt like this: /property:Version=1.0.0.0-->
    <PropertyGroup>
        <Version>0.0.0.0</Version>
    </PropertyGroup>
    
    <PropertyGroup>
        <!--Used to ensure there is a newline before our text to not break the .cs files if there is no newline at the end of the file.-->
        <newLine>%0D%0A</newLine>
    

    <Target Name="BeforeCompile">
        <!--Update our assembly version. Pass it in from the command prompt like this: /property:Version=1.0.0.0-->
    
        <!--attrib needs to be run first to remove the read only attribute since files from tfs are read only by default.-->
        <Exec Command='attrib -R $(SolutionRoot)\Source\Project\GlobalAssemblyInfo.cs'  />
    
        <WriteLinesToFile File="$(SolutionRoot)\Source\Project\GlobalAssemblyInfo.cs"
                          Lines='$(newLine)[assembly: AssemblyVersion("$(Version)")]'/>
    
    </Target>