繁体   English   中英

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

[英]ItemGroup Item scope, alternatively “Why does MSBuild hate me?”

我有一个解决方案,我想在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>

我的树看起来像这样:

  • test.proj
  • application.sln
  • 应用(文件夹)
    • main.cs
    • 属性(文件夹)
      • AssemblyInfo.cs中

当我从解决方案文件夹运行“c:\\ Windows \\ Microsoft.NET \\ Framework \\ v3.5 \\ MSBuild.exe test.proj”时...我得到以下输出:

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使用的所有Targets文件都做同样的事情,而且它们似乎都是全局的...我不明白为什么这对我不起作用。

有帮助吗?

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

前一个评论者是正确的,您应该更改此选项以使用DependsOnTargets而不是使用CallTarget任务。 你所看到的是一个bug,而不是一个范围问题。 避免这个错误的方法是使用DependsOnTargets(这是一个更好的方法)。

Sayed Ibrahim Hashimi

我的书: Microsoft Build Engine内部:使用MSBuild和Team Foundation Build

如上所述,您应该使用DependsOnTargets。 我已经对MSBuild范围做了一些研究,你可以在我的博客上找到我的结果: http//blog.qetza.net/2009/10/23/scope-of-properties-and-item-in-an-msbuild -脚本/

事情似乎是项目的全球范围和目标的本地范围。 输入目标时,将复制全局范围,退出目标时,将合并本地范围。 因此,CallTarget不会获得修改后的本地范围值,但是DependsOnTargets将在进入第二个目标之前退出第一个目标。

我们在构建中做了类似的事情。 我们将该版本作为命令行参数传递。

在我们的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM