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

Deadly 提交于 2019-12-22 05:15:12

问题


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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/8404114/how-make-msbuild-build-custom-target-specified-in-csproj-building-sln

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!