-
Notifications
You must be signed in to change notification settings - Fork 0
/
how-to-restore-nuget-packages-for-solution.htm
156 lines (138 loc) · 7.93 KB
/
how-to-restore-nuget-packages-for-solution.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>How to restore nuget packages for solution</title>
</head>
<body><a href="https://github.com/ArsenShnurkov/gentoo-mono-handbook"><img alt="Fork me on GitHub" id="forkme" src="images/forkme.png" align="right" width="100" /></a>
<table><tr><td style="vertical-align:top;">
<h1>How to restore nuget packages for solution</h1>
</td><td style="vertical-align:top;">
<a href="index.htm">Gentoo Mono Handbook</a>
<br />
</td></tr></table>
By default, the NuGet.Config file instructs NuGet to bypass adding package binaries to source control.
<h2>.gitignore</h2>
<a href="https://docs.nuget.org/consume/package-restore">https://docs.nuget.org/consume/package-restore</a>
<pre>
# Ignore NuGet Packages
*.nupkg
# Ignore the packages folder
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
</pre>
<h2>Three approaches</h2>
NuGet offers three approaches to using package restore.
<br />
Command-Line Package Restore is required when building a solution from the command-line; it was introduced in early versions of NuGet, but was improved in NuGet 2.7.
<br />
The MSBuild-integrated package restore approach is the original Package Restore implementation and though it continues to work in many scenarios, it does not cover the full set of scenarios addressed by the other two approaches.
<br />
Automatic Package Restore is the NuGet team's recommended approach to Package Restore within Visual Studio, and it was introduced in NuGet 2.7.
<h3>Command-Line Package Restore</h3>
nuget restore TheSolutionFilname.sln
<h3>MSBuild-Integrated approach</h3>
In the old way, you right click on your solution in VS and choose Enable package restore. This causes VS to modify your csproj files, and create .nuget folder containing nuget.exe and some other files.
<br />
<br />
NuGet modifies the project files in the solution to reference the NuGet.targets file
so it can participate in the build process.
<br />
the presence of a NuGet.targets file
determines whether NuGet will continue to use the MSBuild-Integrated approach.
<br />
<br />
Each project file contain the fragment like
<pre>
<RestorePackages>true</RestorePackages>
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
...
<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>
</pre>
<h3>Automatic Package Restore</h3>
Nuget Automatic Package Restore has changed in Nuget 2.7+. Do not mix 'old' and new methods for automatic package restoration.
<br />
<br />
Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file.
Open the project file(s) in the editor of your choice and remove the settings
<br />
<br />
when building from the command line, you need to run ‘nuget restore’ yourself before msbuild.
<h2>NuGet.targets</h2>
An example of content:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<NuGetPath>$(MSBuildThisFileDirectory).nuget</NuGetPath>
<NuGetExe>$(NuGetPath)\NuGet.exe</NuGetExe>
<CachedNuGet>$(LocalAppData)\NuGet\NuGet.exe</CachedNuGet>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
<NuGetExe>nuget</NuGetExe>
</PropertyGroup>
<Target Name="RestorePackages" BeforeTargets="Build" DependsOnTargets="GetNuGet">
<Exec Command="&quot;$(NuGetExe)&quot; Restore &quot;$(SolutionPath)&quot;" Condition="'$(SolutionPath)' != ''" />
<Exec Command="&quot;$(NuGetExe)&quot; Restore &quot;%(Solution.Identity)&quot;" Condition="'%(Solution.Identity)' != ''" />
</Target>
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' == ''">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll</CodeTaskAssembly>
<!-- In VS2013, the assembly contains the VS version. -->
<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' == '12.0'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll</CodeTaskAssembly>
<!-- In VS2015+, the assembly was renamed, hopefully this will be the last condition! -->
<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' != '' and '$(MSBuildAssemblyVersion)' &gt;= '14.0'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</CodeTaskAssembly>
</PropertyGroup>
<Target Name="GetNuGet" Condition="'$(OS)' == 'Windows_NT' And !Exists('$(NuGetExe)')">
<MakeDir Directories="$(NuGetPath)" Condition="!Exists($(NuGetPath))" />
<DownloadNuGet TargetPath="$(CachedNuGet)" Condition="!Exists($(NuGetExe)) And !Exists($(CachedNuGet))" />
<Copy SourceFiles="$(CachedNuGet)" DestinationFolder="$(NuGetPath)" Condition="!Exists($(NuGetExe))" />
</Target>
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(CodeTaskAssembly)" Condition="'$(OS)' == 'Windows_NT'">
<ParameterGroup>
<TargetPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
TargetPath = Path.GetFullPath(TargetPath);
if (!Directory.Exists(Path.GetDirectoryName(TargetPath)))
Directory.CreateDirectory(Path.GetDirectoryName(TargetPath));
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://www.nuget.org/nuget.exe", TargetPath);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
</pre>
Here we see, that it just ensures, that <strong>nuget restore</strong> command was executed once at the start of build process
<br />
it can be absent, or just in wrong relative path:
<br />
<a href="http://ikeptwalking.com/this-project-references-nuget-packages-that-are-missing-on-this-computer/">http://ikeptwalking.com/this-project-references-nuget-packages-that-are-missing-on-this-computer/</a>
</body>
</html>