5

我有一个非常简单的 MSBuild 脚本,可以构建一堆 .sln 文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- Builds all *.sln files under this repository. -->
    <ItemGroup>
        <SolutionFiles Include="**/*.sln" />
    </ItemGroup>


    <Target Name="Build">
        <MSBuild Projects="@(SolutionFiles)" Targets="Rebuild" />
    </Target>   

    <Target Name="AfterBuild">
        <Message Text="After Build" />
    </Target>

    <Target Name="AfterRebuild">
        <Message Text="After Rebuild" />
    </Target>   

</Project>

AfterBuild/AfterRebuild 目标应该做其他事情,我现在只是在测试它们。

我希望在每个项目构建后触发这些目标,但这些目标不会被触发。

难道我做错了什么?

编辑:由于每个项目都定义了自己的 AfterBuild 目标,我想这种方式不会真正起作用。我尝试将AfterBuildAfterRebuild目标放在它们自己的文件 (custom.targets) 中,并使用 / p:CustomAfterMicrosoftCommonTargets =custom.targets 运行 MSBuild。这也没有奏效。

有什么建议么?

4

1 回答 1

8

您必须<Import Project="MyCommon.proj" />Microsoft.*.targets. 因为AfterBuild定义在Microsoft.*.targets

它实际上记录在每个项目文件中。

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- 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>

像这样导入您的自定义或常见目标:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MyBuildRoot)\Common.targets" />

你甚至可以覆盖OutputPathIntermediateOutputPath. 但它们必须先导入Microsoft.CSharp.targets。否则,它们将不会被 中定义的目标正确处理Microsoft.CSharp.targets

例子

Common.props

<PropertyGroup>
  <DocumentationFile></DocumentationFile> <!-- disables xml-doc generate -->
  <ProjectRootPath>$(MSBuildThisFileDirectory)</ProjectRootPath>
</PropertyGroup>

<PropertyGroup Condition="$(BuildInOnePlace)!=''">
  <BaseIntermediateOutputPath>$(ProjectRootPath)obj/<BaseIntermediateOutputPath>
  <BaseOutputPath>$(ProjectRootPath)bin/<BaseOutputPath>
</PropertyGroup>

<PropertyGroup Condition="$(BuildInOnePlace)==''">
  <BaseIntermediateOutputPath>obj/<BaseIntermediateOutputPath>
  <BaseOutputPath>bin/<BaseOutputPath>
</PropertyGroup>

<PropertyGroup>
  <OutputPath>$(BaseOutputPath)$(Configuration)/</OutputPath>
  <IntermediateOutputPath>$(BaseOutputPath)$(Configuration)/</IntermediateOutputPath>
</PropertyGroup>

公共目标

<Target Name="AfterBuild">
  <Message Text="$(ProjectName): $(OutputPath)" />
</Target>

子项目1\子项目1.csproj

...
<Import Project="../Common.props" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="../Common.targets" />
...

子项目2\子项目2.csproj

...
<Import Project="../Common.props" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="../Common.targets" />
...
于 2014-01-22T20:21:15.607 回答