The Asciidoctor Maven Plugin is the official way to convert your AsciiDoc documentation using Asciidoctor from an Apache Maven build.
The conversion can happen in 2 flavors:
-
as a Maven plugin: AsciiDoc files are converted at full Asciidoctor power independently from Maven site,
-
as a Maven site integration: AsciiDoc files are integrated with Maven reports, which comes with a few limitations (see below for details).
As this is a typical Maven plugin, simply declare the plugin in the <plugins>
section of your POM file:
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version> <!--(1)-->
...
</plugin>
</plugins>
-
As this plugin tracks the version of Asciidoctor, you can use whichever version of Asciidoctor you prefer.
<plugin>
...
<executions>
<execution>
<id>output-html</id> <!--(1)-->
<phase>generate-resources</phase> <!--(2)-->
<goals>
<goal>process-asciidoc</goal> <!--(3)-->
</goals>
</execution>
</executions>
</plugin>
-
This is simply an unique id for the execution
-
The asciidoctor-maven-plugin does not run in any phase by default, so one must be specified
-
The (only for the moment) Asciidoctor Maven plugin goal
There are several configuration options that the Asciidoctor Maven plugin accepts, which parallel the options in Asciidoctor:
- sourceDirectory
-
defaults to ${basedir}/src/main/asciidoc
๐
|
All paths and AsciiDoc documents that start with |
- sourceDocumentName
-
an override to process a single source file; defaults to all files in
${sourceDirectory}
- sourceDocumentExtensions
-
(named
extensions
in v1.5.3 and below) aList<String>
of non-standard file extensions to render. Currently ad, adoc, and asciidoc will be rendered by default - resources
-
list of resource files to copy to the output directory (e.g., images, css). The configuration follows the same patterns as the
maven-resources-plugin
. If not set, all resources inside${sourceDirectory}
are copied.๐Converters that embed resources such as images into the output document need to be able to locate those resources at conversion time. For example, when generating a PDF (or HTML with the
data-uri
attribute set), all images need to be aggregated under a common root (i.e., image catalog). Theimagesdir
attribute should be overridden to point to that folder. When converting to HTML, images must be copied to the output location so that the browser can resolve those images when the user views the page.<resources> <resource> <!-- (Mandatory) Directory to copy from. Paths are relative to maven's ${baseDir} --> <directory>DIRECTORY</directory> <!-- (Optional) Directory to copy to. By default uses the option `outputDirectory` --> <targetPath>OUTPUT_DIR</targetPath> <!-- (Optional) NOTE: SVN, GIT and other version control files are excluded by default, there's no need to add them --> <excludes> <exclude>**/.txt</exclude> </excludes> <!-- (Optional) If not set, includes all files but default exceptions mentioned --> <includes> <include>**/*.jpg</include> <include>**/*.gif</include> </includes> </resource> ... <resources>
- outputDirectory
-
relative paths are added to the project root path. Defaults to ${project.build.directory}/generated-docs.
- outputFile
-
defaults to
null
, used to override the name of the generated output file, can be a relative or absolute path. Useful for backends that create a single file, e.g. the pdf backend. All output will be redirected to the same file, the same way as the-o, --out-file=OUT_FILE
option from theasciidoctor
CLI command. - baseDir
-
(not Mavenโs basedir) enables to set the root path for resources (e.g. included files), defaults to
${sourceDirectory}
- skip
-
set this to
true
to bypass generation, defaults tofalse
- preserveDirectories
-
enables to specify whether the documents should be rendered in the same folder structure as in the source directory or not, defaults to
false
. Whentrue
, instead of generating all output in a single folder, output files are generated in the same structure. See the following exampleโโโ docs โโโ docs โ โโโ examples.adoc โ โโโ examples.html โ โโโ examples => โ โโโ examples โ โโโ html.adoc โ โโโ html.html โ โโโ docbook.adoc โ โโโ docbook.html โโโ index.adoc โโโ index.html
- relativeBaseDir
-
only used when baseDir is not set, enables to specify that each AsciiDoc file must search for its resources in the same folder (for example, included files). Internally, for each AsciiDoc source, sets
baseDir
to the same path as the source file. Defaults tofalse
- imagesDir
-
defaults to
images
, which will be relative to the directory containing the source files - backend
-
defaults to
docbook
- doctype
-
defaults to
null
(which triggerโs Asciidoctorโs default ofarticle
) - eruby
-
defaults to erb, the version used in JRuby
- headerFooter
-
defaults to
true
- templateDir
-
directory of Tilt-compatible templates to be used instead of the default built-in templates, disabled by default (
null
) - templateDirs
-
list of directories of Tilt-compatible templates to be used instead of the default built-in templates, empty by default
๐Order is not relevant for templateDir and templateDirs. Values set in both properties are merged together into a single final list.
- templateEngine
-
template engine to use for the custom converter templates, disabled by default (
null
) - templateCache
-
enables the built-in cache used by the template converter when reading the source of template files. Only relevant if the
:template_dir
option is specified, defaults totrue
- sourceHighlighter
-
enables and sets the source highlighter (currently
coderay
orhighlight.js
are supported) - sourcemap
-
adds file and line number information to each parsed block (
lineno
andsource_location
attributes), defaults tofalse
- catalogAssets
-
tells the parser to capture images and links in the reference table available via the
references
property on the document AST object (experimental), defaults tofalse
- attributes
-
a
Map<String,Object>
of attributes to pass to Asciidoctor, defaults tonull
- embedAssets
-
Embedd the CSS file, etc into the output, defaults to
false
- gemPaths
-
enables to specify the location to one or more gem installation directories (same as GEM_PATH environment var),
empty
by default - requires
-
a
List<String>
to specify additional Ruby libraries not packaged in AsciidoctorJ,empty
by default - extensions
-
List
of extensions to include during the conversion process (see AsciidoctorJโs Extension API for information about the available options). For each extension, the implementation class must be specified in theclassName
parameter, theblockName
is only required when configuring a BlockProcessor, BlockMacroProcessor or InlineMacroProcessor. Here follows a configuration example:<plugin> ... <executions> <execution> <configuration> ... <extensions> <extension> <className>org.asciidoctor.maven.SomePreprocessor</className> </extension> <extension> <className>org.asciidoctor.maven.SomeBlockProcessor</className> <blockName>yell</blockName> </extension> </extensions> </configuration> </execution> </executions> <dependencies> <dependency> <!--(1)--> <groupId>org.asciidoctor.maven</groupId> <artifactId>my-asciidoctor-extensions</artifactId> <version>1.0.0</version> </dependency> </dependencies> </plugin>
-
Note that processors must be included in the pluginโs execution classpath, not in the projectโs.
-
๐
|
Extensions can also be integrated through the SPI interface implementation. This method does not require any configuration in the pom.xml, see Extension SPI for details. |
- enableVerbose
-
enables Asciidoctor verbose messages, defaults to
false
. Enable it, for example, if you want to validate internal cross references and capture the messages with the logHandler option. - logHandler
-
enables processing of Asciidoctor messages (e.g. errors on missing included files), to hide messages as well setup build fail conditions based on them. Contains the following configuration elements:
-
outputToConsole
:Boolean
, defaults totrue
. Redirects all Asciidoctor messages to Mavenโs console logger as INFO during renderization. -
failIf
: build fail conditions, disabled by default. Allows setting one or many conditions that when met, abort the Maven build withBUILD FAILURE
status.๐Note that the plugin matches that all conditions are met together. Unless you are controlling a very specific case, setting one condition should be enough.
Also, messages matching fail conditions will be sent to Mavenโs logger as ERROR. So, when enablingoutputToConsole
, some messages will appear duplicated as both INFO and ERROR.Currently, two conditions can be defined:
-
severity
: severity of the Asciidoctor message, in order:INFO
,WARN
,ERROR
,FATAL
,UNKNOWN
. Build will fail if a message is found of severity equal or higher. -
containsText
: text to search inside messages. Build will fail if the text is found.
For example, setinclude
to fail on any issue related to included files regardless the severity level.example: fail on any message<logHandler> <outputToConsole>false</outputToConsole> <!--(1)--> <failIf> <severity>DEBUG</severity> <!--(2)--> </failIf> </logHandler>
-
Do not show messages as INFO in Maven output
-
Build will fail on any message of severity
DEBUG
or higher, that includes all. All matching messages will appear as ERROR in Maven output.
-
-
-
๐
|
Since version 1.5.8 of AsciidoctorJ set |
There are various attributes Asciidoctor recognizes. Below is a list of them and what they do.
- title
-
An override for the title of the document.
๐
|
This attribute, for backwards compatibility, can still be used in the top level configuration options. |
Many other attributes are possible. Refer to the catalog of document attributes in the Asciidoctor user manual for a complete list.
More will be added in the future to take advantage of other options and attributes of Asciidoctor.
Any setting in the attributes section that conflicts with an explicitly named attribute configuration will be overidden by the explicitly named attribute configuration.
These settings can all be changed in the <configuration>
section of the plugin section:
<plugin>
<configuration>
<sourceDirectory>src/docs/asciidoc</sourceDirectory>
<outputDirectory>target/docs/asciidoc</outputDirectory>
<backend>html</backend>
<doctype>book</doctype>
<attributes>
<stylesheet>my-theme.css</stylesheet>
</attributes>
</configuration>
</plugin>
It is possible to pass properties defined in the POM to the Asciidoctor processor. This is handy for example to include in the generated document the POM artifact version number.
This is done by creating a custom AsciiDoc property in the attributes
section of the configuration
.
The AsciiDoc property value is defined in the usual Maven way: ${property.name}
.
<attributes>
<project-version>${project.version}</project-version>
</attributes>
The custom AsciiDoc property can then be used in the document like this:
The latest version of the project is {project-version}.
Boolean attributes in asciidoctor, such as sectnums
, linkcss
or copycss
can be set with a value of true
and unset with a value of false
.
In the <attributes>
part of the Asciidoctor Maven Plugin configuration:
<sectnums>true</sectnums>
<linkcss>false</linkcss>
You can find more information and many examples ready to copy-paste in the Asciidoctor Maven examples project.
Configuration options can be set (but not replaced) using system properties directly in the command line as follows:
mvn generate-resources -Dasciidoctor.sourceDirectory=src/docs -Dasciidoctor.outputDirectory=target/docs
All options follow the naming convention `asciidoctor.` + option_name.
In order to provide a higher degree of flexibility attributes
configuration follows a different behavior.
Attributes defined through the command line are added to the ones already found in the XML configuration.
The result of it is that attributes and other configuration options can be updated if they are added to the command line as attributes.
For example, the following configuration could be modified with the command options as seen below.
<configuration>
<backend>html5</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<toc>left</toc>
</attributes>
</configuration>
mvn generate-resources -Dasciidoctor.attributes=toc=right
mvn generate-resources -Dasciidoctor.attributes="toc=right source-highlighter=highlight.js imagesdir=my_images"
Note that in the second case we need to use quotes due to the spaces, and that source-highlighter
is the asciidoctor attribute name used to update the configuration.
Maven has the ability to execute a Mojo multiple times. Instead of reinventing the wheel inside the Mojo, weโll push this off to Maven to handle the multiple executions. An example of this setup is below:
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version>
<executions>
<execution>
<id>output-html</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceHighlighter>coderay</sourceHighlighter>
<backend>html</backend>
<attributes>
<toc/>
<linkcss>false</linkcss>
</attributes>
</configuration>
</execution>
<execution>
<id>output-docbook</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>docbook</backend>
<doctype>book</doctype>
</configuration>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/asciidoc</sourceDirectory>
<headerFooter>true</headerFooter>
<imagesDir>../resources/images</imagesDir> <!--(1)-->
</configuration>
</plugin>
-
imagesDir
should be relative to the source directory. It defaults toimages
but in this example the images used in the docs are also used elsewhere in the project.
Any configuration specified outside the executions section is inherited by each execution. This allows an easier way of defining common configuration options.
To author your Maven-generated site in AsciiDoc, you must first add a dependency on the Asciidoctor plugin to your maven-site-plugin declaration (which more precisely adds a Doxia Parser Module).
โ
|
Maven v3.2.1 or above required, and since asciidoctor-maven-plugin v1.5.6 only maven-site-plugin v3.4 or above is supported. |
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.4</version>
<dependencies>
<dependency><!-- add Asciidoctor Doxia Parser Module -->
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
The Asciidoctor Doxia module follows the maven-site-plugin conventions for file location, and delegates all resource management to it.
First, all of your AsciiDoc-based files should be placed in src/site/asciidoc with an extension of .adoc
.
These files will be rendered into the target/site directory.
For example, the file src/site/asciidoc/usage.adoc will be rendered into target/site/usage.html.
Then, all resources (images, css, etc.) should be placed in src/site/resources. These will be automatically copied into target/site.
Also note that AsciiDoc files are converted to embeddable HTML and inserted into the siteโs page layout. This disables certain features such as a the sidebar toc.
Make sure you add a menu
item for each page so you can access it from the site navigation:
<body>
...
<menu name="User guide">
<item href="usage.html" name="Usage" />
</menu>
...
</body>
As of version 1.5.3 of the plugin, you can configure Asciidoctor by specifying configuration properties in the plugin declaration, just like with the main plugin goal. There are two important differences, however.
-
All the configuration for Asciidoctor in the site integration must be nested inside an
<asciidoc>
element. This is necessary since the<configuration>
element is used to configure more than just the Asciidoctor integration.Hereโs an example that shows how to set options, attributes and ignore partial AsciiDoc files (i.e., files that begin with an underscore).
Maven site integration with Asciidoctor configuration<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.4</version> <configuration> <asciidoc> <templateDirs> <dir>src/site/asciidoc/templates</dir> </templateDirs> <requires> <require>asciidoctor-diagram</require> </requires> <attributes> <source-highlighter>coderay</source-highlighter> <coderay-css>style</coderay-css> </attributes> </asciidoc> <moduleExcludes> <asciidoc>**/_*.adoc</asciidoc> </moduleExcludes> </configuration> <dependencies> <dependency> <groupId>org.asciidoctor</groupId> <artifactId>asciidoctor-maven-plugin</artifactId> <version>1.5.3</version> </dependency> </dependencies> </plugin>
โThe Asciidoctor base directory (i.e., document root) is configured as src/site/asciidoc by default, though this can be overridden. To do so, you can use either maven-site-plugin
siteDirectory
or AsciidoctorbaseDir
configuration options. Note that the first will affect the default resources directory also.Youโll notice in the example that excludes have been added for certain AsciiDoc files. This prevents the site integration from processing partial files (i.e., includes) as individual pages. You can tune this pattern to your liking. Thereโs currently no way (that we can tell) to configure this automatically.
-
For simplicity and the fact that resources are managed by maven-site-plugin, not all options found in the asciidoctor-maven-plugin are available in the
<asciidoc>
element.The supported ones are:
- baseDir
-
Sames as the pluginโs
baseDir
. Sets the root path for resources. Not set by default, AsciiDoc documents will be searched in src/site/asciidoc. External resources should be located in src/site/resources.๐Consider using maven-site-pluginโs siteDirectory
instead for better integration with the site functions (ie. resource copying). - templatesDir (also template_dir)
-
Built-in template are supported by specifying a templates directory (i.e.,
templatesDir
). This feature enables you to provide a custom template for converting any node in the tree (e.g., document, section, listing, etc). Custom templates can be extremely helpful when trying to customize the appearance of your site. - templatesDirs (also template_dirs)
-
Allows to set multiple templates. Note that each one should be enclosed in a
<dir>
element. - requires
-
Sames as the pluginโs
requires
. Specifies additional Ruby libraries not packaged in AsciidoctorJ,empty
by default.
Developer setup for hacking on this project isnโt very difficult. The requirements are very small:
-
Java
-
Maven 3
Everything else will be brought in by Maven. This is a typical Maven Java project, nothing special. You should be able to use IntelliJ, Eclipse, or Netbeans without any issue for hacking on the project.
Unit tests are written with Spock. This will be downloaded by Maven and can be run from IntelliJ without any additional setup. Tests are run simply by:
mvn clean test
Or any of the other goals which run tests.
Integration tests under src/it
are run using maven-invoker-plugin and the runt-its
profile.
To only run them without excluding unit tests, use:
mvn clean verify -DskipTests -Prun-its
To run all tests at once just use mvn clean verify -DskipTests -Prun-its
.
Use Maven project.version
property to create dedicated custom output directories.
<configuration>
...
<outputDirectory>target/generated-docs/${project.version}</outputDirectory>
...
</configuration>
Enable section numbering in the build using the attributes
section.
<configuration>
...
<attributes>
...
<sectnums>true</sectnums>
...
</attributes>
...
</configuration>
Automatically add version details to header and footer to all documents.
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH</maven.build.timestamp.format> (1)
</properties>
<configuration>
...
<attributes>
...
<revnumber>${project.version}</revnumber>
<revdate>${maven.build.timestamp}</revdate>
<organization>${project.organization.name}</organization>
</attributes>
...
</configuration>
-
Add
maven.build.timestamp.format
to the pomโs properties section to set a custom date format.