【问题标题】:Running MSBuild custom tasks and MSBuildRuntimeType difficulties运行 MSBuild 自定义任务和 MSBuildRuntimeType 困难
【发布时间】:2020-10-15 03:23:50
【问题描述】:

主要战略:

这里我们有一个库NugLibEnforceCore,其中包含一个自定义 MSBuild 任务,我们将它放在一个文件夹中:The solution 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 core - 通过 Visual Studio Build Menu 构建 得到这个错误:

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 core - 通过 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'

在上一次构建中,我评论/删除了“netstandard2.1”以便于构建,因为它目前是一个单目标程序集。

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

【问题讨论】:

    标签: visual-studio msbuild visual-studio-2019 roslyn


    【解决方案1】:

    答案是:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2012-03-16
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多