简体   繁体   English

此项目引用此计算机上缺少的 NuGet 包

[英]This project references NuGet package(s) that are missing on this computer

I have an ASP.NET MVC5 application that worked yesterday and now I am getting this error when I try to build:我有一个昨天工作的 ASP.NET MVC5 应用程序,现在当我尝试构建时出现此错误:

This project references NuGet package(s) that are missing on this computer.此项目引用此计算机上缺少的 NuGet 包。

I have the two options checked that allow nuget to automatically download and install missing packages checked / turned ON.我选中了两个选项,允许 nuget 自动下载和安装丢失的软件包,选中/打开。 I have also tried deleting all of the files in the packages folder and then have nuget re-download them.我还尝试删除包文件夹中的所有文件,然后让 nuget 重新下载它们。 Also when I open nuget and look for updates it says there are none that need to be installed.此外,当我打开 nuget 并查找更新时,它说不需要安装任何更新。 I can't figure what else to do to move beyond this amazingly annoying issue.我想不出还能做些什么来超越这个令人惊讶的烦人问题。

I have also enabled nuget restore by right clicking the project and selecting that option.我还通过右键单击项目并选择该选项来启用 nuget 恢复。 It then added a nuget folder and three items in that folder and does nothing to resolve the problem.然后它在该文件夹中添加了一个 nuget 文件夹和三个项目,但没有解决问题。 I've tried re-building and still get the same error.我已经尝试重新构建,但仍然得到同样的错误。

In my case, I had to remove the following from the .csproj file:就我而言,我必须从 .csproj 文件中删除以下内容:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(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>

In fact, in this snippet you can see where the error message is coming from.实际上,在此代码段中,您可以看到错误消息的来源。

I was converting from MSBuild-Integrated Package Restore to Automatic Package Restore ( http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore )我正在从MSBuild-Integrated Package Restore转换为Automatic Package Restore ( http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore )

One solution would be to remove from the .csproj file the following:一种解决方案是从 .csproj 文件中删除以下内容:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(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>

How?如何?

  1. Right click on project.右键单击项目。 Unload Project.卸载项目。
  2. Right click on project.右键单击项目。 Edit csproj.编辑 csproj。
  3. Remove the part from the file.从文件中删除零件。 Save.节省。
  4. Right click on project.右键单击项目。 Reload Project.重新加载项目。

In my case it happened after I moved my solution folder from one location to another, re-organized it a bit and in the process its relative folder structure changed.在我的情况下,它发生在我将我的解决方案文件夹从一个位置移动到另一个位置,重新组织它并且在此过程中它的相对文件夹结构发生变化之后。

So I had to edit all entries similar to the following one in my .csproj file from所以我不得不在我的.csproj文件中编辑所有类似于以下条目的条目

  <Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

to

  <Import Project="packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

(Note the change from ..\packages\ to packages\ . It might be a different relative structure in your case, but you get the idea.) (注意从..\packages\packages\的变化。在你的情况下,它可能是一个不同的相对结构,但你明白了。)

I easily solve this problem by right clicking on my solution and then clicking on the Enable NuGet Package Restore option我通过右键单击我的解决方案然后单击启用 NuGet 包还原选项轻松解决了这个问题

(PS: Ensure that you have the Nuget Install From Tools--> Extensions and Update--> Nuget Package Manager for Visual Studio 2013. If not install this extention first) (PS:确保你有 Nuget Install From Tools--> Extensions and Update--> Nuget Package Manager for Visual Studio 2013。如果没有先安装这个扩展)

Hope it helps.希望能帮助到你。

In my case it had to do with the Microsoft.Build.Bcl version.就我而言,它与 Microsoft.Build.Bcl 版本有关。 My nuget package version was 1.0.21, but my project files were still pointing to version 1.0.14我的 nuget 包版本是 1.0.21,但我的项目文件仍然指向版本 1.0.14

So I changed my .csproj files from:所以我改变了我的 .csproj 文件:

  <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
   <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="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=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
  </Target>

to:至:

 <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
  <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="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=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />

And the build was working again.并且构建再次工作。

I have the same issue.我有同样的问题。 I encountered it when I copied an existing project and transferred it on the folder of my solution directory and added it as existing project to my empty solution.当我复制一个现有项目并将其转移到我的解决方案目录的文件夹中并将其作为现有项目添加到我的空解决方案中时,我遇到了它。 So I have to edit my csproj file and look for this specific line of code, most of the time, this is can be found on the last lines:所以我必须编辑我的 csproj 文件并寻找这行特定的代码,大多数时候,这可以在最后几行找到:

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

After that line, I have to comment these out:在该行之后,我必须将这些注释掉:

  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use 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('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props'))" />
    <Error Condition="!Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets'))" />
  </Target>
  <Import Project="..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets" Condition="Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" />

Your solution will prompt that there was a change on your project, just select Reload All:您的解决方案会提示您的项目有更改,只需选择 Reload All:

在此处输入图像描述 Then everything works fine after rebuilding my solution.然后在重建我的解决方案后一切正常。

If you are using TFS如果您使用的是 TFS

Remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder.从解决方案的.nuget文件夹中删除NuGet.exeNuGet.targets文件。 Make sure the files themselves are also removed from the solution workspace.确保文件本身也从解决方案工作区中删除。 Retain the NuGet.Config file to continue to bypass adding packages to source control.保留NuGet.Config文件以继续绕过将包添加到源代码管理。

Edit each project file (eg, .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file.编辑解决方案中的每个项目文件(例如,.csproj、.vbproj)并删除对NuGet.targets文件的任何引用。 Open the project file(s) in the editor of your choice and remove the following settings:在您选择的编辑器中打开项目文件并删除以下设置:

<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>

If you are not using TFS如果您不使用 TFS

Remove the .nuget folder from your solution.从您的解决方案中删除.nuget文件夹。 Make sure the folder itself is also removed from the solution workspace.确保文件夹本身也从解决方案工作区中删除。

Edit each project file (eg, .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file.编辑解决方案中的每个项目文件(例如,.csproj、.vbproj)并删除对NuGet.targets文件的任何引用。 Open the project file(s) in the editor of your choice and remove the following settings:在您选择的编辑器中打开项目文件并删除以下设置:

<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>

Reference: Migrating MSBuild-Integrated solutions to use Automatic Package Restore参考: 迁移 MSBuild 集成解决方案以使用自动包还原

Removed below lines in .csproj file删除了 .csproj 文件中的以下行

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" 
Condition="Exists('$(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>

Is it possible that the packages have been restored to the wrong folder?软件包是否可能已恢复到错误的文件夹? Check that the paths in the csproj files are correct.检查 csproj 文件中的路径是否正确。

If they are different it could be caused by the packages now being restored to a different location.如果它们不同,则可能是由于现在将包恢复到不同的位置。 This could be caused by a NuGet.Config file being checked in specifying a node like this:这可能是由于在指定这样的节点时检查了 NuGet.Config 文件:

<add key="repositoryPath" value="..\..\Packages" />

The packages are being restored, by the projects are still looking at the old location.正在恢复包,项目仍在查看旧位置。

I had the same issue.我遇到过同样的问题。 In my case installing the Microsoft.Bcl.Build package fixed the problem.在我的情况下,安装Microsoft.Bcl.Build包解决了这个问题。

One solution would be to remove from the .csproj file the following:一种解决方案是从 .csproj 文件中删除以下内容:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

This project references NuGet package(s) that are missing on this computer.此项目引用此计算机上缺少的 NuGet 包。 Enable NuGet Package Restore to download them.启用 NuGet 包还原以下载它们。 For more information, see http://go.microsoft.com/fwlink/?LinkID=322105 .有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=322105 The missing file is {0}.丢失的文件是 {0}。

The first thing to try is to right-click the solution and select "Restore Nuget Packages".首先要尝试的是右键单击解决方案并选择“还原 Nuget 包”。

In my case that did not work, so I followed some recommendations on deleting "Import" and "Target" on project file, this worked for 2 of my 3 projects, but got a different error on the last one.就我而言,这不起作用,所以我遵循了一些关于删除项目文件中的“导入”和“目标”的建议,这适用于我的 3 个项目中的 2 个,但在最后一个项目中出现了不同的错误。

What worked was to open the Package Manager Console and run:有效的是打开包管理器控制台并运行:

Update-Package -reinstall -ProjectName MyProjectName

It takes some time but since it reinstall all packages your project will compile without problems这需要一些时间,但由于它重新安装所有包,您的项目将毫无问题地编译

These are the steps I used to fix the issue:这些是我用来解决问题的步骤:

To add nuget packages to your solution:要将 nuget 包添加到您的解决方案中:

  1. Right click on the project (not solution) you want to reference nuget packages.右键单击要引用 nuget 包的项目(不是解决方案)。
  2. Choose: Manage nuget packages选择:管理 nuget 包
  3. On the popup window, on the left you have three choices.在弹出窗口的左侧,您有三个选择。 If you choose Online > Microsoft & .NET , you will be able to install Microsoft ASP.NET Web API 2.2 package grouper (or whatever package you need - mine was this).如果您选择Online > Microsoft & .NET ,您将能够安装 Microsoft ASP.NET Web API 2.2 包 grouper(或您需要的任何包 - 我的是这个)。
  4. Now right click on your solution (not project) and choose Enable nuget package restore .现在右键单击您的解决方案(不是项目)并选择Enable nuget package restore This will cause the packages to be automagically downloaded at compilation.这将导致包在编译时自动下载。

对我来说,它起作用了,因为我只是将 .nuget 文件夹从工作解决方案复制到现有解决方案,并引用了它的内容!

I had this when the csproj and sln files were in the same folder (stupid, I know).当 csproj 和 sln 文件在同一个文件夹中时,我就有了这个(愚蠢,我知道)。 Once I moved to sln file to the folder above the csproj folder my so一旦我将 sln 文件移动到 csproj 文件夹上方的文件夹中

I had this issue when I got a new computer from my company and tried to build the project after cloning it with Git.当我从公司获得一台新计算机并尝试在使用 Git 克隆项目后构建项目时,我遇到了这个问题。 The problem was my NuGet settings did not include a remote repository from which to fetch packages.问题是我的 NuGet 设置不包括从中获取包的远程存储库。 Following the FAQ at NuGet.org , I found this: 在 NuGet.org 的常见问题解答之后,我发现了这个:

I don't see nuget.org in my list of repositories, how do I get it back?我在我的存储库列表中没有看到 nuget.org,我该如何取回它?

  • Add https://api.nuget.org/v3/index.json to your list of sources, or Delete %appdata%\.nuget\NuGet.Config (Windows) or ~/.nuget/NuGet/NuGet.Config (Mac/Linux) and let NuGet re-create it.https://api.nuget.org/v3/index.json添加到您的源列表,或删除%appdata%\.nuget\NuGet.Config (Windows) 或~/.nuget/NuGet/NuGet.Config (Mac /Linux) 并让 NuGet 重新创建它。

In my case, there wasn't a %appdata%\.nuget directory on my machine at all, so from within Visual Studio, I followed these steps to fix the issue:就我而言,我的机器上根本没有%appdata%\.nuget目录,因此在 Visual Studio 中,我按照以下步骤解决了这个问题:

  1. Click on Tools > NuGet Package Manager > Package Manager Settings in the toolbar单击工具栏中的Tools > NuGet Package Manager > Package Manager Settings
  2. Select NuGet Package Manager > Package Sources from the list on the left从左侧列表中选择NuGet Package Manager > Package Sources
  3. Click the green + in the top-right to add a new source单击右上角的绿色+以添加新源
  4. Set the Name: value to NuGet.orgName:值设置为NuGet.org
  5. Set the Source: value to https://api.nuget.org/v3/index.jsonSource:值设置为https://api.nuget.org/v3/index.json

After that, NuGet was able to download the packages it couldn't find.之后,NuGet 能够下载它找不到的包。

To get rid of the errors in the build, I did these steps:为了摆脱构建中的错误,我执行了以下步骤:

  1. Delete the bin and obj directories in the solution删除解决方案中的binobj目录
  2. Click on Build > Rebuild Solution in the toolbar单击工具栏中的Build > Rebuild Solution

Once the build was complete, the errors were gone.构建完成后,错误就消失了。

The mistake appears to have been triggered somewhere in the code.该错误似乎是在代码中的某处触发的。 Let's look at the.csproj file, where this can be found towards the end.让我们看一下 .csproj 文件,它可以在最后找到。

Step 1 Remove the package from package.config file.步骤 1从 package.config 文件中删除包。

<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net46" />

Step 2 Edit the .csproj project file and removed the below settings步骤 2编辑 .csproj 项目文件并删除以下设置

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Use 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('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />

Step 3 Go to package manager console and run the command Update-Package –reinstall步骤 3转到包管理器控制台并运行命令 Update-Package –reinstall

I had the same issue when i reference the Class library into my MVC web application,当我将类库引用到我的 MVC Web 应用程序中时,我遇到了同样的问题,

the issue was the nuget package version number mismatch between two projects.问题是两个项目之间的 nuget 包版本号不匹配。

ex: my class library had log4net of 1.2.3 but my webapp had 1.2.6例如:我的类库的 log4net 为 1.2.3,但我的 webapp 有 1.2.6

fix: just make sure both the project have the same version number referenced.修复:只需确保两个项目都引用了相同的版本号。

Editing .sln and .csproj is not always that easy or desirable.编辑 .sln 和 .csproj 并不总是那么容易或理想。 Once you get the error list you can see what projects have missing packages (also, the References node usually indicate that there are missing assemblies unless packages are source code, resources, images, or just text-based ones).获得错误列表后,您可以查看哪些项目缺少包(此外,References 节点通常指示缺少程序集,除非包是源代码、资源、图像或只是基于文本的包)。

Removing and then adding the packages is not a good idea unless you use the latest version of the package.除非您使用最新版本的软件包,否则删除然后添加软件包不是一个好主意。 Otherwise be prepared for surprises, not always pleasant ones.否则,要为惊喜做好准备,而不是总是令人愉快的。

If, say, the package is EntityFramework then from NuGet gallery you get the latest version which at the time of writing this comment it is 6.1.3 .例如,如果包是 EntityFramework ,那么您可以从 NuGet 库中获得最新版本,在撰写此评论时它是6.1.3

So, maybe the safest way to handle the situation is to restore the missing packages one by one.所以,或许最安全的处理方式就是将丢失的包一一还原。 Yes, a bit painful exercise but chasing subtle bugs due to different package version maybe much more unpleasant.是的,有点痛苦的练习,但由于不同的包版本而追逐细微的错误可能更令人不快。

Having this said, and let again EntityFramework be the missing package, you can issue the following command in the Package-Manager Console:话虽如此,让 EntityFramework 再次成为丢失的包,您可以在 Package-Manager 控制台中发出以下命令:

PM> Install-Package EntityFramework -Version 6.0.1 

This will install the correct version, that is 6.0.1 , that is the one that is specified in packages.config:这将安装正确的版本,即6.0.1 ,即在 packages.config 中指定的版本:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="EntityFramework" version="6.0.1" targetFramework="net451" />
    </packages>

I had the same error but in my case it was not related to nuget packages at all.我有同样的错误,但在我的情况下,它根本与 nuget 包无关。 My solution had project that had reference to other projects that were not a part of my solution and were not built.我的解决方案的项目引用了不属于我的解决方案且未构建的其他项目。 After building them with some other solution (or I could include them into my solution as well), AND re-opening my solution in visual studio the issue was resolved.在使用其他解决方案构建它们之后(或者我也可以将它们包含到我的解决方案中),并在 Visual Studio 中重新打开我的解决方案后,问题得到了解决。

After some time to solve this problem.一段时间后解决了这个问题。 Simply add a Package source with the source https://api.nuget.org/v3/index.json solve the error on my side只需添加带有源https://api.nuget.org/v3/index.json的包源即可解决我这边的错误

I created a folder named '.nuget' in solution root folder Then added file 'NuGet.Config' in this folder with following content我在解决方案根文件夹中创建了一个名为“.nuget”的文件夹,然后在此文件夹中添加了文件“NuGet.Config”,内容如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
 <add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>

Then created file '.nuGet.targets' as below $(MSBuildProjectDirectory)..\然后创建文件'.nuGet.targets'如下$(MSBuildProjectDirectory)..\

    <!-- Enable the restore command to run before builds -->
    <RestorePackages Condition="  '$(RestorePackages)' == '' ">false</RestorePackages>

    <!-- Property that enables building a package from a project -->
    <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

    <!-- Determines if package restore consent is required to restore packages -->
    <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

    <!-- Download NuGet.exe if it does not already exist -->
    <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
    <!--
        <PackageSource Include="https://nuget.org/api/v2/" />
        <PackageSource Include="https://my-nuget-source/nuget/" />
    -->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
    <!-- Windows specific commands -->
    <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
    <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
    <PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
    <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
    <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
    <PackagesConfig>packages.config</PackagesConfig>
    <PackagesDir>$(SolutionDir)packages</PackagesDir>
</PropertyGroup>

<PropertyGroup>
    <!-- NuGet command -->
    <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
    <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

    <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
    <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>

    <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

    <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
    <!-- Commands -->
    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch) -o "$(PackagesDir)"</RestoreCommand>
    <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(RestorePackages) == 'true'">
        RestorePackages;
        $(BuildDependsOn);
    </BuildDependsOn>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(BuildPackage) == 'true'">
        $(BuildDependsOn);
        BuildPackage;
    </BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
    <!-- Raise an error if we're unable to locate nuget.exe  -->
    <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
    <SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
    <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')"  />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(RestoreCommand)"
          Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

    <Exec Command="$(RestoreCommand)"
          LogStandardErrorAsError="true"
          Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(BuildCommand)" 
          Condition=" '$(OS)' != 'Windows_NT' " />

    <Exec Command="$(BuildCommand)"
          LogStandardErrorAsError="true"
          Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <OutputFilename 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 {
                OutputFilename = Path.GetFullPath(OutputFilename);

                Log.LogMessage("Downloading latest version of NuGet.exe...");
                WebClient webClient = new WebClient();
                webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

                return true;
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                return false;
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

 <UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <EnvKey ParameterType="System.String" Required="true" />
        <EnvValue ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Using Namespace="System" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try {
                Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
            }
            catch  {
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM