给定相对路径时,如何使MSBUILD评估并打印完整路径? [英] How can I get MSBUILD to evaluate and print the full path when given a relative path?
问题描述
如何让MSBuild评估并在<Message />
任务中打印给定相对路径的绝对路径?
How can I get MSBuild to evaluate and print in a <Message />
task an absolute path given a relative path?
属性组
<Source_Dir>..\..\..\Public\Server\</Source_Dir>
<Program_Dir>c:\Program Files (x86)\Program\</Program_Dir>
任务
<Message Importance="low" Text="Copying '$(Source_Dir.FullPath)' to '$(Program_Dir)'" />
输出
将''复制到'c:\ Program Files(x86)\ Program \'
Copying '' to 'c:\Program Files (x86)\Program\'
推荐答案
在MSBuild 4.0中,最简单的方法如下:
In MSBuild 4.0, the easiest way is the following:
$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\your\path'))
即使将脚本<Import>
绑定到另一个脚本中,该方法也有效;该路径是相对于包含上述代码的文件的.
This method works even if the script is <Import>
ed into another script; the path is relative to the file containing the above code.
(从 Aaron的答案以及 在MSBuild 3.5中,您可以使用 ConvertToAbsolutePath 任务:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Test"
ToolsVersion="3.5">
<PropertyGroup>
<Source_Dir>..\..\..\Public\Server\</Source_Dir>
<Program_Dir>c:\Program Files (x86)\Program\</Program_Dir>
</PropertyGroup>
<Target Name="Test">
<ConvertToAbsolutePath Paths="$(Source_Dir)">
<Output TaskParameter="AbsolutePaths" PropertyName="Source_Dir_Abs"/>
</ConvertToAbsolutePath>
<Message Text='Copying "$(Source_Dir_Abs)" to "$(Program_Dir)".' />
</Target>
</Project>
相关输出:
Project "P:\software\perforce1\main\XxxxxxXxxx\Xxxxx.proj" on node 0 (default targets).
Copying "P:\software\Public\Server\" to "c:\Program Files (x86)\Program\".
如果您问我,那会有点long,但是可以.这将是相对于原始"项目文件的,因此,如果放置在被<Import>
ed的文件中,则不会相对于该文件.
A little long-winded if you ask me, but it works. This will be relative to the "original" project file, so if placed inside a file that gets <Import>
ed, this won't be relative to that file.
在MSBuild 2.0中,有一种方法无法解析"..".但是,它的行为就像绝对路径一样:
In MSBuild 2.0, there is an approach which doesn't resolve "..". It does however behave just like an absolute path:
<PropertyGroup>
<Source_Dir_Abs>$(MSBuildProjectDirectory)\$(Source_Dir)</Source_Dir_Abs>
</PropertyGroup>
$(MSBuildProjectDirectory)保留属性始终是包含此引用的脚本的目录.
The $(MSBuildProjectDirectory) reserved property is always the directory of the script that contains this reference.
这也将相对于原始"项目文件,因此,如果将其放置在已获得<Import>
ed的文件中,则不会相对于该文件.
This will also be relative to the "original" project file, so if placed inside a file that gets <Import>
ed, this won't be relative to that file.
这篇关于给定相对路径时,如何使MSBUILD评估并打印完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!