关于msbuild:将ProjectReference视为PackageReference或允许PackageReference到本地csproj

Treat ProjectReference as PackageReference or allow PackageReference to local csproj

我有一个netstandard2.0 csproj(简称为MyPackage),它在构建时(由GeneratePackageOnBuild指定)打包到nuget包中。此nuget包在build目录中具有自定义的props和target(因此引用项目可将其导入)。

我在同一解决方案中有另一个项目(简称为MyConsumer),用于测试MyPackage。我希望MyConsumer在构建时从MyPackage导入构建资产的道具和目标,就好像它是从某个远程nuget来源将其作为PackageReference消耗一样。

我如何才能使它正常工作(最简单)?

我已经能够通过一种非常复杂的方法来做到这一点,在该方法中,我让MyConsumer向MyPackage添加PackageReference并覆盖MyConsumer中的RestoreSources以指向MyPackage的bin目录。当运行sln的dotnet构建或Visual Studio构建时,这变得非常奇怪,因为在还原期间所有项目的项目元数据都是预先生成的,因此此时MyPackage不存在。解决方案是在MyConsumer项目中向MSBuild添加嵌套调用,但是这变得更加糟糕,因为Visual Studio还原的操作与dotnet构建执行的自动还原的操作大不相同。

有什么简单的方法可以做到吗?

这就是我现在所拥有的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Project>
  <Target Name="Build">    
    <Message Text="Running inner build" Importance="high" />

    <!--
    Need to call MSBuild twice, once to restore, then again to restore and build to get the restore of the Sdk to work
    because of this bug in MSBuild: https://github.com/Microsoft/msbuild/issues/2455
    Note the trailing Prop=1 is required to get MSBuild to invalid it's cache of the project target imports
    -->
    <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Restore" Properties="Configuration=$(Configuration);Version=$(Version);IsInnerBuild=true;Prop=1" />
    <!-- Have to use dotnet build instead of another call to MSBuild because of another bug that prevents proper imports within the same physical process  -->
    <Exec Command="dotnet build /p:Configuration=$(Configuration) /p:Version=$(Version) /p:IsInnerBuild=true" />
    <Message Text="Finished inner build" Importance="high" />
  </Target>

  <Target Name="Restore" />

  <Target Name="RemoveBin">
    <RemoveDir Directories="bin" />
  </Target>

  <!-- Don't do real cleans old rebuild since it breaks MSBuild due to the same above bug -->
  <Target Name="Rebuild" DependsOnTargets="RemoveBin;Build">
  </Target>
</Project>

Treat ProjectReference as PackageReference or allow PackageReference to local csproj

如果我理解的正确,您想使用项目MyPackage生成软件包,然后将其安装到测试项目MyConsumer,并在构建时从MyPackage导入构建资产的道具和目标。

要实现此目标,您需要完成以下几件事:

  • 确保在项目MyConsumer之前先构建项目MyPackage
  • 将软件包设置为打包程序源
  • 在构建期间,将包MyPackage.nupkg添加到测试项目MyConsumer中。

以上详细信息:

  • Make sure the project MyPackage build before the project MyConsumer.

由于要测试由项目MyConsumer生成的软件包,因此应确保在测试项目使用该软件包之前先使它磨合,因此我们需要将项目MyConsumer设置为引用项目MyPackage。铅>

  • Set the package into the packager source

您可以使用项目MyPackage的构建后事件将包MyPackage.nupkg复制到本地提要,或者只需将MyPackage.nupkg的bin目录添加到包源中。

  • Add the package MyPackage.nupkg to the test project MyConsumer during the build time.

使用VS 2017和测试项目MyConsumerPackageReference样式,您可以在包含所需测试项目MyConsumer的解决方案的根目录中设置Directory.Build.props文件:

1
2
3
4
5
<Project>
  <ItemGroup>
    <PackageReference Include="MyPackage" Version="1.0.* />
  </ItemGroup>
</Project>

这会将这些NuGet包添加到解决方案中的测试项目MyConsumer中,它将用作来自某些远程nuget源的PackageReference。

查看马丁的答案以获取更多详细信息。

希望这会有所帮助。