简体   繁体   English

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

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

I have a solution I'm trying to get to build on TFS. 我有一个解决方案,我想在TFS上构建。 I want to update the versions of all appropriate files, and I've been stuck trying to get this done. 我想更新所有适当文件的版本,我一直在努力完成这项工作。 There are plenty of links on how to do it, but none of them work for me, due to one little issue... Scope. 关于如何做到这一点有很多链接,但由于一个小问题,它们都不适合我...范围。

<?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>

My tree looks like this: 我的树看起来像这样:

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

When I run "c:\\Windows\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe test.proj" from the solution folder... I get the following output: 当我从解决方案文件夹运行“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

So, how can I make my ItemGroup have global scope? 那么,我如何让我的ItemGroup具有全局范围? All the Targets files used by the compiler and TeamBuild do this same thing, and theirs all seem to be global... I don't understand why this isn't working for me. 编译器和TeamBuild使用的所有Targets文件都做同样的事情,而且它们似乎都是全局的...我不明白为什么这对我不起作用。

Any help? 有帮助吗?

Have you tried using DependsOnTarget rather than CallTarget? 您是否尝试过使用DependsOnTarget而不是CallTarget? It could be that CallTarget is causing the scope issue. 可能是CallTarget引起了范围问题。

The previous commenter was correct, you should change this to use DependsOnTargets instead of using the CallTarget task. 前一个评论者是正确的,您应该更改此选项以使用DependsOnTargets而不是使用CallTarget任务。 What you are seeing is a bug not a scoping inssue. 你所看到的是一个bug,而不是一个范围问题。 The way to avoid this bug is to use DependsOnTargets (which is a much better approach anywayz). 避免这个错误的方法是使用DependsOnTargets(这是一个更好的方法)。

Sayed Ibrahim Hashimi Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build 我的书: Microsoft Build Engine内部:使用MSBuild和Team Foundation Build

As said, you should use DependsOnTargets. 如上所述,您应该使用DependsOnTargets。 I've done some research on MSBuild scope, you can find my results on my blog : http://blog.qetza.net/2009/10/23/scope-of-properties-and-item-in-an-msbuild-script/ 我已经对MSBuild范围做了一些研究,你可以在我的博客上找到我的结果: http//blog.qetza.net/2009/10/23/scope-of-properties-and-item-in-an-msbuild -脚本/

The thing is there seems to be a global scope for the project and a local scope for the target . 事情似乎是项目的全球范围和目标的本地范围。 When entering the target, the global scope is copied and when exiting the target, the local scope is merged back. 输入目标时,将复制全局范围,退出目标时,将合并本地范围。 So a CallTarget will not get the modified local scope values but the DependsOnTargets will since the first target is exited before the entering the second target. 因此,CallTarget不会获得修改后的本地范围值,但是DependsOnTargets将在进入第二个目标之前退出第一个目标。

We do something similar in our build. 我们在构建中做了类似的事情。 We pass the version as a command line parameter. 我们将该版本作为命令行参数传递。

In our TFSBuild.proj we set the version to 0.0.0.0 if no version was supplied: 在我们的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>

Then we do this: 然后我们这样做:

<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.

相关问题 为什么MsBuild的ItemGroup-&gt; EndWith无法用于&#39;.exe&#39;? - Why is MsBuild's ItemGroup->EndWith not working for '.exe'? MSBuild ItemGroup 条件项目参考 - MSBuild ItemGroup Condition Project Reference 如何对带引号的文件的ItemGroup列表进行转义-MSBuild - How to escape an ItemGroup list of files with quotes - MSBuild 在MsBuild中,PropertyGroup和ItemGroup之间有什么区别 - In MsBuild, what's the difference between PropertyGroup and ItemGroup 从MSBuild XML获取ItemGroup集合及其项目 - Getting ItemGroup collection and their Items from MSBuild xml 当我尝试使用 OpenProjectAsync 打开项目时,为什么 msbuild 会给我一个“不支持语言“C#”错误 - Why does msbuild give me a "language "C#" not supported" error when I try to open a project with OpenProjectAsync C# - MSBuild 参考 - 复制到 CopyToOutputDirectory 的所有项目<itemgroup></itemgroup> - C# - MSBuild reference - Copy to CopyToOutputDirectory all items of <itemGroup> 为什么用于MSBuild的SonarQube扫描仪不能与MSBuild 15.0一起使用? - Why SonarQube Scanner for MSBuild does not work with MSBuild 15.0? 如何正确请求 Azure 不记名令牌或为什么要求我发送“范围”参数? - How to request an Azure bearer token properly or why does it ask me to send a “scope” parameter? 为什么我的MSBuild Exec命令失败? - Why does my MSBuild Exec Command Fail?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM