Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make MSBuild build custom target specified in csproj building sln?

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!

like image 497
nbulba Avatar asked Dec 06 '11 17:12

nbulba


People also ask

How do you build specific targets in solutions using MSBuild EXE?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.

Will MSBuild compile without any target?

If there are no initial targets, default targets, or command-line targets, then MSBuild runs the first target it encounters in the project file or any imported project files.

How do I change properties in MSBuild?

MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.

What are MSBuild targets?

A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output. If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. This is called an incremental build of the target.


2 Answers

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
like image 60
Raul Nohea Goodness Avatar answered Oct 01 '22 00:10

Raul Nohea Goodness


One option is to tie your SampleTarget to the standard Build targets via overriding the appropriate DependsOn property. In this case you could tell BeforeBuild that it DependsOn SampleTarget or you do the same thing with AfterBuild. This will ensure that MSBuild processes your target prior to the standard target indicated.

like image 25
Nick Nieslanik Avatar answered Oct 01 '22 01:10

Nick Nieslanik