I like the RAML syntax because it's text based, versatile, and easy to use.
However, when I publish a REST API, I must provide readable API documentation or no one will use my API.
This blog post describes how can we solve that problem. We will learn how we can generate HTML documentation from RAML documents by using Maven.
Let's start by installing the required tools.
Installing the Required Tools
We can transform RAML documents into HTML by using an utility called raml2html. We can install it by following these steps:
First, the easiest way to install raml2html is to use NPM. In other words, we have to install NPM before we can install raml2html. We can do this by following the instructions given by the Node.js reference documentation.
Second, after we have installed Node.js, we can install raml2html. Because we want to use it as a command line tool, we have to install it globally. We can do this by running the following command at the command prompt:
npm install -g raml2html
After we have installed raml2html, we can generate HTML from a single RAML document by running the following command at the command prompt:
raml2html -i [input file path] -o [output file path]
We have now installed all the required tools. Let's move on and configure our Maven build.
Configuring Our Maven Build
Our next step is to invoke the raml2html application with Maven. We want to invoke the raml2html application when we package our Maven project because this way it doesn't cause unnecessary delays to compilation or unit testing.
We can configure our Maven build by following these steps:
- Add the Exec Maven Plugin into the plugins section of our pom.xml file.
- Create an execution that invokes the plugin's exec goal when the package phase of the Maven default lifecycle is invoked.
- Ensure that the Exec Maven Plugin executes the raml2html application.
- Configure the location of the input file (src/docs/api.raml) and the location of the output file (target/api.html).
The build section of our pom.xml file looks as follows:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.4.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>raml2html</executable> <commandlineArgs>-i src/docs/api.raml -o target/api.html</commandlineArgs> </configuration> </plugin> </plugins> </build>
We can now create the HTML API documentation by running the following command at the command prompt:
mvn clean package
This transforms the src/docs/api.raml file into HTML and writes the created HTML to the target/api.html file. The API documentation of our example application looks as follows:
Let's summarize what we learned from this blog post.
This blog post has taught us two things:
- We can transform RAML documents into HTML by using raml2html.
- We can invoke raml2html during our Maven build by using the Exec Maven Plugin.