Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overridden AfterBuild target in MSBuild script is not executed

I have a very simple MSBuild script that builds a bunch of .sln files:

<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>

The AfterBuild/AfterRebuild targets should do something else, i am just testing them now.

I'd like these targets to fire after every project build, but these are not fired.

Am i doing something wrong?

EDIT: Since each project defines its own AfterBuild target, i guess this way wouldn't really work. I tried placing the AfterBuild and AfterRebuild targets in their own file (custom.targets) and running MSBuild with /p:CustomAfterMicrosoftCommonTargets=custom.targets. This also did not work.

Any suggestions?

like image 811
lysergic-acid Avatar asked Dec 20 '12 12:12

lysergic-acid


1 Answers

You have to add an <Import Project="MyCommon.proj" /> in every Project after the Microsoft.*.targets. Because AfterBuild is defined in Microsoft.*.targets

It's actually documented in every project-file.

<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 your custom or common targets like this:

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

you can even overwrite OutputPath and IntermediateOutputPath. But they have to be imported before Microsoft.CSharp.targets. Otherwise they will not correctly processed by targets defined in Microsoft.CSharp.targets.

Example

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>

Common.targets

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

SubProject1\SubProject1.csproj

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

SubProject2\SubProject2.csproj

...
<Import Project="../Common.props" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="../Common.targets" />
...
like image 119
mo. Avatar answered Oct 17 '22 05:10

mo.