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
如果我理解的正确,您想使用项目
要实现此目标,您需要完成以下几件事:
-
确保在项目
MyConsumer 之前先构建项目MyPackage 。 - 将软件包设置为打包程序源
-
在构建期间,将包
MyPackage.nupkg 添加到测试项目MyConsumer 中。
以上详细信息:
- Make sure the project
MyPackage build before the projectMyConsumer .
由于要测试由项目
- Set the package into the packager source
您可以使用项目
- Add the package
MyPackage.nupkg to the test projectMyConsumer during the build time.
使用VS 2017和测试项目
1 2 3 4 5 | <Project> <ItemGroup> <PackageReference Include="MyPackage" Version="1.0.* /> </ItemGroup> </Project> |
这会将这些NuGet包添加到解决方案中的测试项目
查看马丁的答案以获取更多详细信息。
希望这会有所帮助。