MSBuild何时设置$(ProjectName)属性? [英] When does MSBuild set the $(ProjectName) property?

查看:193
本文介绍了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)build\binaries\$(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)属性在构建时都评估为",这会产生奇怪的结果.在进行调试时,似乎好像在执行BeforeBuild之前设置了$(ProjectName),但是我的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,但是已经使用ProjectName的空白值设置了我的OutputPath属性.

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内部构建.我要说的是,我认为$(TeamBuildOutDir)在您的csproj中不可用.我通常测试$(TeamBuildConstants)属性,因为当teambuild调用您的proj文件时,该属性 被传递了下来.我尚未参加2010年的YMMV.

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)属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆