运行 MSBuild 自定义任务和 MSBuildRuntimeType 困难

Running MSBuild custom tasks and MSBuildRuntimeType difficulties

主要攻略:

这里我们有一个库NugLibEnforceCore,其中包含一个自定义 MSBuild 任务,我们将把它放在一个文件夹中:解决方案folder.libs

第一步是构建这个库并将其放在适当的文件夹中。 然后我们可能会尝试构建 Consumer 项目,其中有 build.targets 并将尝试 运行 那个自定义任务。

项目和任务是满足预期的最低限度的需求,它们没有任何特殊之处。

Consumer项目中的.targets文件:

  <PropertyGroup>
    <LibFolder>$(SolutionDir).libs</LibFolder>
    <!-- <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' == 'Core'">netstandard2.1\NugLibEnforceCore.dll</LibFolderPart2> -->
    <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' == 'Core'">NugLibEnforceCore.dll</LibFolderPart2>
    <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' != 'Core'">net472\NugLibEnforceCore.dll</LibFolderPart2>
  </PropertyGroup>

  <UsingTask
    TaskName="TestTask"
    AssemblyFile="$(LibFolder)$(LibFolderPart2)"
  />

  <Target Name="DoTheTask" AfterTargets="Build">
    <Message Text="=== Trying TestTask ===" Importance="high" />
    <TestTask />
    <Message Text=" == TestTask is finished" Importance="high" />
  </Target>

在这里分享一下我遇到的问题和一些困难以及我尝试过的与主题相关的方法。

试图重现一个令我恼火且时常发生的问题。 我可以使用自定义任务,但想找到原因并永远解决这个问题。

1.方法 1: 全部基于 .net 核心 - 通过 Visual Studio 构建菜单构建 收到此错误:

Error   MSB4062 
The "TestTask" task could not be loaded from the assembly 
...\MSBuild13 Enforce Core\.libs\net472\WeRTheBest.NugLibEnforceCore.dll. 
Could not load file or assembly 'file:///...\MSBuild13 Enforce Core\.libs\net472\WeRTheBest.NugLibEnforceCore.dll' or one of its dependencies. 
The system cannot find the file specified. 
Confirm that the <UsingTask> 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.   Consumer    ...\MSBuild13 Enforce Core\Consumer\build\build.targets 27  

2。方法 2: 全部基于 .net 核心 - 通过 dotnet msbuild 命令构建

dotnet msbuild NugLibEnforceCore\NugLibEnforceCore.csproj

得到这个错误:

错误:

  === Trying TestTask ===
...\MSBuild13 Enforce Core\Consumer\build\build.targets(27,5): 
error MSB4062: The "TestTask" task could not be loaded from the assembly 
...\MSBuild13 Enforce Core\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll. 
Could not load file or assembly '...\MSBuild13 Enforce Core\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll'. 
The system cannot find the path specified. 
Confirm that the <UsingTask> 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. 
...\MSBuild13 Enforce Core\Consumer\Consumer.csproj]

3。方法 3: 多目标(.Net Core 和 .Net Framework)- 通过 Visual Studio 构建菜单构建:

会造成其他困难,如果需要或在我们继续讨论时,我可能会在此处提供有关后续步骤的更多详细信息。

简介:

在项目基于“.net 标准”的第一种方法中,为什么 MSBuildRuntimeType 试图采用 net472 库?!

在第二种方法中,它无法理解第一种方法中使用的相同路径,正在寻找:

...\[Solution]\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll

而不是

...\[Solution]\.libs\netstandard2.1\NugLibEnforceCore.dll'

在上一个构建中,我 commented/removed“netstandard2.1”是为了更容易构建,因为它目前是一个单目标程序集。

我们知道文件在正确的位置,您也可以手动放置库并在需要时进行测试,唯一重要的是能够 运行 消费者项目的任务而无需任何问题和担忧。 如果需要,我可以分享有关它的视频。

答案简要如下:

  1. visual studio 下的构建和 运行 自定义任务不支持 .net 核心,因此它正在寻找您的库的 .net 框架版本。
  2. 不支持在 MSBuild 中获取解决方案文件夹。

一些参考资料: ref 1 ref 2 ref 3