简体   繁体   English

NuGet软件包无法使用gitLab从VS2015恢复

[英]NuGet Package won't restore from VS2015 with GitLab

I have been searching for a few days now about how to get NuGet to automatically restore packages using VS2015 with GitLab, and I have had no luck. 我一直在寻找有关如何使NuGet如何使用VS2015和GitLab自动还原软件包的几天,但我没有运气。

Scenario: I cloned an empty repository from GitLab and used VS to add the default .gitignore and .getattribute files. 场景:我从GitLab克隆了一个空的存储库,并使用VS添加了默认的.gitignore和.getattribute文件。 I created a HelloWorld console app using .NET framework 4.5.2. 我使用.NET Framework 4.5.2创建了一个HelloWorld控制台应用程序。 I want auto-incrementing build numbers, so I installed the MSBuild Extension Pack (type "Install-Package MSBuild.Extension.Pack" in the NuGet console in VS). 我想要自动增加内部版本号,因此我安装了MSBuild扩展包(在VS的NuGet控制台中键入“ Install-Package MSBuild.Extension.Pack”)。 This modifies the CSPROJ file. 这将修改CSPROJ文件。 I then modified the project file further to make the build and revision numbers update on each build. 然后,我进一步修改了项目文件,以使版本号和修订号在每个版本上都更新。 The net modified portion of the project file looks like this: 项目文件的净修改部分如下所示:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets" Condition="Exists('..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets')" />
<Import Project="$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" />
<Target Name="BeforeBuild" Condition="'$(Configuration)' == 'Release'">
  <AssemblyInfo AssemblyInfoFiles="@(AssemblyInfoFiles)" />
</Target>
<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\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

So I build it, it behaves as expected, and I check it all in. On another machine, I clone the repository and open the solution. 因此,我对其进行了构建,使其表现出预期的效果,并全部检入。在另一台计算机上,我克隆了存储库并打开了解决方案。 The project will not load, and VS made no attempt to restore anything; 该项目将不会加载,并且VS并未尝试还原任何内容; this is the error: 这是错误:

C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\HelloConsole\HelloConsole.csproj : error  : The imported project "C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.  C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\HelloConsole\HelloConsole.csproj

I see lots of other people having problems with NuGet restores, and lots of solutions, but none of them appear to pertain to projects that were first started using VS2015. 我看到很多其他人在NuGet还原方面遇到问题,也有很多解决方案,但是似乎都与最初使用VS2015开始的项目无关。 (There are many solutions that say to alter something in the project's or solution's .nuget subdirectory...VS2015 creates no such directory.) (有许多解决方案说要更改项目或解决方案的.nuget子目录中的某些内容……VS2015不会创建此类目录。)

On top of all this, the NuGet restore needs to work from MSBuild, since our build machine won't be running Visual Studio. 最重要的是,由于我们的构建计算机将无法运行Visual Studio,因此NuGet还原需要通过MSBuild进行工作。

To deal with the issue of cannot open solution: 处理无法打开解决方案的问题:

Using this code instead 改用此代码

<Import Project="..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" Condition="Exists('..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets')" />

After that, the packages will be restored when build in Visual Studio. 之后,将在Visual Studio中构建时还原软件包。 ( Enabling and disabling package restore in VS ) 在VS中启用和禁用程序包还原

Regarding the issue of restore: 关于还原的问题:

With MSBuild-integrated restore , there is .nuget folder in the solution. 使用MSBuild集成的还原 ,解决方案中有.nuget文件夹。 Since you don't want to include .nuget folder to solution folder and do build on build machine (eg CI), you need to restore the package before build. 由于您不想将.nuget文件夹包括到解决方案文件夹中,并且不想在构建机器(例如CI)上进行构建,因此需要在构建之前还原软件包。

For example, you can add NuGet restore build step if do build through TFS/VSTS vNext build. 例如,如果通过TFS / VSTS vNext构建进行构建,则可以添加NuGet恢复构建步骤。 For gitlab CI build, you can restore package in before_script section in .gitlab-ci.yml file. 对于gitlab CI构建,您可以在.gitlab-ci.yml文件的before_script部分恢复软件包。

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

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