我遇到一堆绝对路径的情况,我想将它们转换为基于MSBuild的另一个目录的相对路径。这是我到目前为止的代码:
<PropertyGroup>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="@(Foo)" Exclude="@(Baz)" />
<PathsRelativeToBar Include="@(AbsolutePaths->'???')" /> <!-- What goes here? -->
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激,谢谢!
编辑:我在这个 StackOverflow问题中找到了一个基于C#的解决方案,但是我不确定如何(或是否有可能)将其转换为MSBuild。
MSBuild中有一个本地函数,称为“ MakeRelative”
这是如何使用它。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.5" >
<PropertyGroup>
<Bar>c:\temp\msbuild-sample\2</Bar>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="c:\temp\msbuild-sample\1\**\*.txt" />
</ItemGroup>
<Target Name="Build">
<ItemGroup>
<PathsRelativeToBar Include="@(AbsolutePaths)">
<!-- Here is the magic... we're adding a piece of metadata with the relative path -->
<RelativePath>$([MSBuild]::MakeRelative($(FromPath), %(AbsolutePaths.FullPath)))</RelativePath>
</PathsRelativeToBar>
</ItemGroup>
<Message Text="----- Absolute paths -----" />
<Message Text="%(AbsolutePaths.FullPath)" />
<Message Text="----- Relative paths (showing full path) -----" />
<Message Text="%(PathsRelativeToBar.FullPath)" />
<Message Text="----- Relative paths (relative to $(FromPath)) -----" />
<Message Text="%(PathsRelativeToBar.RelativePath)" />
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)
这是我当前环境的快速浏览
C:\temp\msbuild-sample>dir /s /b
C:\temp\msbuild-sample\1
C:\temp\msbuild-sample\sample.build
C:\temp\msbuild-sample\1\1.1
C:\temp\msbuild-sample\1\1.txt
C:\temp\msbuild-sample\1\2.txt
C:\temp\msbuild-sample\1\1.1\3.txt
C:\temp\msbuild-sample\1\1.1\4.txt
Run Code Online (Sandbox Code Playgroud)
这是输出。
----- Absolute paths -----
c:\temp\msbuild-sample\1\1.1\3.txt
c:\temp\msbuild-sample\1\1.1\4.txt
c:\temp\msbuild-sample\1\1.txt
c:\temp\msbuild-sample\1\2.txt
----- Relative paths (showing full path) -----
c:\temp\msbuild-sample\1\1.1\3.txt
c:\temp\msbuild-sample\1\1.1\4.txt
c:\temp\msbuild-sample\1\1.txt
c:\temp\msbuild-sample\1\2.txt
----- Relative paths (relative to c:\temp\msbuild-sample\2) -----
..\1\1.1\3.txt
..\1\1.1\4.txt
..\1\1.txt
..\1\2.txt
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :)
| 归档时间: |
|
| 查看次数: |
1275 次 |
| 最近记录: |