Optimus
Optimus

Reputation: 2210

How to call second target in Msbuild

I need to call the second target in the msbuild but when I'm calling it in the cmd it shows error my code is give below

MsBuild.csproj

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <alen>123456</alen>
  </PropertyGroup>

  <Target Name="FirstTarget">
    <Message Text="Hello World $(alen)" />
  </Target>
  <Target Name="SecondTarget">
    <Message Text="The second target" />
  </Target>


</Project>

The first target called successfully but I cant load the second Target...How it is possible???

Upvotes: 3

Views: 4389

Answers (4)

M Townsend
M Townsend

Reputation: 171

I know this is a really old post, but you can also have one target call other targets.

  <Target Name="Build">
    <CallTarget Targets="PreBuild"/>
    <CallTarget Targets="Main"/>
    <CallTarget Targets="AfterBuild"/>
  </Target>

Upvotes: 8

MikeR
MikeR

Reputation: 3075

Another option is to define a default target in your build file and than define the order of targets using the DependsOnTargets:

<Project DefaultTargets="DefaultTarget" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<Target Name="DefaultTarget" DependsOnTargets="FirstTarget;SecondTarget">
  <Message Text="Executing DefaultTarget" />
</Target>

<!--  your targets -->

</Project>

The targets defined in DependsOnTargets will run before the target itself is running. Doing it this way, you do not need to set the /t: parameter in your call.

Upvotes: 2

Mike Zboray
Mike Zboray

Reputation: 40838

Since you have not defined it, the default target is the first target in the file, FirstTarget. To call the second target from the command line you need call it explicitly with /t:SecondTarget. You can use /t:FirstTarget;SecondTarget if you want to run both.

You could also define SecondTarget to always come after first target. Use the AfterTargets attribute like so:

  <Target Name="SecondTarget" AfterTargets="FirstTarget">

Now msbuild msbuild.proj would call both targets.

Upvotes: 8

Aleksei Poliakov
Aleksei Poliakov

Reputation: 1332

Have you tried

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe "D:\test_2\MsBuild\MsBuild\BuildScript\MsBuild.csproj" /t:SecondTarget

?

Upvotes: 3

Related Questions