views:

38

answers:

1

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.
A: 

Target names must be fixed values, so what you have here wouldn't work.

Also I would recommend not using Batching Expressions inside of the DependsOnTargets expression as well. This could lead to strange behavior if you do not fully understand what is happening.

In your case you may be able to just create a "driver" target which uses those items to perform the build. The only difficult part would be the DependsOnTargets that you are trying to perform. I'm not sure about the details on what you are trying to do with that so cannot make any suggestions but as for the other take a look at creating a target similar to.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;

  <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="Build">
    <!-- 
    This will be executed once for every unique value of Project in the 
    Target item group 
    -->
    <MSBuild Projects="%(Targets.Project)"
             Properties="Configuration=$(Configuration)"
  </Target> 
</Project>
Sayed Ibrahim Hashimi
Hi, thanks for the prompt reply. I have modified my question to clarify a few bits. BTW, I am already using the structure you suggest for the clean target, because I usually clean all the projects. But I need an ability to build individual projects and I do not see how your solution allows me to do so, let alone it ignores the dependencies.
mark