2017-01-09 149 views
0

我一直在寻找关于如何让NuGet使用VS2015与GitLab自动恢复软件包的几天,而且我没有运气。NuGet包不会从VS2015与GitLab恢复

场景: 我从GitLab中克隆了一个空的存储库,并使用VS来添加默认的.gitignore和.getattribute文件。我使用.NET框架4.5.2创建了一个HelloWorld控制台应用程序。我想自动递增内部版本号,所以我安装了MSBuild扩展包(在VS的NuGet控制台中输入“Install-Package MSBuild.Extension.Pack”)。这会修改CSPROJ文件。然后,我进一步修改了项目文件,以便在每个版本上更新构建版本号和修订版号。项目文件的净修改部分看起来是这样的:

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

所以我盖了,它的表现不如预期,我检查所有在另一台机器上,我克隆存储库并打开该解决方案。该项目不会加载,VS也不会尝试恢复任何内容;这是错误:

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 

我看到很多具有的NuGet恢复问题的其他人,和很多的解决方案,但没有人似乎属于第一次被使用VS2015开工项目。 (有许多解决方案说,要改变项目或解决方案的.nuget子目录中的内容... VS2015不会创建这样的目录。)

最重要的是,NuGet还原需要在MSBuild中工作,因为我们的构建机器不会运行Visual Studio。

回答

2

为了应对无法开解的问题:

使用此代码,而不是

<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')" /> 

之后,包将被还原时生成在Visual Studio。 (Enabling and disabling package restore in VS

关于恢复问题:

随着MSBuild-integrated restore,有在溶液.nuget文件夹。由于您不想将.nuget文件夹包含到解决方案文件夹中,并在构建机器(例如CI)上构建,因此您需要在构建之前恢复该包。

例如,如果通过TFS/VSTS vNext构建构建,则可以添加NuGet恢复构建步骤。对于gitlab CI版本,可以在文件中的before_script部分中恢复包。

+0

谢谢,解决方案工作。您更正的行是在我安装MSBuild扩展包时由NuGet添加的。顺便说一下,这并不是说我不会使用.nuget文件夹,它不是由VS2015为我创建的,而且我没有文件来填充它。 –