.net - Is it possible to have nuget delay download until build? - Stack Overflow

admin2025-04-19  1

I have a project that needs a reference to a nuget package, containing a number of dlls that it will use later.

When I install the nuget package, it instantly copies all the libraries into the correct location. However, the point of this exercise is to keep them out of the project's git repository, and for nuget to do the download while building.

Is this possible?

This is my nuspec:

<?xml version="1.0"?>
<package xmlns=".xsd">
  <metadata>
    <version>1.0.0</version>
    <authors>MyCompany Ltd</authors>
    <owners>MyCompany Ltd</owners>
    <id>MyCompany.ReferencedLibraries</id>
    <title>MyCompany.ReferencedLibraries</title>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Libraries referenced by my project</description>
    <copyright>Copyright MyCompany Ltd 2025</copyright>
    <dependencies />
  </metadata>
  <files>
    <file src="Package\**" target="content\Lib" />
  </files>
</package>

... all the files are in the Package directory and are to be copied into the project's Lib directory within the project's code base.

I have noticed that if I delete the files after installation, and build again, it doesn't restore the files automatically, so adding them to .gitignore won't help - even though I thought this was how nuget worked.

Could the difference be pointing the target the content folder?

I have a project that needs a reference to a nuget package, containing a number of dlls that it will use later.

When I install the nuget package, it instantly copies all the libraries into the correct location. However, the point of this exercise is to keep them out of the project's git repository, and for nuget to do the download while building.

Is this possible?

This is my nuspec:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft/packaging/2011/08/nuspec.xsd">
  <metadata>
    <version>1.0.0</version>
    <authors>MyCompany Ltd</authors>
    <owners>MyCompany Ltd</owners>
    <id>MyCompany.ReferencedLibraries</id>
    <title>MyCompany.ReferencedLibraries</title>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Libraries referenced by my project</description>
    <copyright>Copyright MyCompany Ltd 2025</copyright>
    <dependencies />
  </metadata>
  <files>
    <file src="Package\**" target="content\Lib" />
  </files>
</package>

... all the files are in the Package directory and are to be copied into the project's Lib directory within the project's code base.

I have noticed that if I delete the files after installation, and build again, it doesn't restore the files automatically, so adding them to .gitignore won't help - even though I thought this was how nuget worked.

Could the difference be pointing the target the content folder?

Share Improve this question asked Mar 5 at 23:00 komodospkomodosp 3,6584 gold badges37 silver badges68 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Not exactly answering your question, but if you want to keep NuGet packages from being pushed to the GIT repository ever, add an entry to your .gitignore file. Even after building the project and the NuGet packages have been downloaded, none of these files will be pushed to the GIT repository. Hope this helps.

the point of this exercise is to keep them out of the project's git repository

From looking at this document Omitting NuGet packages in source control systems, we can know that

Developers typically omit NuGet packages from their source control repositories and rely instead on package restore to reinstall a project's dependencies before a build.

As @areyesram said, you can use .gitignore file.

For example:

# Ignore NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*

Docs Referred:

Omitting NuGet packages in source control systems

Automatically check for missing packages during build

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745003617a279377.html

最新回复(0)