How to use the console output in MSBuild Target (Exec)

tags: .NET  .NET Core  MSBuild  

 

I once wrote an articleHow to create a cross-platform NuGet toolkit based on command line tools, Participate in the compilation process by writing a console program. But compared toTask-based approach, The controllable factors are still too few.

Is there any way to enable more information exchange between the console program and MSBuild Target? The answer is yes, by capturing the output of the console!


 

Capture console output

If you like reading documents, then the answer is no longer unfamiliar, in Microsoft's official documentsExec Task Attributes are already mentioned inConsoleToMSBuild. Set this property toTrue, Will be able to capture the console output to MSBuild. (But it is said that typical programmers do not like to read documents

So, where did the captured output go?

I'm here How to create a cross-platform NuGet toolkit based on MSBuild Task Mentioned in usingOutput Coming willTask The parameters in are output. WhileExec Do the same. We will ConsoleOutput Just output it. Since this attribute is notITaskItem[] Type, so we can only get string attributes, so we can only passPropertyName To receive such output.

<Exec ConsoleToMSBuild="True" Command="&quot;$(NuGetWalterlvToolPath)&quot;">
  <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfTheCommand" />
</Exec>

PropertyGroup to ItemGroup

If all you need is a string, that's enough after reading the previous section. But if what you want is a set of values ​​(for example, a new set of files that need to be compiled), then what you need to get isItemGroup Multiple values ​​in instead ofPropertyGroup A single value in. (If you don’t understandItemGroup with PropertyGroup The difference between it does not matter, you can readUnderstand the essence of the C# project csproj file format and compilation process。)

MSBuild also comes with aTask, NamedCreateItem, Is to create a group from a stringItem. With the following code, we can convert the properties captured in the previous section into a collection of items.

<CreateItem Include="$(OutputOfTheCommand)">
  <Output TaskParameter="Include" ItemName="AdditionalCompile" />
</CreateItem>

In this way, we can

A more complete code may have more reference significance, so I posted it below:

<Project>
  <Target Name="GenerateAdditionalCode" BeforeTargets="CoreCompile">
    <Exec ConsoleToMSBuild="True" Command="&quot;$(NuGetWalterlvToolPath)&quot;">
      <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfTheCommand" />
    </Exec>
  </Target>
  <Target Name="_IncludeGeneratedAdditionalCode" AfterTargets="GenerateAdditionalCode">
    <CreateItem Include="$(OutputOfTheCommand)">
      <Output TaskParameter="Include" ItemName="AdditionalCompile" />
    </CreateItem>
    <ItemGroup>
      <Compile Include="@(AdditionalCompile)" />
    </ItemGroup>
    <Message Text="Additional compilation file: @(AdditionalCompile)" />
  </Target>
</Project>

Conversion separator for CreateItem

CreateItem Going from an attribute or string to an item is distinguished by the separator. Due to use@(Item) To get the item, you will get a; Separated strings, so it is not difficult to think of the string output from our console using; Separation can meet our conversion needs.But in fact this is not possible!

Because of the conversion of the console, there is a buffer limit for each line, that is to say, the number of words in a single line cannot be too much, otherwise a newline character will be automatically added-this may cause one or more items we converted to have a newline character in the middle , Resulting in an error.

Therefore, it is recommended to directly use the newline character itself as a separator in the console program, so that this restriction can be removed. Because ofCreateItem It also supports line breaks.


Reference


Intelligent Recommendation

Use logging in Python to write logs or output to the console

1. If the module is executed directly, __ name__ is '__main__' 2. If the module is imported, the value of __ name__ is the module name  ...

Use log4cxx to output information to Console in the GUI program

I saw that there is a method to implement in the project properties settings. Take VS2010 as an example: Right-click Project Select Properties-> Configuration Properties-> Build Events-> Post...

How to use @output () in Angular

HTML file in subcomponent TS file in subcomponent HTML in Parent Component Parent Component in TS  ...

Use of msbuild to avoid killing payload

0x00 Preface One of the necessary skills for the red team to avoid killing, is now mainly implemented under .net4.0. Wait for me to have time to implement .net2.0 in a few days. 0x01 Free killing proc...

msbuild

Msbuild is a build platform under WINDOWS. (linux: mono xbuild; dotnetcore build) Contains two parts 1. Standardized script description language 2. msbuild command Basic concept The msbuild script fil...

More Recommendation

MSBuild

aa\DispatchCar.sln bb\DispatchCar.sln      ...

MSBuild

 ...

MSBuild

introduction We will be natural when using the Visual Studio IDE development projectRun / F5 / Right-click Project - Generate / Regenerate / Clean, Then you can see the corresponding result. These res...

MSBuild

All resource files under the project are copied to the operating directory, together with the directory structure of the resource file. Method 1: Set the production operation of the resource file as t...

How VC MFC uses Console to output debugging information..

Step 1: Add the following code in the CPP file where CXXXXApp is located: Then, call LauchConsole in CXXXXApp::InitInstance() to output information using cout or printf....

Related Posts

Popular Posts

Recommended Posts

Related Tags