.net-coresdkmsbuildnugetpackagereference

How to run dotnet tool in prebuild phase of an sdk project


As part of my build process I want to run a dotnet tool before the compile.

I can add this section to my sdk project file:

  <ItemGroup>
    <PackageDownload Include="MyTool" Version="[1.0.1]" />
  </ItemGroup>

Then the tool is downloaded and is available inside:

  \Users\me\.nuget\packages\MyTool\1.0.1\tools\netcoreapp3.1\any\

I can then add a prebuild target like this:

  <Target Name="PreBuild" BeforeTargets="CoreCompile">
    <Exec Command="dotnet C:\Users\me\.nuget\packages\MyTool\1.0.1\tools\netcoreapp3.1\any\MyTool.dll <MyOptions> />
  </Target>

This works, but obviously I do not want absolute references to my user profile (or version) in the path.

Is there a way to substitute path with an environment variable?

I have tried adding GeneratePathProperty="true" to the PackageDownload but $(PkgMyTool) is undefined.

I also tried referencing the tool with <PackageReference> but this fails due to SDK incompatibility. My Tool is netcore3.1 and this project is netstandard2.0.


Solution

  • As you learned, PackageDownload doesn’t yet support GeneratePathProperty. Here are a couple of workarounds you could try, though:

    All three of these have worked for me in the past.