您必须<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" />
你甚至可以覆盖OutputPath
和IntermediateOutputPath
. 但它们必须先导入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" />
...