How make MSBuild build custom target specified in csproj building sln

msbuildmsbuild-task

I am facing an issue with MSBuild I can't overcome it by myself. As a result I rely on community's wisdom.

The real situation I'm having troubles with

I have a soluiton file containing several projects with dependencies to other projects in same solution. I'd like to append a custom target to one of the project's csproj file and build it from the command line. It will allow me to make all the necessary output binaries for this project for further processing during the building of the custom target. But the main thing is that I can't figure out how to do it, googling doesn't help either.

Simplification

To make thing simplier I decided to make a new C# console project, add a simple custom target to the project's file and try to make it build. Still no success! Here what I've done so far:

  1. Created a solution app with a default console project coreapp. This gaves me at least two files:

    • app.sln
    • coreapp\coreapp.csproj
  2. Modified coreapp.csproj with addition of my custom target inside of the Project tag

    <Target Name="SampleTarget">
      <Message Text="This is a SampleTarget" />
    </Target>
    
  3. Run on the command line the following command

    %windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp:SampleTarget

    or even

    %windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp.csproj:SampleTarget

Results

No luck, facing the error

MSB4057: The target "coreapp.csproj:SampleTarget" does not exist in the project.

I suspect that MSBuild thinks somehting fundamentally different from what I want it to think…

BEsides that, I also tried to set on the same command line the environment variable MSBuildEmitSolution=1 to force msbuild dump a temporary solution file it creates while processing the solution. In this file, indeed, no such target. However I guess it isn't the reason because I asked msbuild to build coreapp.proj where target SampleTarget really resides.

The question is how to build SampleTarget in this simplified scenario using solution file since potencially it can contain dependencies for the project containing this SampleTarget target?

I'd be greatful for any sort of help or firection for further investigation!

Best Answer

Instead of inserting a custom target in your project file, you could try creating a new standalone msbuild file, which would:

  • build the solution file (which builds projects)
  • defines your extra target

Call it app-custom-Debug.msbuild , for example.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WorkingFolder>$(MSBuildProjectDirectory)</WorkingFolder>
    <Configuration>Debug</Configuration>
    <SolutionFile>app.sln</SolutionFile>
  </PropertyGroup>

  <Target Name="Build" DependsOnTargets="Compile" />  

  <Target Name="Compile">
    <Message Text="=== COMPILING $(Configuration) configuration ===" />
    <MSBuild Projects="$(SolutionFile)"
             Properties="Configuration=$(Configuration)" />
  </Target>

  <Target Name="SampleTarget">
    <Message Text="This is a SampleTarget" />
  </Target>

</Project>

Then you execute:

msbuild.exe app-custom-Debug.msbuild /t:SampleTarget