Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change output directory of nuget package

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>
like image 797
Sabrina Avatar asked Mar 09 '16 13:03

Sabrina


People also ask

How do I change the NuGet package location?

Change NuGet package location folder Depending on what sort of project you are using this setting may or may not be successfully to change NuGet packager folder. If you are using a.NET Framework project that has a packages.config file then this setting will change the nuget package folder to C:projects.

How do I create a NuGet package from a project file?

Creates a NuGet package based on the specified .nuspec or project file. The dotnet pack command (see dotnet Commands) and msbuild -t:pack (see MSBuild targets) may be used as alternates. Use dotnet pack or msbuild -t:pack for PackageReference based projects. Under Mono, creating a package from a project file is not supported.

How to change the NuGet packager folder in DotNet restore?

To change the nuget packager folder, you can you can set "NUGET_PACKAGES" environment variable. Just Set "NUGET_PACKAGES" = "c: eampackages". Or you can place a NuGet.Config file next to the solution with the following content: Dotnet restore does not honour nuget.config 'repositoryPath'.

How do I change the output directory of a Visual Studio project?

Change the build output directory On the menu bar, choose Project > <Appname> Properties. If you have a Visual Basic project, select the Compile tab. In the configuration drop-down at the top, choose the configuration whose output file location you want to change (debug, release, or all).


2 Answers

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" />
like image 159
mnwsmit Avatar answered Sep 25 '22 05:09

mnwsmit


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>
like image 37
LazZiya Avatar answered Sep 24 '22 05:09

LazZiya