当给定相对路径时,如何让 MSBUILD 评估并打印完整路径?
如何让 MSBuild 在
任务中评估并打印给定相对路径的绝对路径?
属性组
<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\”
How can I get MSBuild to evaluate and print in a <Message />
task an absolute path given a relative path?
Property Group
<Source_Dir>..\..\..\Public\Server\</Source_Dir>
<Program_Dir>c:\Program Files (x86)\Program\</Program_Dir>
Task
<Message Importance="low" Text="Copying '$(Source_Dir.FullPath)' to '$(Program_Dir)'" />
Output
Copying '' to 'c:\Program Files (x86)\Program\'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您需要将属性转换为项目,您有两种选择。 在 msbuild 2 中,您可以使用 CreateItem 任务
,在 MSBuild 3.5 中,您可以在任务中包含 ItemGroups
If you need to convert Properties to Items you have two options. With msbuild 2, you can use the CreateItem task
and with MSBuild 3.5 you can have ItemGroups inside of a Task
在 MSBuild 4.0 中,最简单的方法如下:
即使脚本被
编辑到另一个脚本中,此方法仍然有效; 该路径是相对于包含上述代码的文件的。(综合Aaron的回答以及Sayed 的回答)
在 MSBuild 3.5 中,您可以使用 ConvertToAbsolutePath 任务:
相关输出:
如果你问我的话,有点啰嗦,但它有效。 这将相对于“原始”项目文件,因此如果放置在经过编辑的文件内,则这不会相对于该文件。
在 MSBuild 2.0 中,有一种方法无法解析“..”。 然而,它的行为就像绝对路径一样:
$( MSBuildProjectDirectory) 保留属性始终是包含此引用的脚本的目录。
这也将相对于“原始”项目文件,因此如果放置在经过编辑的文件内,则这不会相对于该文件。
In MSBuild 4.0, the easiest way is the following:
This method works even if the script is
<Import>
ed into another script; the path is relative to the file containing the above code.(consolidated from Aaron's answer as well as the last part of Sayed's answer)
In MSBuild 3.5, you can use the ConvertToAbsolutePath task:
Relevant output:
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.In MSBuild 2.0, there is an approach which doesn't resolve "..". It does however behave just like an absolute path:
The $(MSBuildProjectDirectory) reserved property is always the directory of the script that contains this reference.
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 4.0 添加了属性函数,允许您在某些情况下调用静态函数.net 系统 dll。 属性函数的一个非常好的事情是它们将在目标之外进行计算。
要评估完整路径,您可以使用 System.IO.Path .GetFullPath 在定义属性时,如下所示:
语法有点难看,但非常强大。
MSBuild 4.0 added Property Functions which allow you to call into static functions in some of the .net system dlls. A really nice thing about Property Functions is that they will evaluate out side of a target.
To evaluate a full path you can use System.IO.Path.GetFullPath when defining a property like so:
The syntax is a little ugly but very powerful.
韦恩是正确的,众所周知的元数据不适用于属性 - 仅适用于项目。 使用“MSBuildProjectDirectory”等属性将起作用,但我不知道解析完整路径的内置方法。
另一种选择是编写一个简单的自定义任务,该任务将采用相对路径并吐出完全解析的路径。 它看起来像这样:
你的 MSBuild 行看起来像这样:
Wayne is correct that well-known metadata does not apply to properties - only to items. Using properties such as "MSBuildProjectDirectory" will work, but I'm not aware of a built in way to resolve the full path.
Another option is to write a simple, custom task that will take a relative path and spit out the fully-resolved path. It would look something like this:
And your MSBuild lines would look something like:
您正在尝试通过属性访问项目元数据属性,但这是不可能的。 您想要做的是这样的:
它将生成输出为:(
该脚本是从我的文档文件夹运行的,所以 ..\Desktop 是到达我的桌面的正确相对路径。)
在您的情况下,替换“ Source_Dir 项中包含“..\Desktop”和“......\Public\Server”,您应该已全部设置完毕。
You are trying to access an item metadata property through a property, which isn't possible. What you want to do is something like this:
Which will generate output as:
(The script was run from my Documents folder, so ..\Desktop is the correct relative path to get to my desktop.)
In your case, replace the "..\Desktop" with "......\Public\Server" in the Source_Dir item and you should be all set.