简体   繁体   English

使用git子模块恢复Nuget包

[英]Nuget Package restore with git submodule

i have a project where i include 2 submodules from git. 我有一个项目,其中包括来自git的2个子模块。 Both projects have "nuget package restore" enabled, the parent project too. 两个项目都启用了“nuget包恢复”,父项目也是如此。 The package folder in the two included submodules is not checked in, does not exist in checked out projects. 两个包含的子模块中的包文件夹未签入,在签出的项目中不存在。 When building the parent project Nuget tries to restore the packages in the subfolders but into the wrong package folder! 构建父项目时,Nuget尝试将子文件夹中的包还原到错误的包文件夹中!

"C:\Dev\git\oasisdb\odb_oasis_repository\ODB_OASIS_Repository\.nuget\NuGet.exe" install "C:\Dev\git\oasisdb\odb_oasis_repository\odb_oasis_rvm\ODB_OASIS_RVM_EF\ODB_OASIS_RVM_EF\packages.config" -source ""  -NonInteractive -RequireConsent -solutionDir "C:\Dev\git\oasisdb\odb_oasis_repository\ODB_OASIS_Repository\ "

Why does nuget not restore in the solution dir of the submodule? 为什么nuget不能在子模块的解决方案目录中恢复?

Thanks 谢谢

Nuget is restoring the package in the opened solution directory. Nuget正在打开的解决方案目录中恢复软件包。

You can edit the .csproj of the submodule project and modify package dll references from : 您可以编辑子模块项目的.csproj并修改包的dll引用:

   <ItemGroup>
    <Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.1.0\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
      <Private>True</Private>
    </Reference>

to : 至 :

 <ItemGroup>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>$(SolutionDir)\packages\Microsoft.Rest.ClientRuntime.2.1.0\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
  <Private>True</Private>
</Reference>

Hope this help! 希望这有帮助!

you can use symbolic link: After nuget downloads all packages to solution's packages directory, create symbolic link in submodule's root directory (names packages and link to the solution level packages directory). 您可以使用符号链接:在nuget将所有包下载到解决方案的packages目录后,在子模块的根目录中创建符号链接(命名packages并链接到解决方案级别packages目录)。 In short - in your startup project add Pre-Build event that creates symbolic link between your solution packages directory to all your submodules packages directory: 简而言之 - 在您的启动项目中添加Pre-Build事件,该事件在您的解决方案packages目录与所有子模块packages目录之间创建符号链接:

This is the batch: 这是批次:

SET sourceDir=$(SolutionDir)packages
SET destDir=$(SolutionDir)..\..\submodules\saturn72\src\packages

if not exist %sourceDir% mkdir %sourceDir%

if not exist %destDir% mklink /j %destDir% %sourceDir%

Full explanation and source code: SolutionWithGitSubmodulesAndNuget 完整的解释和源代码: SolutionWithGitSubmodulesAndNuget

If you're using VS2015 Update 1 or later, you can convert your project to use project.json to fix this . 如果您使用的是VS2015 Update 1或更高版本,则可以将项目转换为使用project.json来解决此问题

In short: 简而言之:

  • Run Uninstall-Package <package name> -Force -RemoveDependencies for all your packages. 为所有包运行Uninstall-Package <package name> -Force -RemoveDependencies You may wanna copy-paste your packages.config in notepad before you do this. 在执行此操作之前,您可能希望将packages.config复制粘贴到记事本中。
  • Delete packages.config from the project, save the project, unload 从项目中删除packages.config ,保存项目,卸载
  • Edit the project file and remove: 编辑项目文件并删除:
    • Any referenced .props files at the top related to nuget 顶部的任何引用的.props文件都与nuget相关
    • Any <Reference> elements that reference a package 引用包的任何<Reference>元素
    • The .targets files at the bottom that reference nuget - usually starts with: <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> 底部引用nuget的.targets文件 - 通常以: <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    • If your packages contain Roslyn analyzers, make sure to remove them too. 如果您的软件包包含Roslyn分析器,请确保将其删除。
  • Save the file and relod the project 保存文件并重新安装项目

Add project.json with: 添加project.json

{
  "dependencies": {
  },
  "frameworks": {
    ".NETFramework,Version=v4.6.1": {}
  },
  "runtimes": {
    "win": {}
  }
}

Finally add your packages again, either by hand under dependencies or using Install-Package or with the nuget UI in VS. 最后,在dependencies下手动或使用Install-Package或VS中的nuget UI再次添加包。

I've also had to remove any Microsoft.Bcl.* packages from my projects because they explicitly look for a packages.config file. 我还必须从我的项目中删除任何Microsoft.Bcl.*包,因为它们显式查找packages.config文件。

EDIT: this (removing the Microsoft.Bcl.* packages will give you a compile-time error, even though the project will build fine, because the .targets file Microsoft.Bcl.Build adds will still look for packages.config . 编辑:这(删除Microsoft.Bcl.*包将给你一个编译时错误,即使项目将构建正常,因为Microsoft.Bcl.Build添加的.targets文件仍将查找packages.config

To suppress this, edit your project file and add: 要禁止此操作,请编辑项目文件并添加:

<SkipValidatePackageReferences>true</SkipValidatePackageReferences>

This needs to go to the first <PropertyGroup> that doesn't have a Condition attribute set. 这需要转到没有设置Condition属性的第一个<PropertyGroup> If there isn't one, just add another at the top, like: 如果没有,只需在顶部添加另一个,如:

<PropertyGroup>
    <SkipValidatePackageReferences>true</SkipValidatePackageReferences>
</PropertyGroup>

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

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