我有一个netstandard2.0 csproj(让我们称之为MyPackage),它在构建时打包(由GeneratePackageOnBuild指定)到nuget包中。这个nuget包在构建目录中有自定义道具和目标(因此引用项目会导入这些内容)。
我在测试MyPackage的同一个解决方案中有另一个项目(我们称之为MyConsumer)。我希望MyConsumer在构建时拥有从MyPackage导入的构建资产道具和目标,就像它从一些远程nuget源中将它作为PackageReference消费一样。
我怎样才能使这项工作(最简单)?
我已经能够通过一个非常复杂的方法来实现它,我让MyConsumer将一个PackageReference添加到MyPackage并覆盖MyConsumer中的RestoreSources以指向MyPackage的bin目录。这在运行slnet的dotnet构建或Visual Studio构建时非常奇怪,因为在还原期间项目元数据是为所有项目预先生成的,因此此时MyPackage不存在。解决方案是在MyConsumer项目中添加对MSBuild的嵌套调用,但随后情况变得更糟,因为Visual Studio恢复操作与dotnet build执行的自动恢复完全不同。
有没有简单的方法呢?
这就是我现在所拥有的
<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>
答案 0 :(得分:1)
将ProjectReference视为PackageReference或允许PackageReference为本地csproj
如果我理解你是正确的,你想要生成包含项目MyPackage
的包,然后将其安装到测试项目MyConsumer
,并在构建时从MyPackage导入构建资产道具和目标。
要实现这一目标,您需要完成以下几项:
MyPackage
在项目MyConsumer
之前构建。MyPackage.nupkg
添加到测试项目MyConsumer
。以上详情:
- 确保项目
MyPackage
在项目MyConsumer
之前构建。
由于您要测试由项目MyConsumer
生成的包,您应该确保此包在使用它之前生成测试项目,因此我们需要设置项目MyConsumer
引用项目MyPackage
。
- 将包设置为打包器源
您可以使用项目MyPackage
的后期构建事件将包MyPackage.nupkg
复制到本地供稿,或者只需将MyPackage.nupkg
的bin目录添加到包中源。
- 在构建期间将包
MyPackage.nupkg
添加到测试项目MyConsumer
。
使用VS 2017和测试项目PackageReference
的{{1}}样式,您可以将MyConsumer
文件设置到包含测试项目Directory.Build.props
的解决方案的根目录中需要:
MyConsumer
这会将这些NuGet包添加到解决方案中的测试项目<Project>
<ItemGroup>
<PackageReference Include="MyPackage" Version="1.0.* />
</ItemGroup>
</Project>
,它将被用作来自某个远程nuget源的PackageReference。
查看Martin`s answer了解更多详情。
希望这有帮助。