本文介绍了MSBuild 何时设置 $(ProjectName) 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MSBuild 的新手,我已经对我在 Visual Studio 2010 和 TFS 2010 中构建的 WPF 项目文件进行了一些自定义.我自定义了输出路径如下:

I'm fairly new to MSBuild, and I've done some customization on a WPF project file that I'm building both in Visual Studio 2010 and TFS 2010. I've customized the output path as follows:

<OutputPath Condition=" '$(TeamBuildOutDir)' == '' ">$(SolutionDir)buildinaries$(ProjectName)$(Configuration)$(Platform)</OutputPath>
<OutputPath Condition=" '$(TeamBuildOutDir)' != '' ">$(TeamBuildOutDir)binaries$(ProjectName)$(Configuration)$(Platform)</OutputPath>

这允许我在桌面上构建时构建到一个集中的二进制文件目录,并允许 TFS 在 CI 构建运行时找到二进制文件.

This allows me to build to a centralized binaries directory when building on the desktop, and allows TFS to find the binaries when CI builds are running.

但是,似乎在这两种情况下,$(ProjectDir) 属性在构建时都评估为 '',这会产生奇怪的结果.进行一些调试时,似乎 $(ProjectName) 是在 BeforeBuild 执行时设置的,但我的 OutputPath 属性在此之前正在评估它.

However, it seems that in both cases, the $(ProjectDir) property is evaluating to '' at build time, which creates strange results. Doing some debugging, it appears as if $(ProjectName) is set by the time BeforeBuild executes, but that my OutputPath property is evaluating it prior to that point.

<ProjectNameUsedTooEarly Condition=" '$(ProjectName)' == '' ">true</ProjectNameUsedTooEarly>

前面的属性与我的 OutputPath 属性位于同一属性组中.在 BeforeBuild 目标中,$(ProjectNameUsedTooEarly) 的计算结果为 true,但此时 $(ProjectName) 的计算结果正常为项目名称.

The preceding property is in the same property group as my OutputPath property. In the BeforeBuild target, $(ProjectNameUsedTooEarly) evaluates to true, but $(ProjectName) evaluates to the project name as normal by that point.

我可以做些什么来确保 $(ProjectName) 在我使用它时有一个值?

What can I do to ensure that $(ProjectName) has got a value when I use it?

我刚刚使用 Attrice 的 MSBuild Sidekick 来调试我的构建文件,并且在第一个可用于断点的目标 (_CheckForInvalidConfigurationAndPlatform) 中,所有属性似乎都已设置.ProjectName 已正确设置,但我的 OutputPath 属性已使用 ProjectName 的空白值设置.

I just used Attrice's MSBuild Sidekick to debug through my build file, and in the very first target available for breakpoint (_CheckForInvalidConfigurationAndPlatform) all the properties seem to be set already. ProjectName is already set correctly, but my OutputPath property has already been set using the blank value of ProjectName.

推荐答案

嗯 - 有点混乱,我会尝试解决

Hmm - bit of confusion going on there which I'll try to sort out

  1. 不要使用 $(ProjectDir) - 使用 $(MSBuildProjectDir) - 这是您的 csproj 在源代码树中的位置,由 MSBuild.exe 设置为保留属性.我认为 $(ProjectDir) 直到 Microsoft.Common.Targets 被导入(由 Microsoft.Csharp.targets 完成)之后才可用.属性评估总是在文件中就地"进行,而不是在所有导入完成后进行.这可以解释为什么您在 SideKick 工具中看到该属性是有效的

  1. Don't use $(ProjectDir) - use $(MSBuildProjectDir) - that's the location of your csproj in the source tree and is set by MSBuild.exe as a reserved property. I don't think $(ProjectDir) is available until after Microsoft.Common.Targets has been imported (which is done by Microsoft.Csharp.targets). Property evaluation is always carried out "in-place" within the file, and not when all the Imports have completed. This may explain why you are seeing the property as valid in the SideKick tool

同样使用 $(MSBuildProjectName) (我认为这会解决您的问题)

Likewise use $(MSBuildProjectName) (which I think will address your problem)

我不确定 VS2010 和 TFS2010(因为它使用 MSBuild 4.0,毫无疑问是新的 TeamBuild),但在 2008 年,在 .csproj 中很难确定你的构建是否是从命令调用的行/IDE 构建或从 TeamBuild 内部构建.我想说的是,我认为您的 csproj 中没有 $(TeamBuildOutDir) 可用.我通常会测试 $(TeamBuildConstants) 属性,因为当 teambuild 调用您的 proj 文件时,该属性 is 会传递下来.YMMV,因为我还没有玩过 2010..

I'm unsure about VS2010 and TFS2010 (as that uses MSBuild 4.0 and no doubt a new TeamBuild), but in 2008, it's pretty hard within a .csproj to figure out if your build was called from a command line/IDE build or from within TeamBuild. What I'm trying to say is that I don't think $(TeamBuildOutDir) is available within your csproj. I normally test $(TeamBuildConstants) property, as that property is passed down when teambuild calls your proj file. YMMV as I haven't played with 2010 yet..

这篇关于MSBuild 何时设置 $(ProjectName) 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:43