1

I am struggling with seemingly very basic task at the moment, that is addding a new custom target to csproj file and run it from the command line using msbuild.

I did an extensive research on the net but I found no solution that is actually working.

Let's say that I add the following target to my csproj file:

  <Target Name="TeamCity">
    <Message Text="I am Running!"/>
  </Target>

or even something that depends on Build:

  <Target Name="TeamCity" DependsOnTargets="Build">
    <Message Text="I am Running!"/>
  </Target>

This is what msbuild documentation suggests.

But running the target seems to be a mission impossible. While I am able to run predefined target on csproj:

msbuild MySolution.sln /t:MyProject:Rebuild /p:Configuration="Release" /p:Platform="Any CPU"

I am not able to run the target I just added - that is TeamCity target:

msbuild MySolution.sln /t:MyProject:TeamCity /p:Configuration="Release" /p:Platform="Any CPU"

I always get error MSB4057: The target "TeamCity" does not exist in the project.

What is the deeply kept secret to make this running?

PS. Please note that I need the task to be working on a project level not on a solution. And I need to run msbuild MySolution.sln ... not as many incorrectly suggest msbuild MyProject.csproj ...

Radek Strugalski
  • 560
  • 7
  • 22

1 Answers1

0

The secret is pretty simple - you can't make this running.

Because, msbuild generates intermediate project file (YourSolution.sln.metproj) but that will not have the imports from YourProject.csproj, including the .targets files. That's why YourCustomTarget is not recognized.

What you can try is using Before/After Targets to inject your targets in predefined build flow.

Hope it will help.

MaKCbIMKo
  • 2,800
  • 1
  • 19
  • 27