visual-studionugetvisual-studio-2019nuget-spec

Restore nuget package in different folder


We have some projects (Plugins) which use in several projects, the output of these projects will copy to the specific folder in the target projects (Plugins folder). We pack project with Visual studio 2019 Pack option and after that, we push npkg files to our local NuGet server for further use.

The problem is when we want to get these packages, Package Manager should put lib files in the Plugins folder, but unfortunately, the package manager extracts these in the root folder (bin).

My question is: How can I config nuspec file to force package manager to extract to the right folder, and can I do it with visual studio or I have to create nuspec file manually.


Solution

  • You should use a <packages_id>.props file to realize it.

    1. create a file called <packages_id>.props under the build folder of your lib project.

      You should note that if your nupkg file is called Plugins.1.0.0.nupkg, you should name this file as Plugins.props so that it will work.

      enter image description here

    2. add these on Plugins.props file:

      <Project>
      
          <Target Name="CopyFiles" BeforeTargets="Build">
              <ItemGroup>
                  <File Include="$(MSBuildThisFileDirectory)..\Plugins\*.*"></File>
              </ItemGroup>
      
               <!--It will copy the plugins output files into the Plugins folder of the goal project-->
              <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)Plugins"></Copy>
      
          </Target>
      
      </Project>
      
    3. add these on Plugins.csproj file:

      <ItemGroup>
              <None Include="bin\Debug\netstandard2.0\Plugins.dll" Pack="true" PackagePath="Plugins"></None>
      
              //add any output files from Plugins project which you want them to be packed 
      
              <None Include="build\Plugins.props" Pack="true" PackagePath="build"></None>
          </ItemGroup>
      
    4. use Pack Button to create the new release version of your nuget pakckage.

    Also, when you install this new version of nuget package, please clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.

    When you finish the installing process, please click Build button and the files will generated under Plugins folder.

    There is also a similar issue about this.