Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet pack and msbuild - how can pack reference assemblies in another location?

Tags:

nuget

msbuild

I am using NuGet pack for the first time, using the AfterBuild target in the .csproj file.

<Target Name="AfterBuild">
  <!-- package with NuGet -->
  <Exec WorkingDirectory="$(BaseDir)" Command="$(NuGetExePath) pack -Verbose -OutputDirectory $(OutDir) -Symbols -Prop Configuration=$(Configuration)" />
</Target>

This works fine when building the project itself with msbuild ("msbuild MyProj.csproj"). NuGet is able to find the compiled assemblies in projectdir/bin/Release or projectdir/bin/Debug.

But, this project is one of many in a solution and there is a build file dedicated to building the entire solution. The directory tree is like this:

- project root
  - build
  - src
    - MyProj
      - MyProj.csproj
      - MyProj.nuspec
    - AnotherProj
      - AnotherProj.csproj
      - AnotherProj.nuspec
  - project.proj (msbuild file)

This msbuild file overrides the output path of the Visual Studio build.

<PropertyGroup>
  <CodeFolder>$(MSBuildProjectDirectory)\src</CodeFolder>
  <CodeOutputFolder>$(MSBuildProjectDirectory)\build\$(Configuration)</CodeOutputFolder>
</PropertyGroup>

<Target Name="Build" DependsOnTargets="CleanSolution">
  <Message Text="============= Building Solution =============" />
  <Message Text="$(OutputPath)" />
  <Message Text="$(BuildTargets)" />
  <MsBuild Projects="$(CodeFolder)\$(SolutionName)" 
           Targets="$(BuildTargets)"
                 Properties="Configuration=$(Configuration);RunCodeAnalysis=$(RunCodeAnalysis);OutDir=$(OutputPath);" />
</Target>

Now that the build redirects the assemblies to the build directory, when I run pack, NuGet can't find them. How do I get NuGet to find the assemblies in the build directory?

like image 532
RyanW Avatar asked Sep 13 '12 20:09

RyanW


People also ask

Where are NuGet packages references stored?

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

How do I add a reference to a NuGet package?

After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

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.


2 Answers

Giving NuGet a TargetPath property for where to find the assembly works for this.

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' ">
  <!-- package with NuGet -->
  <Exec WorkingDirectory="$(BaseDir)" Command="$(NuGetExePath) pack -OutputDirectory $(OutDir) -Symbols -Prop Configuration=$(Configuration);TargetPath=$(OutDir)$(AssemblyName)$(TargetExt)" />
</Target>
like image 161
RyanW Avatar answered Sep 19 '22 03:09

RyanW


Try to define files section in your *.nuspec files, set copy Copy to Output Directory property to Copy always or Copy if newer for them in VS properties. After that all you compiled files and nuspecs will be in $(OutputPath). And then change AfterBuild to:

<Target Name="AfterBuild">
  <PropertyGroup>
    <NuspecPath>$([System.IO.Path]::Combine($(OutputPath), "$(ProjectName).nuspec"))</NuspecPath>
  </PropertyGroup>
  <!-- package with NuGet -->
  <Exec WorkingDirectory="$(BaseDir)" Command="$(NuGetExePath) pack $(NuspecPath) -Verbose -OutputDirectory $(OutDir) -Symbols -Prop Configuration=$(Configuration)" />
</Target>
like image 29
Pavel Bakshy Avatar answered Sep 22 '22 03:09

Pavel Bakshy