msbuildnugetmsbuild-target

Build target tools path


I am currently packaging a nuget package for my code generator project and I have gotten so far as to include an executable into the tools directory and a build target into the process.

Partial from the nuspec

<files>
    <file src="cgbr.targets" target="build\cgbr.targets" />
    <file src="cgbr.json" target="content\cgbr.json" />
    <file src="..\bin\CGbR.Lib.dll" target="lib\CGbR.Lib.dll" />
    <file src="..\bin\cgbr.exe" target="tools\cgbr.exe" />
</files>

Content of the cgbr.targets file

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="BeforeBuild">
        <Exec Command="cgbr.exe $(ProjectDir)"/>
    </Target>
</Project>

Now when I install the package I see that it is included into the build process. Unfortunately the path to cgbr.exe is invalid and I am a little stuck. Of course I could use $(SolutionDir)packages\CGbR.0.3\tools\cgbr.exe but than I would have to modify it every time I change the version.

To clarify: I need the path to my packages tools path.

Edit: Found a related post


Solution

  • You probably want a relative path to the tool from the targets file. There are a number of predefined properties in msbuild. Perhaps the most useful for these scenarios is MSBuildThisFileDirectory which returns the full path to the directory of the current proj file. An example:

    <Exec Command="&quot;$(MSBuildThisFileDirectory)..\tools\cgbr.exe&quot; &quot;$(ProjectDir)&quot;"/>