Executable Javadoc Code Snippets

java kotlin documentation October 18, 2021

Gunnar Morling posted “Executable JavaDoc Code Snippets”, describing a way you can use the Maven Javadoc Plugin to embed code straight from your source tree into Javadoc comments.

He provides an example of the Maven configuration (it’s just configuring the Javadoc plugin) and here’s an example of the source for the Javadoc comment:

package snippets; 

import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;

public class CustomProviderSnippet {

  public void customProvider() {
    // @start region="provider" 
    ACMEConfiguration configuration = Validation
        .byProvider(ACMEProvider.class)
        .providerResolver( new MyResolverStrategy() )
        .configure();
    ValidatorFactory factory = configuration.buildValidatorFactory();
    // @end 
  }
}

And here;s how you’d refer to it in Javadoc:

/**
 *  {@snippet class="snippets.CustomProviderSnippet" region="provider"}
 */

His examples are more complete and contain explanations, of course; they’re just repeated here to show the value he’s describing.

in java kotlin documentation javadoc annotation

Reading time: 1 minute.