开发者

MSBuild: convert relative path in imported project to absolute path

开发者 https://www.devze.com 2023-01-03 06:57 出处:网络
Short version: I have an MSBuild project that imports another project. There is a property holding a relative path in the imported project that is relative to the location of the imported project. Ho

Short version:

I have an MSBuild project that imports another project. There is a property holding a relative path in the imported project that is relative to the location of the imported project. How do I convert this relative path to be absolute? I've tried the ConvertToAbsolutePath task, but this makes it relative to the importing project's location).

Long version:

I'm trying out Robert Koritnik's MSBuild task for integrating nunit output into Visual Studio (see this other SO question for a link). Since I like to have all my tools under version control, I want the target file with the custom task in it to point to the nunit console application using a relative path.

My problem is that this relative path ends up being made relative to the importing project.

E.g. (in ... MyRepository\Third Party\NUnit\MSBuild.NUnit.Task.Source\bin\Release\MSBuild.NUnit.Task.Targets):

...
  <PropertyGroup Condition="'$(NUnitConsoleToolPath)' == ''">
    <NUnitConsoleToolPath>..\..\..\NUnit 2.5.5\bin\net-2.0</>
  </PropertyGroup>  
...
  <Target Name="IntegratedTest">
    <NUnitIntegrated
      TreatFailedTestsAsErrors="$(NUnitTreatF开发者_JAVA百科ailedTestsAsErrors)"
      AssemblyName="$(AssemblyName)"
      OutputPath="$(OutputPath)"
      ConsoleToolPath="$(NUnitConsoleToolPath)"
      ConsoleTool="$(NUnitConsoleTool)"
    />
  </Target>
...

The above target fails with the error that the file cannot be found (that is the nunit-console.exe file). Inside the NUnitIntegrated MSBuild task, when the the execute() method is called, the current directory is the directory of the importing project, so relative paths will point to the wrong location.

I tried to convert the relative path to absolute by adding these tasks to the IntegratedTest target:

<ConvertToAbsolutePath Paths="$(NUnitConsoleToolPath)">
  <Output TaskParameter="AbsolutePaths" PropertyName="AbsoluteNUnitConsoleToolPath"/>
</ConvertToAbsolutePath>

but this just converted it to be relative to the directory of the project file that imports this target file.

I know I can use the property $(MSBuildProjectDirectory) to get the directory of the importing project, but can't find any equivalent for directory of the imported target file.

Can anyone tell me how a path in an imported file that is supposed to be relative to the directory that the imported file is in can be made absolute?

Thanks!


You'll have to define your relative path with MSBuildProjectDirectory

Importing project :

<PropertyGroup>
  <RelativePathToProject>subdir\</RelativePathToProject>
</PropertyGroup>

<Import Project="$(RelativePathToProject)SubSample.proj"/>

<Target Name="GetNUnitConsolePath">
  <Message Text="NUnitConsoleToolPath : %(NUnitConsoleToolPath.FullPath)"/>
</Target>

Imported project file :

<ItemGroup>
  <NUnitConsoleToolPath 
     Include="$(MSBuildProjectDirectory)\$(RelativePathToProject)..\..\..\NUnit 2.5.5\bin\net-2.0"/>
</ItemGroup>


You could try $([System.IO.Path]::GetFullPath('RelativePath')) to convert your relative path to an absolute path. Note, however, that this will not work on all types of project files. So, for example:

$([System.IO.Path]::GetFullPath('..\..\..\NUnit 2.5.5\bin\net-2.0'))
0

精彩评论

暂无评论...
验证码 换一张
取 消