gpt4 book ai didi

MSBuild ItemGroup 包含/排除模式问题

转载 作者:行者123 更新时间:2023-12-02 06:41:04 26 4
gpt4 key购买 nike

问题:未根据排除属性中传递的值正确构建 ItemGroups 数组。

如果您运行此脚本,它会创建一些示例文件,然后尝试根据包含/排除属性创建一个名为 TheFiles 的数组,问题是当排除不是硬编码或非常简单的属性时,它会出错。

目标 DynamicExcludeList 错误地选择了这些文件:
.\AFolder\test.cs;.\AFolder\test.txt

目标 HardcodedExcludeList 正确选择这些文件:
.\AFolder\test.txt

非常感谢任何帮助,这让我发疯。

(注意其 msbuild v4)

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

<Target Name="Run" >
<CallTarget Targets="CreateSampleFiles" />
<CallTarget Targets="DynamicExcludeList" />
<CallTarget Targets="HardcodedExcludeList" />
</Target>

<Target Name="CreateSampleFiles" >
<MakeDir Directories="AFolder" />
<WriteLinesToFile Lines="Test" File="AFolder\test.cs" Overwrite="true" />
<WriteLinesToFile Lines="Test" File="AFolder\test.txt" Overwrite="true" />
</Target>

<Target Name="DynamicExcludeList" >

<PropertyGroup>
<CommonFileExclusion>.\DIRECTORY_NAME_TOKEN\**\*.cs</CommonFileExclusion>
<FinalExcludes>$(CommonFileExclusion.Replace('DIRECTORY_NAME_TOKEN', 'AFolder'))</FinalExcludes>
</PropertyGroup>

<Message Text="FinalExcludes: $(FinalExcludes)" />
<ItemGroup>
<TheFiles
Include=".\AFolder\**\*;"
Exclude="$(FinalExcludes)"
/>
</ItemGroup>
<Message Text="TheFiles: @(TheFiles)" />

</Target>

<Target Name="HardcodedExcludeList" >

<PropertyGroup>
<FinalExcludes>.\AFolder\**\*.cs</FinalExcludes>
</PropertyGroup>

<Message Text="FinalExcludes: $(FinalExcludes)" />
<ItemGroup>
<TheFilesWithHardcodedExcludes
Include=".\AFolder\**\*;"
Exclude="$(FinalExcludes)"
/>
</ItemGroup>
<Message Text="TheFilesWithHardcodedExcludes: @(TheFilesWithHardcodedExcludes)" />

</Target>
</Project>

这是输出,请注意“TheFiles”和“TheFilesWithHardcodedExcludes”之间的差异

PS C:\SVN\TrunkDeployment\TestMsBuild> msbuild .\Test.build.xmlMicrosoft (R) Build Engine Version 4.0.30319.1[Microsoft .NET Framework, Version 4.0.30319.1]Copyright (C) Microsoft Corporation 2007. All rights reserved.Build started 8/10/2010 2:30:42 PM.Project "C:\SVN\TrunkDeployment\TestMsBuild\Test.build.xml" on node 1 (default targets).DynamicExcludeList:  FinalExcludes: .\AFolder\**\*.cs  TheFiles: .\AFolder\test.cs;.\AFolder\test.txtHardcodedExcludeList:  FinalExcludes: .\AFolder\**\*.cs  TheFilesWithHardcodedExcludes: .\AFolder\test.txtDone Building Project "C:\SVN\TrunkDeployment\TestMsBuild\Test.build.xml" (default targets).Build succeeded.    0 Warning(s)    0 Error(s)Time Elapsed 00:00:00.06

EDITS

I've updated the above script to use the CreateItem, however there is still an issue when the list of items to exclude contains more than 1 path (i.e. the value of CommonFileExclusion has changed):

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

<Target Name="Run" >
<CallTarget Targets="CreateSampleFiles" />
<CallTarget Targets="DynamicExcludeList" />
<CallTarget Targets="HardcodedExcludeList" />
</Target>

<Target Name="CreateSampleFiles" >
<MakeDir Directories="AFolder" />
<WriteLinesToFile Lines="Test" File="AFolder\test.cs" Overwrite="true" />
<WriteLinesToFile Lines="Test" File="AFolder\test.txt" Overwrite="true" />
<WriteLinesToFile Lines="Test" File="AFolder\test.vb" Overwrite="true" />
</Target>

<Target Name="DynamicExcludeList" >

<PropertyGroup>
<CommonFileExclusion>.\DIRECTORY_NAME_TOKEN\**\*.cs;.\DIRECTORY_NAME_TOKEN\**\*.vb;</CommonFileExclusion>
<FinalExcludes>$(CommonFileExclusion.Replace('DIRECTORY_NAME_TOKEN', 'AFolder'))</FinalExcludes>
</PropertyGroup>

<Message Text="FinalExcludes: $(FinalExcludes)" />
<CreateItem Include=".\AFolder\**\*;"
Exclude="$(FinalExcludes)">
<Output TaskParameter="Include" ItemName="TheFiles"/>
</CreateItem>
<Message Text="TheFiles: @(TheFiles)" />

</Target>

<Target Name="HardcodedExcludeList" >

<PropertyGroup>
<FinalExcludes>.\AFolder\**\*.cs;.\AFolder\**\*.vb</FinalExcludes>
</PropertyGroup>

<Message Text="FinalExcludes: $(FinalExcludes)" />
<CreateItem Include=".\AFolder\**\*;"
Exclude="$(FinalExcludes)">
<Output TaskParameter="Include" ItemName="TheFilesWithHardcodedExcludes"/>
</CreateItem>
<Message Text="TheFilesWithHardcodedExcludes: @(TheFilesWithHardcodedExcludes)" />

</Target>
</Project>

最佳答案

好吧,我尝试了一点,我认为问题来自于您使用一个代表多个值的标量值的属性。我建议批处理和转换(请参阅 http://scottlaw.knot.org/blog/?p=402http://msdn.microsoft.com/en-us/library/ms171476.aspx )。例如,以下代码正在运行:

<Target Name="DynamicExcludeList" >
<ItemGroup>
<ExtensionsExcluded Include="cs;vb" />
</ItemGroup>

<CreateItem Include=".\AFolder\**\*"
Exclude="@(ExtensionsExcluded->'.\AFolder\**\*.%(identity)')">
<Output TaskParameter="Include" ItemName="TheFiles"/>
</CreateItem>
<Message Text="TheFiles: @(TheFiles)" />
</Target>

关于MSBuild ItemGroup 包含/排除模式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com