我有一个netStandard2.0csproj(让我们称之为MyPackage),它在构建时(如GeneratePackageOnBuild指定)打包到nuget包中。这个nuget包在构建目录中有自定义的道具和目标(所以引用项目会得到这些导入的)。
我有另一个项目(让我们称之为MyConsumer)在相同的解决方案中测试MyPackage。我希望MyConsumer在构建时拥有从MyPackage导入的构建资产支持和目标,就像它将其作为一个来自远程nuget源代码的PackageReference使用一样。
我怎样才能做到这一点(最简单)?
我已经能够通过一个非常复杂的方法来完成这个任务,在这个方法中,MyConsumer向MyPackage添加了一个PackageReference,并覆盖了MyConsumer中的RestoreSources以指向MyPackage的bin目录。当运行sln的dotnet或时,这会变得非常奇怪,因为项目元数据是在还原期间为所有项目预先生成的,因此在还原过程中不存在MyPackage。解决方案是在MSBuild项目中添加对MyConsumer的嵌套调用,但结果更糟,因为Visual还原操作与由dotnet执行的自动还原操作非常不同。
有什么简单的方法吗?
这就是我现在拥有的
<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>发布于 2018-05-14 15:00:58
将ProjectReference视为PackageReference或允许PackageReference到本地csproj
如果我理解您的更正,您希望生成带有项目MyPackage的包,然后将它安装到测试项目MyConsumer中,并在构建时从MyPackage导入构建资产支持和目标。
要实现这个目标,您需要完成以下几件事情:
MyPackage构建在项目MyConsumer之前。MyPackage.nupkg添加到测试项目MyConsumer中。上述详情:
MyPackage构建在项目MyConsumer之前。由于您希望测试由项目MyConsumer生成的包,所以在测试项目之前,您应该确保这个包很好地使用它,因此我们需要设置项目引用项目 MyPackage.。
您可以使用项目MyPackage的编译后事件将包MyPackage.nupkg复制到本地提要,或者只需将MyPackage.nupkg的bin目录添加到包源。
MyPackage.nupkg添加到测试项目MyConsumer中。使用VS 2017和测试项目MyConsumer的MyConsumer样式,可以将Directory.Build.props文件设置到包含所需测试项目MyConsumer的解决方案根目录中:
<Project>
<ItemGroup>
<PackageReference Include="MyPackage" Version="1.0.* />
</ItemGroup>
</Project>这将将这些NuGet包添加到解决方案中的测试项目MyConsumer中,它将用作来自某些远程nuget源代码的PackageReference。
有关更多细节,请查看Martin`s answer。
希望这能有所帮助。
https://stackoverflow.com/questions/50321933
复制相似问题