MSBuild ItemGroups中是否有索引器?

时间:2015-04-02 02:15:46

标签: visual-studio msbuild version post-build-event

我正在使用this stack overflow answer将构建的程序集@(Version)作为ItemGroup返回,以便在构建后事件中使用。

但是,我希望单独使用Major和Minor,因为我只想在帖子构建中使用vMajor.Minor。为此,我想在Include属性中进行索引。

以下情况有效,但如果主要或次要数字超过9(仅限个位数),则会中断:

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <ItemGroup>
    <Version Include="@(Targets->'%(Version)')" />
    <Major Include="@(Version->Substring(0,1))" />
    <Minor Include="@(Version->Substring(2,1))" />
  </ItemGroup>
</Target>
<PropertyGroup>
  <PostBuildEventDependsOn>
    $(PostBuildEventDependsOn);
    PostBuildMacros;
  </PostBuildEventDependsOn>
  <PostBuildEvent>
    if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v@(Major).@(Minor)$(TargetExt)
  </PostBuildEvent>
</PropertyGroup>

我正在寻找的东西更像是:

<Major Include="@(Version->Split('.')[0])" />
<Minor Include="@(Version->Split('.')[1])" />

或者,只有当索引器真的不可能时,

<Major Include="@(Targets->Version->Major)" />
<Minor Include="@(Targets->Version->Minor)" />

<Major Include="@(Targets->'%(Version)'->'%(Major)')" />
<Minor Include="@(Targets->'%(Version)'->'%(Minor)')" />

<Major Include="@(Targets->'%(Version.Major)')" />
<Minor Include="@(Targets->'%(Version.Minor)')" />

我需要使用哪些选项来索引数组,或者将1.0.2838.24877之类的版本号分开到10

修改

这现在有效,首先使用<PropertyGroup>我可以像this post一样使用Split('.')[n],但$(Major)属性($())不可用在Post-Build活动中(为什么不呢?我错过了什么?)。如果我可以在后期构建中直接使用$(Major),那将更加优雅。

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <PropertyGroup>
    <Version>@(Targets->'%(Version)')</Version>
    <Major>$(Version.Split('.')[0])</Major>
    <Minor>$(Version.Split('.')[1])</Minor>
    <Build>$(Version.Split('.')[2])</Build>
    <Revision>$(Version.Split('.')[3])</Revision>      
  </PropertyGroup>
  <ItemGroup>
    <Version Include="$(Version)" />
    <Major Include="$(Major)" />
    <Minor Include="$(Minor)" />
    <Build Include="$(Build)" />
    <Revision Include="$(Revision)" />
  </ItemGroup>
  <Message Text="Version: @(Version)" />
  <Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
  <PostBuildEventDependsOn>
    $(PostBuildEventDependsOn);
    PostBuildMacros;
  </PostBuildEventDependsOn>
  <PostBuildEvent>
    if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v@(Major).@(Minor)$(TargetExt)
  </PostBuildEvent>
</PropertyGroup>

为什么ItemGroups @()可用且PropertyGroups $()没有?

1 个答案:

答案 0 :(得分:1)

这是我遇到的最好的事情,很少了解在ItemGroups内可以使用的其他内容或如何使用索引器。

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <PropertyGroup>
    <Version>@(Targets->'%(Version)')</Version>
  </PropertyGroup>
  <ItemGroup>
    <Version Include="$(Version)" />
    <Major Include="$(Version.Split('.')[0])" />
    <Minor Include="$(Version.Split('.')[1])" />
    <Build Include="$(Version.Split('.')[2])" />
    <Revision Include="$(Version.Split('.')[3])" />
  </ItemGroup>
  <Message Text="Version: @(Version)" />
  <Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
  <PostBuildEventDependsOn>
    $(PostBuildEventDependsOn);
    PostBuildMacros;
  </PostBuildEventDependsOn>
  <PostBuildEvent>
    if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v@(Major).@(Minor)$(TargetExt)
  </PostBuildEvent>
</PropertyGroup>

如果可能,我希望能够在没有<PropertyGroup>部分的情况下完成此任务。