6

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>
1
  • You cannot do this from the .nuspec file. You can create a NuGet.Config file and define a global packages directory for all solutions. This is done outside the .nupkg file and stored either under your profile or in a subdirectory which is a parent to all the directories containing your solutions. See the NuGet documentation on this feature. Commented Mar 9, 2016 at 15:11

4 Answers 4

4

In the 'old' way of NuGet (which you seem to use, check this for info on new vs old) this was possible by using the command in the .nuget\NuGet.targets file you mention. If you change the line with PackageOutputDir to below it will work.

<PackageOutputDir Condition="$(PackageOutputDir) == ''">C:\LocalPackageRepository</PackageOutputDir>

Even better would be to set a property on the PropertyGroup in the .csproj like this:

<PackageOutputDir>C:\LocalPackageRepository</PackageOutputDir>

In the new way of NuGet you would add this key to the NuGet.config file:

<add key="repositoryPath" value="C:\LocalPackageRepository" />
Sign up to request clarification or add additional context in comments.

3 Comments

The trouble with repositoryPath is that NuGet also expects dependency packages to be located there so it changes more than out the output directory.
PackageOutputDir works just fine in a nuget project made in .net 6 here in 2021. So wouldnt call it the "old" way.
I had to use PackageOutputPath in VS 2022 17.3
3

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>

2 Comments

Note that the output path of the nuget package and the output path of build are different. The build is in a directory with the targeted framework, the nuget package files are one directory above them. eg. Packages are in bin\Release\ the OutputPath is bin\Release\{targetframework}
@DouglasGaskell yes sure, and currently I'm using a simpler version as mentioned in the updated answer.
0

You cannot do this from the .nuspec file. You can create a NuGet.Config file and define a global packages directory for all solutions:

<configuration>
  <config>
    <add key="repositoryPath" value="C:\myteam\teampackages" />
  </config>
  ... 
</configuration>

This is done outside the .nupkg file and stored either under your profile or in a subdirectory which is a parent to all the directories containing your solutions. See the NuGet documentation for more details on this feature.

Comments

0

As an alternative, you can package one (or more) projects with msbuild/VS and then use NuGet add or NuGet init to copy/publish them to your local NuGet feed:

nuget add .\bin\debug\MyPackage.nupkg -source c:\LocalPackageRepository

NuGet init will only pull from the current directory, so I used something like this:

echo "Start"
$packages = Get-ChildItem "C:\source\repos\Projects\*.nupkg" -Recurse -Force
echo "packages to publish =" $packages
ForEach ($p in $packages)
{
    echo $p
    nuget add $p -Source C:\source\NuGetLocal
}
echo "End"

https://learn.microsoft.com/en-us/nuget/hosting-packages/local-feeds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.