通过任务选择在目标中生成的ItemGroup文件的位置

7

我有以下的设置(为了简洁起见,无关紧要的XML已被删除):

MyProject.fsproj

<Project ...>
  <Import Project="MyTask.props" />
  ...
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

MyTask.props

<Project ...>
  <UsingTask XXX.UpdateAssemblyInfo />
  <Target Name="UpdateAssemblyInfo"
          BeforeTargets="CoreCompile">
    <UpdateAssemblyInfo ...>
      <Output
        TaskParameter="AssemblyInfoTempFilePath"
        PropertyName="AssemblyInfoTempFilePath" />
    </UpdateAssemblyInfo>

    <ItemGroup>
      <Compile Include="$(AssemblyInfoTempFilePath)" />
    </ItemGroup>
  </Target>
</Project>

问题在于MyTask.props添加的ItemGroup被添加到了最后,尽管它是在项目一开始就导入的。我认为这是因为ItemGroup实际上并没有被导入,而是在运行任务时才被添加。
在F#中,这不是一个好的事情,因为文件顺序很重要 - 将文件包含在构建列表的末尾意味着无法构建EXE,例如(因为入口点必须在最后一个文件中)。
因此我的问题是 - 是否有办法让我输出一个ItemGroup作为目标的一部分,并使生成的ItemGroup排在第一位?
1个回答

2

可能有些晚了,但这可能会帮助未来的某个人。在此示例中,我没有使用import标签,但它将以相同的方式工作,重要的部分是"UpdateAssemblyInfo"目标,主要思想是清除并使用适当的排序顺序重新生成Compile ItemGroup。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>

  <Target Name="Build" DependsOnTargets="UpdateAssemblyInfo">

  </Target>

  <Target Name="UpdateAssemblyInfo">
    <!-- Generate your property -->
    <PropertyGroup>
      <AssemblyInfoTempFilePath>ABC.xyz</AssemblyInfoTempFilePath>
    </PropertyGroup>

    <!-- Copy current Compile ItemGroup to TempCompile -->
    <ItemGroup>
      <TempCompile Include="@(Compile)"></TempCompile>
    </ItemGroup>

    <!-- Clear the Compile ItemGroup-->
    <ItemGroup>
      <Compile Remove="@(Compile)"/>
    </ItemGroup>

    <!-- Create the new Compile ItemGroup using the required order -->    
    <ItemGroup>
      <Compile Include="$(AssemblyInfoTempFilePath)"/>
      <Compile Include="@(TempCompile)"/>
    </ItemGroup>

    <!-- Display the Compile ItemGroup ordered -->
    <Message Text="Compile %(Compile.Identity)"/>
  </Target>
</Project>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接