Recently saw a short of the always impressive [1] José Paumard.
It shows how to add snippets of code to your javadoc, and also add those snippets of code to your test suite.
I didn't know how to add them in Maven, but the reference [2] was be helpful.
So, first of all, we need, like, "production code" that refers to code snippets in the JavaDoc. In the example below there are both an inline snippet as well as a reference to a snippet elsewhere.
Then we have to define our external snippet for getAddress() as referred to above, inside AddressTest with appropriate zones.
Now this AddressTest is part of a Maven project, and is available in the src/test/java directory, as you would expect. So we need to provide this directory in the pom.xml via --snippet-path, as follows:
In the javadoc, when you generate it using "mvn javadoc:javadoc" it will look as follows:
The great part is that the Unit test also is regularly run.
I like it a lot!
P.S. the @snippet and assorted JavaDoc tags have a plethora of different options. For example for highlighting code.
References
- [1] How can you add a snippet of code in your JavaDoc? - Cracking the Java Coding Interview
- https://www.youtube.com/shorts/ZNe_-Z1qxp8
- [2] Configuring Maven For Compiled And Tested Code In Javadoc
- https://nipafx.dev/javadoc-snippets-maven/
- [3] Oracle Java - 3 Programmer's Guide to Snippets
- https://docs.oracle.com/en/java/javase/22/javadoc/programmers-guide-snippets.html
- Baeldung - Code Snippets in Java API Documentation
- https://www.baeldung.com/java-doc-code-snippets

No comments:
Post a Comment