AppendTargetFrameworkToOutputPath 在 .targets 文件中失败

2024-02-19

我正在尝试集中许多项目的一些构建配置,而 MSBuild 解决方案是使用.targets files https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-dot-targets-files.

我想应用于所有项目的一种配置是防止netcoreapp2.1附加到所有输出目录。要实现这一点,只需包含这一行配置即可:

<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>

但是,当我将这条线放入Common.targets文件,并将该文件包含在我的.csproj文件使用<Import Project="PathToCommonTargets" />,那么它没有任何效果,我仍然得到netcoreapp2.1在我的输出文件夹的末尾。

我该如何解决这个问题?

原因更新:我们是一家是其他 10 多家公司的技术合作伙伴的公司。也就是说,我们有一个基础设施的中央源代码控制(假设映射到C:\Infra),并且对于每个公司都有一个不同的源代码控制(假设映射到C:\CompanyA and C:\CompanyB等),其中可能包含最多 100 个解决方案,每个解决方案可能包含 10 多个项目。因此,我们的首要要求是通过干燥所有内容(从 Angular 到 Java、Swift 和 .NET)来尽可能降低成本。基于持续改进的精神,我们不希望我们的产出投入\bin\Debug\netcoreapp2.1超过 2000 个项目文件(并且还在不断增加),以减少开发人员花在输出文件夹上的时间,并使重构到更高版本的 .NET 变得更容易、更便宜。升级自netcoreapp2.0 to netcoreapp2.1由于自动化中的输出路径,我们做了噩梦。这就是为什么我们尝试使用AppendTargetFrameworkToOutputPath。但我真的很沮丧,为什么事情会如此具有挑战性。对我来说,MSBuild 应该首先加载所有内容,然后开始构建。这样,布尔属性将在构建管道期间随处出现,并且不需要如此复杂的配置。毕竟,如果布尔属性需要超过true/false要配置它吗?


AppendTargetFrameworkToOutputPath 在 .targets 文件中失败

该物业AppendTargetFrameworkToOutputPath可以阻止 MSBuild 追加TargetFramework to OutputPath,但是因为TargetFramework我们创建项目时已经生成了文件夹,我们需要手动删除TargetFramework文件夹添加后<Import Project="PathToCommonTargets" />到项目中,否则该文件夹将永远存在,那么我们就可以构建项目而无需TargetFramework to OutputPath.

If the TargetFramework删除后仍然生成文件夹,应将其内容加倍.targets并且进口声明是正确的。为了确保它们,我在.target file:

<?xml version="1.0" encoding="utf-8"?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>    
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> 
  </PropertyGroup>

  <Target Name="Test" AfterTargets="Build">
    <Message Text="This is Test Custom Target!"></Message>
  </Target>

</Project>

当我们构建项目时,我们可以在输出窗口中看到消息信息,这意味着内容.targets并且进口声明是正确的。

Update:

您能帮我了解如何删除 TargetFramework 吗?

要实现此目的,请卸载您的项目。然后在项目的最后,就在结束标记之前</project>,放置以下脚本:

<Target Name="RemoveDirectories" AfterTargets="Build">  
    <RemoveDir  
        Directories="$(ProjectDir)$(OutDir)" /> 
</Target> 

此外,您需要备份文件TargetFramework到 bin\debug 或 bin\release 文件夹,所以我们还需要在删除任务之前复制任务:

<ItemGroup>  
    <MySourceFiles Include="$(ProjectDir)$(OutDir)\*.*"/>  
</ItemGroup> 

<Copy  
    SourceFiles="@(MySourceFiles)"  
    DestinationFolder="$(ProjectDir)bin\$(ConfigurationName)"  
 /> 

 <RemoveDir  
    Directories="$(ProjectDir)$(OutDir)" />  

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AppendTargetFrameworkToOutputPath 在 .targets 文件中失败 的相关文章

随机推荐