在应该通过msbuild PackageReference使用nuget软件包的新世界中,我们应该如何执行软件包构建目标? [英] How are we supposed to execute package build targets in the new world where nuget packages are consumed through msbuild PackageReference?

查看:64
本文介绍了在应该通过msbuild PackageReference使用nuget软件包的新世界中,我们应该如何执行软件包构建目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium开发一套UI测试.此套件的运行时依赖项之一是chromedriver.exe,我们希望它通过 Selenium.WebDriver.ChromeDriver NuGet程序包.

I am developing a suite of UI tests using Selenium. One of the run-time dependencies of this suite is the chromedriver.exe, which we are expected to consume through the Selenium.WebDriver.ChromeDriver NuGet package.

旧世界

导入此NuGet软件包时,会将以下行注入到csproj文件中:

When this NuGet package is imported the following lines are injected into the csproj file:

<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\Selenium.WebDriver.ChromeDriver.2.44.0\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.44.0\build\Selenium.WebDriver.ChromeDriver.targets'))" />
</Target>
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.44.0\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.44.0\build\Selenium.WebDriver.ChromeDriver.targets')" />

它是Visual Studio自动生成的.这涵盖了我们的基础,请确保在构建时存在Selenium.WebDriver.ChromeDriver包提供的构建目标,并根据需要运行它们.构建目标文件中的逻辑将chromedriver.exe复制/发布到正确的位置.

And it is automatic by the Visual Studio. This covers our bases, making sure the build targets provided by the Selenium.WebDriver.ChromeDriver package are there at the time of the build and running them as needed. The logic inside the build targets file copies/publishes the chromedriver.exe to the right location.

全部为绿色.

新世界.

我在csproj文件中使用了与PackageReference相同的NuGet软件包.凉爽的.但是,该软件包的构建目标不再执行.参见 https://github.com/NuGet/Home/issues/4013 .显然,这是设计使然.

I consume the same NuGet package as PackageReference in the csproj file. Cool. However, the build targets of that package are no longer executed. See https://github.com/NuGet/Home/issues/4013. Apparently, this is by design.

我可以手动导入目标,但是问题是我必须对恢复软件包的位置进行硬编码.它不再在解决方案的packages目录中还原,而是在我的Windows配置文件下还原.但是没有指向该位置的属性,它很难进行硬编码.

I could import the targets manually, but the problem is that I will have to hard code the location where the package is restored. It is no longer restored in the packages directory in the solution, but under my windows profile. But there is no property pointing to this location and hard coding it sucks.

因此,这是适用于我的版本,我讨厌它:

So, here is the version that works for me and I hate it:

<PropertyGroup>
  <MyPackagesPath>$(UserProfile)\.nuget\packages\</MyPackagesPath>
  <SeleniumWebDriverChromeDriverTargets>$(MyPackagesPath)selenium.webdriver.chromedriver\2.44.0\build\Selenium.WebDriver.ChromeDriver.targets</SeleniumWebDriverChromeDriverTargets>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="2.44.0" />
</ItemGroup>

<Target Name="EnsureChromeDriver" AfterTargets="PrepareForRun">
  <Error Text="chrome driver is missing!" Condition="!Exists('$(OutDir)chromedriver.exe')" />
</Target>
<Import Project="$(SeleniumWebDriverChromeDriverTargets)" Condition="Exists('$(SeleniumWebDriverChromeDriverTargets)') And '$(ExcludeRestorePackageImports)' == 'true'" />

总的来说,Sdk风格的项目绝对是很棒的,但是,即使是设计使然,从软件包中运行目标的整个业务也被彻底破坏了.

Overall, the Sdk style projects are absolutely great, but this whole business of running targets from the packages is totally broken, even if it is by design.

我想念什么?

编辑1

因此,这是生成的obj\UITests.csproj.nuget.g.targets的内容:

So, here is the content of the generated obj\UITests.csproj.nuget.g.targets:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>
  <ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
    <Import Project="$(NuGetPackageRoot)selenium.webdriver.chromedriver\2.44.0\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('$(NuGetPackageRoot)selenium.webdriver.chromedriver\2.44.0\build\Selenium.WebDriver.ChromeDriver.targets')" />
  </ImportGroup>
</Project>

请注意,ImportGroup条件为'$(ExcludeRestorePackageImports)' != 'true'.现在,此条件始终为false,因为ExcludeRestorePackageImports似乎被硬编码为

Notice the ImportGroup condition is '$(ExcludeRestorePackageImports)' != 'true'. Now, this condition is always false, because ExcludeRestorePackageImports seems to be hard coded to be true in

c:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets

检查二进制日志可以确认这一点.另外 https://github.com/NuGet/Home/issues/4013 已关闭为WontFix.

Inspecting binary log confirms this. Plus https://github.com/NuGet/Home/issues/4013 was closed as WontFix.

还是我还想念一些东西?

Or am I still missing something?

推荐答案

如果在构建过程中正在运行Restore和其他目标,则由于NuGet修改磁盘上的xml文件或由于导入的MSBuild文件而可能会得到意外的结果. NuGet软件包未正确导入.

If you are running Restore and other targets during the build, you may get unexpected results due to NuGet modifying xml files on disk or because MSBuild files imported by NuGet packages aren't imported correctly.

这篇关于在应该通过msbuild PackageReference使用nuget软件包的新世界中,我们应该如何执行软件包构建目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆