从 NuGet 签入软件包进入版本控制?

在 NuGet 之前,签入项目中使用的所有外部 DLL 是公认的“最佳实践”。通常在 Libs3rdParty目录中。

在使用 NuGet 时,我是否应该签入 packages目录,或者 MSBuild 是否有办法从 NuGet feed 自动下载所需的软件包?

17660 次浏览

Yes. Consider the "packages" directory to be equivalent to your "libs" directory that you mentioned in your question. This is the approach I personally take with my OSS projects.

We are investigating features that would allow MSBuild to auto download the needed packages, but that hasn't been implemented (as of NuGet 1.1).

I think some people may have already implemented such features on their own, but our plan is to look at having that feature built in to NuGet 1.2 or 1.3 hopefully.

I realize the reality was different when this question has been originally posted and answered, but fortunately the answer changed a bit. It is now possible to use NuGet to download dependencies via MSBuild using a Pre-Build event. You don't need to put the packages folder in your code repository, all dependencies will be downloaded and/or updated on build. It may a workaround, but it looks decent enough. See the following blog post for details: http://blog.davidebbo.com/2011/03/using-nuget-without-committing-packages.html

Since asking the question, I've put in the following approach so that I do not have to check in the toplovel Packages directory.

In a toplevel build.msbuild file:

<Target Name="NuGet">
<ItemGroup>
<NuGetPackage Include="*\packages.config" />
</ItemGroup>
<Exec Command='libs\NuGet.exe install "%(NuGetPackage.FullPath)" -o Packages'  />


<!-- optional for project that has JavaScript content -->
<CreateItem Include="Packages\*\Content\Scripts\*">
<Output TaskParameter="Include" ItemName="NuGetJSFiles"/>
</CreateItem>
<Copy SourceFiles="@(NuGetJSFiles)" DestinationFolder="MainProj\Scripts\" OverwriteReadOnlyFiles="true" SkipUnchngedFiles="true" />
<Delete Files="MainProj\Scripts\.gitignore" />
<WriteLinesToFile File="MainProj\Scripts\.gitignore" Lines="%(NuGetJSFiles.Filename)%(NuGetJSFiles.Extension)" /
<Delete Files="@(PostNuGetFiles)" />
</Target>

In each project.csproj file

<Target Name="BeforeBuild">
<Error Condition="!Exists('..\Packages\')" Text="You must run &gt; msbuild build.msbuild to download required NuGet
Packages" />


<!-- optional for project that has JavaScript content -->
<ReadLinesFromFile File="Scripts\.gitignore">
<Output TaskParameter="Lines" ItemName="ReqJSFiles" />
</ReadLinesFromFile>
<Message Text="@(ReqJSFiles)" />
<Error Condition="!Exists('Scripts\%(ReqJSFiles.Identity)')" Text="You must run &gt; msbuild build.msbuild to download required NuGet JS Package - Scripts\%(ReqJSFiles.Identity)" />
</Target>

No

Since this question was asked there is now an easy workflow to use NuGet without commiting packages to source control

From your package manager console you need to install the 'NuGetPowerTools':

Install-Package NuGetPowerTools

Then to enable your projects to support pack restore you need to run another command:

Enable-PackageRestore

Now you are ready to commit your code base without the packages folder. The previous command changed your project files so that if packages are missing they get automatically downloaded and added.

Source

Using NuGet without committing packages to source control

AS of 09/20/13, there is something called "Nuget Restore". You actually don't have to check in package folder if you wish to do so. (Especially if you are using DVCS)

Check out this: Using NuGet Without commiting packages to source control http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

This post has become very outdated. The answer is still NO, but the solution has changed. As of NuGet 2.7+ you can enable automatic package restore without including the NuGet.exe file in your source (this is undesirable to say the least) and if you use any modern DVCS you can ignore the packages folder. If you need any special customizations you can create a nuget.config file in the solution root.

http://docs.nuget.org/docs/reference/package-restore

Also, with the new csproj format you can avoid the extra nuget.config files as well since that is integrated now. Please check out this post which explains that better:

Should .nuget folder be added to version control?

Despite all the answers here, it is still a plain ole' horrible solution to not have all your dependencies under "some kind" of version control.

For GIT, this would mean GIT-LFS.

The recent episode with NPM shows why: If the internet repository of which you depend breaks, are unavailable etc., well then you're screwed aint you?

You are no longer able to build your stuff - and therefore not able to deliver.