How to add a class attribute to a block? How can I add a class attribute to a Drupal 8 block?

There are many examples in Drupal core such as the help modules that sets the role attributes for the block. So following that I can succesfully add a class.

/**
 * Implements hook_preprocess_HOOK() for block templates.
 */
function mymodule_preprocess_block(&amp;$variables) {
  if ($variables['plugin_id'] == 'mymodule_my_block') {
    $variables['attributes']['class'][] = 'my-nice-block';
  }
}


Is it possible to add a class without the preprocess hook? 
Ideally within the block plugin file located in src/Plugin/Block/ ?

This would help keeping all the code related to one block contained in one location.
Have you tried to just set #attributes on the render array returned by our block plugin? 

$build['#attributes']['class'][] = 'my-nice-block';


Looking at BlockViewBuilder::preRender(), that should work.