Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Item Batching from executing a batch when there are zero items?

Tags:

msbuild

Execute this with msbuild:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <Colors Include="Blue">
      <Shade>Dark</Shade>
    </Colors>
  </ItemGroup>

  <Target Name="Main">
    <Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
  </Target>

</Project>

And it outputs:

Color: Dark Blue

All well, and good, but delete the color and use this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
  </ItemGroup>

  <Target Name="Main">
    <Message Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
  </Target>

</Project>

And it outputs:

Color:

Why is one batch of the Message task being executed when there are no items in the group? I would have expected for zero items, the batch would execute zero times and I would not see "Color:" followed by nothing in the output.

Am I doing something wrong or is there a workaround for this?

Thanks.


Update: I've found you can do:

<Message Condition="'@(Colors)'!=''" Text="Color: %(Colors.Shade) %(Colors.Identity)"/>

But, if feels unsatisfactory to have to explicitly write code for the case when there are no items every time batching is used.

like image 767
Scott Langham Avatar asked Nov 04 '10 11:11

Scott Langham


People also ask

How do I stop batch processing?

You can insert the pause command before a section of the batch file that you might not want to process. When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.

Which interface provides the stop method to stopping a job manually?

Stopping a Job Manually for Business Reasons. Spring Batch provides a stop () method through the JobLauncher interface, but this is really for use by the operator rather than the application programmer. Sometimes it is more convenient or makes more sense to stop a job execution from within the business logic.

How do I stop a Spring Batch job?

The method is stop(long executionId) You would have to use JobExplorer to find the correct executionId to stop. Also from within a job flow config you can configure a job to stop after a steps execution based on exit status (see https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#stopElement).

What is batch processing mode?

Batch processing is a technique for automating and processing multiple transactions as a single group. Batch processing helps in handling tasks like payroll, end-of-month reconciliation, or settling trades overnight.


1 Answers

My 2 cents :

In your Message Task, there is information from Batching and static information ("Colors :"). I think MsBuild prints the static information and then batch over the values of your Colors Item. The probleme is that you don't have any data in your collection (it is even undeclared), I suppose MsBuild interpretates this as an empty list, which, when you try to print it, print the empty string ''.

If you remove the static content ("Colors :" and the whitespace before identity), you won't have anything displayed.

A solution for printing with batching only if the items collection is not empty would be either :

  1. Check if the collection is empty

    <Message Condition="'@(Colors)'!=''" Text="Color: %(Colors.Shade) %(Colors.Identity)"/>
    
  2. Use MSBuild transforms

    <Message Text="@(Colors->'Color : %(Shade) %(Identity)')"/>
    
like image 139
Benjamin Baumann Avatar answered Oct 08 '22 15:10

Benjamin Baumann