【问题标题】:How to nest a Target within another Target in the .csproj file?如何将目标嵌套在 .csproj 文件中的另一个目标中?
【发布时间】:2020-12-15 15:19:35
【问题描述】:

长话短说,在 nuget 还原任务完成后,我需要在 Azure 管道 Ubuntu 代理上终止 VBCSCompiler.exe。在 windows2019 代理上我不需要这样做,但在 ubuntu 上我遇到了一个问题:

/home/vsts/work/1/s/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8/build/net45/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props(17,5): 
warning MSB3021: Unable to copy file "/home/vsts/work/1/s/packages/Microsoft.Net.Compilers.2.4.0/build/../tools/csc.exe" to "/bin/roslyn/csc.exe". Access to the path '/bin/roslyn' is denied. [/home/vsts/work/1/s/Bobby.ProjectA/Bobby.ProjectA.csproj]

所以根据 this post here 中的 Levi,我需要在 .csproj 文件中添加 <Target Name="CheckIfShouldKillVBCSCompiler"> 行。我是这样添加的:

...
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">
   </Target>
   <Target Name="AfterBuild">
   </Target> -->
   <Target Name="CheckIfShouldKillVBCSCompiler">
     <PropertyGroup>
       <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
     </PropertyGroup>
   </Target>
 </Project>

但这并没有做任何事情来解锁/bin/roslyn 路径。

我认为这必须添加到 BeforeBuild 目标行中(例如嵌套它们),所以我尝试了这个:

   <Target Name="BeforeBuild">
       <Target Name="CheckIfShouldKillVBCSCompiler">
         <PropertyGroup>
           <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
         </PropertyGroup>
       </Target>
   </Target>

但我最终遇到了错误: error MSB4067: The element &lt;PropertyGroup&gt; beneath element &lt;Target&gt; is unrecognized.

【问题讨论】:

  • 你有没有试过看看它是否有效?输出是什么?
  • @DanCsharpster 刚试了一下,出现错误:error MSB4067: The element &lt;PropertyGroup&gt; beneath element &lt;Target&gt; is unrecognized.
  • 与这种愚蠢的单路径Linux 的东西相比,您应该更轻松地引用编译器nuget 包,它将在您的项目中克隆Roslyn 编译器并使用它。
  • 这里的答案可能对你有用。 social.msdn.microsoft.com/Forums/vstudio/en-US/…
  • @Blindy 很有趣,你能详细说明一下吗?

标签: c# csproj


【解决方案1】:

我从this answer 那里得知这是无法实现的。

目标不能嵌套在另一个目标中。相反,可以像这样修改目标:

 <Target Name="CheckIfShouldKillVBCSCompiler"  BeforeTargets="build">
   <PropertyGroup>
     <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
   </PropertyGroup>
 </Target>

但是,这无助于解决我最初遇到的构建问题。所做的是发现了另一种方法here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多