简体   繁体   English

MSBuild - 一个 Package 参考干扰加载单独的任务

[英]MSBuild - one Package Reference interferes with loading a separate Task

Using MSBuild, the following builds and works fine:使用 MSBuild,以下构建和工作正常:

<PackageReference Include="Publicise.MSBuild.Task" Version="1.3.0"/>

<Target Name="Publicise" BeforeTargets="BeforeBuild">
  <Publicise
    AssemblyPath="..."
    OutputPath="../"/>
</Target>

However, when I add another Package Reference (changing nothing else), it encounters errors on build:但是,当我添加另一个 Package 参考(没有其他改变)时,它在构建时遇到错误:

<PackageReference Include="Publicise.MSBuild.Task" Version="1.3.0"/>
<PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.13"/>

This results in error MSB4062:这会导致错误 MSB4062:

The "Publicise" task could not be loaded from the assembly...\ILRepack.MSBuild.Task.dll.无法从程序集中加载“公开”任务...\ILRepack.MSBuild.Task.dll。 Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.确认声明正确,程序集及其所有依赖项均可用,并且任务包含实现 Microsoft.Build.Framework.ITask 的公共 class。

Why is something completely separate preventing the task from being properly found, and how can I fix this?为什么完全独立的东西会阻止正确找到任务,我该如何解决这个问题?

The issue is caused by these two msbuild task nuget packages accidentally.该问题是由这两个 msbuild 任务 nuget 包意外引起的。

And since you have installed the ILRepack.MSBuild.Task nuget package at the end.并且由于您最后安装了ILRepack.MSBuild.Task nuget package。 And the PackageReference node of ILRepack.MSBuild.Task is akways at the end. ILRepack.MSBuild.TaskPackageReference节点在最后。 So $(TaskAssembly) is always loads from ILRepack.MSBuild.Task nuget package and the value from publicise.msbuild.task is being covered. So $(TaskAssembly) is always loads from ILRepack.MSBuild.Task nuget package and the value from publicise.msbuild.task is being covered. And the issue The "Publicise" task could not be loaded from the assembly ILRepack.MSBuild.Task.dll makes sense.并且The "Publicise" task could not be loaded from the assembly ILRepack.MSBuild.Task.dll的问题是有意义的。

C:\Users\xxx\.nuget\packages\publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props

在此处输入图像描述

C:\Users\xxx\.nuget\packages\ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props

在此处输入图像描述

Also , when you project loads the nuget package, you can check under C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props : Also , when you project loads the nuget package, you can check under C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props :

在此处输入图像描述

Loading the ILRepack.MSBuild.Task.props is always at the end and $(TaskAssembly) is always from ilrepack.msbuild.task due to being overwritten by the later installed package.加载ILRepack.MSBuild.Task.props总是在最后, $(TaskAssembly)总是来自ilrepack.msbuild.task因为被后来安装的 ZEFE90A8E604A7C840E88D03A6 覆盖

The error Publicise task(should be from publicise.msbuild.task ) from ilrepack.msbuild.task could be understood.可以理解来自ilrepack.msbuild.task的错误Publicise task(应该来自publicise.msbuild.task )。

Solution解决方案

So you should make publicise.msbuild.task at the end.所以你应该在最后做publicise.msbuild.task

Solution 1)解决方案1)

open C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props file,打开C:\xxx\source\repos\xxx(project_name)\xxx(project_name)\obj\xxx.csproj.nuget.g.props文件,

modify like this:像这样修改:

<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
   
 <Import Project="$(NuGetPackageRoot)ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props" Condition="Exists('$(NuGetPackageRoot)ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props')" />

 <Import Project="$(NuGetPackageRoot)publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props" Condition="Exists('$(NuGetPackageRoot)publicise.msbuild.task\1.3.0\build\Publicise.MSBuild.Task.props')" />
</ImportGroup>

make Publicise.MSBuild.Task.props at the buttom.在底部制作Publicise.MSBuild.Task.props

Then, save the changes and then click Build button rather than Rebuild button to test again.然后,保存更改,然后单击Build按钮而不是Rebuild按钮再次测试。

Solution 2)解决方案2)

downgrade ILRepack.MSBuild.Task nuget package to version 2.0.0 .ILRepack.MSBuild.Task nuget package 降级到版本2.0.0

=============================== ================================

Update 1更新 1

Thanks for sharing your opinion about the workaround.感谢您分享您对解决方法的意见。 Since these two nuget packages have to be used in your project, so these two solutions might not be very useful.由于您的项目中必须使用这两个 nuget 包,因此这两个解决方案可能不是很有用。

The error, conflict is caused by the author of the nuget packages and incidentally, you're using the same TaskAssembly property from these two nuget packages at the same time.错误,冲突是由 nuget 包的作者引起的,顺便说一句,您同时使用了这两个 nuget 包中的相同TaskAssembly属性。

TreatAsLocalProperty="TaskAssembly" will not solve the issue. TreatAsLocalProperty="TaskAssembly"不会解决问题。 <packages_id>.props files from the nuget packages are still embedded in the project's CSPROj file. nuget 包中的<packages_id>.props文件仍嵌入在项目的 CSPROj 文件中。 Whether the fields are the same as TaskAssembly or will conflict.字段是否与 TaskAssembly 相同或是否会发生冲突。

The better solution is that you should rename one of the TaskAssembly of the nuget packages to another, which would not cause conflict.更好的解决方案是您应该将 nuget 包的TaskAssembly之一重命名为另一个,这样不会导致冲突。

1) Open C:\Users\xxx\.nuget\packages\ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props file: 1)打开C:\Users\xxx\.nuget\packages\ilrepack.msbuild.task\2.0.13\build\ILRepack.MSBuild.Task.props文件:

change TaskAssembly property to another like TaskAssembly_copy :TaskAssembly属性更改为另一个类似TaskAssembly_copy

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM