change output directory of nuget package

Viewed 12637

I created a NuGet package from my project. The output directory of the package is the solution directory. I would like to output it to a specific directory. I tried a target in the csproj file and in the nuspec file. None worked. How do I get the package generated in the specified folder?

In my .csproj:

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

In my .nuspec:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPackage.dll</id>
    <version>1.0.0</version>
    <authors>me</authors>
    <owners>me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <files>
      <file src="bin\MyPackage.dll" target="C:\LocalPackageRepository" />
    </files>
  </metadata>
</package>
3 Answers

Not sure why the global config settings didn't work for me, but adding below solution solved my problem:

  • Create an environment variable under user variables:
Variable name: MyNugetsOutput
Variable value: D:\myteam\teampackages
  • Then add below settings to .csproj file:
<Target Name="CopyPackage" AfterTargets="Pack">
  <Copy SourceFiles="$(OutputPath)\$(PackageId).$(PackageVersion).nupkg"
        DestinationFolder="$(MyNugetsOutput)\$(PackageId).$(PackageVersion).nupkg" />
</Target>

reference: Target build order

Update

Currently I'm using below code, it is simpler but it copies all .nupkg files to the "Local" path

<Target Name="AfterPack" AfterTargets="Pack">
    <Exec Command="dotnet nuget push $(PackageOutputPath)*.nupkg --source Local" />
</Target>
Related