Is it possible to refer to metadata of the target from within the target implementation in MSBuild?

Posted by mark on Stack Overflow See other posts from Stack Overflow or by mark
Published on 2009-12-13T16:08:18Z Indexed on 2010/03/22 12:01 UTC
Read the original article Hit count: 262

Filed under:
|

Dear ladies and sirs.

My msbuild targets file contains the following section:

<ItemGroup>
  <Targets Include="T1">
    <Project>A\B.sln"</Project>
    <DependsOnTargets>The targets T1 depends on</DependsOnTargets>
  </Targets>
  <Targets Include="T2">
    <Project>C\D.csproj"</Project>
    <DependsOnTargets>The targets T2 depends on</DependsOnTargets>
  </Targets>
  ...
</ItemGroup>
<Target Name="T1" DependsOnTargets="The targets T1 depends on">
  <MSBuild Projects="A\B.sln" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="T2" DependsOnTargets="The targets T2 depends on">
  <MSBuild Projects="C\D.csproj" Properties="Configuration=$(Configuration)" />
</Target>

As you can see, A\B.sln appears twice:

  1. As Project metadata of T1 in the ItemGroup section.
  2. In the Target statement itself passed to the MSBuild task.

I am wondering whether I can remove the second instance and replace it with the reference to the Project metadata of the target, which name is given to the Target task?

Exactly the same question is asked for the (Targets.DependsOnTargets) metadata. It is mentioned twice much like the %(Targets.Project) metadata.

Thanks.

EDIT:

I should probably describe the constraints, which must be satisfied by the solution:

  1. I want to be able to build individual projects with ease. Today I can simply execute msbuild file.proj /t:T1 to build the T1 target and I wish to keep this ability.
  2. I wish to emphasize, that some projects depend on others, so the DependsOnTargets attribute is really necessary for them.

© Stack Overflow or respective owner

Related posts about msbuild

Related posts about metadata