Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get NuGet package folder in MSBuild

I want to call executable tools like NUnit which I manage via NuGet in MSBuild:

<Target Name="Test">
  <CreateItem Include="$(BuildCompileDirectory)\*.Tests.*dll">
    <Output TaskParameter="Include" ItemName="TestAssemblies" />
  </CreateItem>
  <NUnit
    Assemblies="@(TestAssemblies)"
    ToolPath="$(PackagesDirectory)\NUnit.2.5.10.11092\tools"
    WorkingDirectory="$(BuildCompileDirectory)"
    OutputXmlFile="$(BuildDirectory)\$(SolutionName).Tests.xml" />
</Target>

The problem is that the folder of a NuGet packages is containing the version number of the package. For instance nunit-console.exe is in the folder packages\NUnit.2.5.10.11092\tools. If I update the NUnit package this path will change and I have to update my MSBuild script. That isn't acceptable.

MSBuild doesn't allow Wildcards in directories, so this isn't working:

ToolPath="$(PackagesDirectory)\NUnit.*\tools"

How can I call tools in MSBuild without having to update my build script whenever I update a NuGet package?

like image 241
Martin Buberl Avatar asked Jul 29 '12 00:07

Martin Buberl


People also ask

Where is the NuGet Packages folder?

The location of the default global packages folder. The default is %userprofile%\. nuget\packages (Windows) or ~/. nuget/packages (Mac/Linux).

Does MSBuild restore NuGet packages?

Restore by using MSBuildYou can use msbuild -t:restore to restore packages in NuGet 4. x+ and MSBuild 15.1+, which are included with Visual Studio 2017 and higher. This command restores packages in projects that use PackageReference for package references.

Where can I find Nuspec file?

The current nuspec. xsd schema file can be found in the NuGet GitHub repository. All XML element names in the . nuspec file are case-sensitive, as is the case for XML in general.

How do I get files from a NuGet package?

on the toolbar of the Assembly Explorer window or choose File | Open from NuGet Packages Cache in the main menu . This will open the Open from NuGet Packages Cache dialog. The dialog lists packages from all NuGet cache locations on your machine. Use the search field in the dialog to find the desired package.


1 Answers

You can use MSBuild Transforms to get the relative directory of a specific tool:

<ItemGroup>
  <NunitPackage Include="$(PackagesDirectory)\NUnit.*\tools\nunit-console.exe"/>
</ItemGroup>

<Target Name="Test">
  <CreateItem Include="$(BuildCompileDirectory)\*.Tests.*dll">
    <Output TaskParameter="Include" ItemName="TestAssemblies" />
  </CreateItem>
  <NUnit
    Assemblies="@(TestAssemblies)"
    ToolPath="@(NunitPackage->'%(relativedir)')"
    WorkingDirectory="$(BuildCompileDirectory)"
    OutputXmlFile="$(BuildDirectory)\$(SolutionName).Tests.xml" />
</Target>
like image 106
Martin Buberl Avatar answered Oct 24 '22 19:10

Martin Buberl