简体   繁体   中英

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

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.

Have you tried

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

?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM