Maven
To generate documentation for a Maven-based project, you can use the Maven plugin for Dokka.
Compared to the Gradle plugin for Dokka, the Maven plugin has only basic features and does not provide support for multi-module builds.
You can play around with Dokka and see how it can be configured for a Maven project by visiting our Maven example project.
Apply Dokka
To apply Dokka, you need to add dokka-maven-plugin
to the plugins
section of your POM file:
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<version>1.9.20</version>
<executions>
<execution>
<phase>pre-site</phase>
<goals>
<goal>dokka</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Generate documentation
The following goals are provided by the Maven plugin:
Goal | Description |
---|---|
dokka:dokka |
Generates documentation with Dokka plugins applied. HTML format by default. |
Experimental
Goal | Description |
---|---|
dokka:javadoc |
Generates documentation in Javadoc format. |
dokka:javadocJar |
Generates a javadoc.jar file that contains documentation in Javadoc format. |
Other output formats
By default, the Maven plugin for Dokka builds documentation in HTML output format.
All other output formats are implemented as Dokka plugins. In order to generate documentation in the desired format, you have to add it as a Dokka plugin to the configuration.
For example, to use the experimental GFM format, you have to add gfm-plugin
artifact:
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
...
<configuration>
<dokkaPlugins>
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>gfm-plugin</artifactId>
<version>1.9.20</version>
</plugin>
</dokkaPlugins>
</configuration>
</plugin>
With this configuration, running the dokka:dokka
goal produces documentation in GFM format.
To learn more about Dokka plugins, see Dokka plugins.
Build javadoc.jar
If you want to publish your library to a repository, you may need to provide a javadoc.jar
file that contains
API reference documentation of your library.
For example, if you want to publish to Maven Central, you
must supply a javadoc.jar
alongside your project. However,
not all repositories have that rule.
Unlike the Gradle plugin for Dokka, the Maven plugin comes with a ready-to-use dokka:javadocJar
goal. By default, it generates documentation in Javadoc output format in thetarget
folder.
If you are not satisfied with the built-in goal or want to customize the output (for example, you want to generate documentation in HTML format instead of Javadoc), similar behavior can be achieved by adding the Maven JAR plugin with the following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
<execution>
<id>dokka-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dokka</classifier>
<classesDirectory>${project.build.directory}/dokka</classesDirectory>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</execution>
</executions>
</plugin>
The documentation and the .jar
archive for it are then generated by running dokka:dokka
and jar:jar@dokka-jar
goals:
mvn dokka:dokka jar:jar@dokka-jar
If you publish your library to Maven Central, you can use services like javadoc.io to host your library's API documentation for free and without any setup. It takes documentation pages straight from the
javadoc.jar
. It works well with the HTML format as demonstrated in this example.
Configuration example
Maven's plugin configuration block can be used to configure Dokka.
Here is an example of a basic configuration that only changes the output location of your documentation:
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
...
<configuration>
<outputDir>${project.basedir}/target/documentation/dokka</outputDir>
</configuration>
</plugin>
Configuration options
Dokka has many configuration options to tailor your and your reader's experience.
Below are some examples and detailed descriptions for each configuration section. You can also find an example with all configuration options applied at the bottom of the page.
General configuration
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<!-- ... -->
<configuration>
<skip>false</skip>
<moduleName>${project.artifactId}</moduleName>
<outputDir>${project.basedir}/target/documentation</outputDir>
<failOnWarning>false</failOnWarning>
<suppressObviousFunctions>true</suppressObviousFunctions>
<suppressInheritedMembers>false</suppressInheritedMembers>
<offlineMode>false</offlineMode>
<sourceDirectories>
<dir>${project.basedir}/src</dir>
</sourceDirectories>
<documentedVisibilities>
<visibility>PUBLIC</visibility>
<visibility>PROTECTED</visibility>
</documentedVisibilities>
<reportUndocumented>false</reportUndocumented>
<skipDeprecated>false</skipDeprecated>
<skipEmptyPackages>true</skipEmptyPackages>
<suppressedFiles>
<file>/path/to/dir</file>
<file>/path/to/file</file>
</suppressedFiles>
<jdkVersion>8</jdkVersion>
<languageVersion>1.7</languageVersion>
<apiVersion>1.7</apiVersion>
<noStdlibLink>false</noStdlibLink>
<noJdkLink>false</noJdkLink>
<includes>
<include>packages.md</include>
<include>extra.md</include>
</includes>
<classpath>${project.compileClasspathElements}</classpath>
<samples>
<dir>${project.basedir}/samples</dir>
</samples>
<sourceLinks>
<!-- Separate section -->
</sourceLinks>
<externalDocumentationLinks>
<!-- Separate section -->
</externalDocumentationLinks>
<perPackageOptions>
<!-- Separate section -->
</perPackageOptions>
</configuration>
</plugin>
Whether to skip documentation generation.
Default: false
The display name used to refer to the project/module. It's used for the table of contents, navigation, logging, etc.
Default: {project.artifactId}
The directory to where documentation is generated, regardless of format.
Default: {project.basedir}/target/dokka
Whether to fail documentation generation if Dokka has emitted a warning or an error. The process waits until all errors and warnings have been emitted first.
This setting works well with reportUndocumented
.
Default: false
Whether to suppress obvious functions.
A function is considered to be obvious if it is:
-
Inherited from
kotlin.Any
,Kotlin.Enum
,java.lang.Object
orjava.lang.Enum
, such asequals
,hashCode
,toString
. -
Synthetic (generated by the compiler) and does not have any documentation, such as
dataClass.componentN
ordataClass.copy
.
Default: true
Whether to suppress inherited members that aren't explicitly overridden in a given class.
Note: This can suppress functions such as equals
/hashCode
/toString
,
but cannot suppress synthetic functions such as dataClass.componentN
and
dataClass.copy
. Use suppressObviousFunctions
for that.
Default: false
Whether to resolve remote files/links over your network.
This includes package-lists used for generating external documentation links. For example, to make classes from the standard library clickable.
Setting this to true
can significantly speed up build times in certain cases,
but can also worsen documentation quality and user experience. For example, by
not resolving class/member links from your dependencies, including the standard library.
Note: You can cache fetched files locally and provide them to
Dokka as local paths. See externalDocumentationLinks
section.
Default: false
The source code roots to be analyzed and documented.
Acceptable inputs are directories and individual .kt
/ .java
files.
Default: {project.compileSourceRoots}
The set of visibility modifiers that should be documented.
This can be used if you want to document protected
/internal
/private
declarations,
as well as if you want to exclude public
declarations and only document internal API.
Can be configured on per-package basis.
Default: PUBLIC
Whether to emit warnings about visible undocumented declarations, that is declarations without KDocs
after they have been filtered by documentedVisibilities
and other filters.
This setting works well with failOnWarning
.
This can be overridden at package level.
Default: false
Whether to document declarations annotated with @Deprecated
.
This can be overridden at package level.
Default: false
Whether to skip packages that contain no visible declarations after various filters have been applied.
For example, if skipDeprecated
is set to true
and your package contains only
deprecated declarations, it is considered to be empty.
Default: true
The directories or individual files that should be suppressed, meaning that declarations from them are not documented.
The JDK version to use when generating external documentation links for Java types.
For example, if you use java.util.UUID
in some public declaration signature,
and this option is set to 8
, Dokka generates an external documentation link
to JDK 8 Javadocs for it.
Default: JDK 8
The Kotlin language version used for setting up analysis and @sample environment.
By default, the latest language version available to Dokka's embedded compiler is used.
The Kotlin API version used for setting up analysis and @sample environment.
By default, it is deduced from languageVersion
.
Whether to generate external documentation links that lead to the API reference documentation of Kotlin's standard library.
Note: Links are generated when noStdLibLink
is set to false
.
Default: false
Whether to generate external documentation links to JDK's Javadocs.
The version of JDK Javadocs is determined by the jdkVersion
option.
Note: Links are generated when noJdkLink
is set to false
.
Default: false
A list of Markdown files that contain module and package documentation
The contents of specified files are parsed and embedded into documentation as module and package descriptions.
The classpath for analysis and interactive samples.
This is useful if some types that come from dependencies are not resolved/picked up automatically.
This option accepts both .jar
and .klib
files.
Default: {project.compileClasspathElements}
A list of directories or files that contain sample functions which are referenced via @sample KDoc tag.
Source link configuration
The sourceLinks
configuration block allows you to add a source
link to each signature
that leads to the url
with a specific line number. (The line number is configurable by setting lineSuffix
).
This helps readers to find the source code for each declaration.
For an example, see the documentation for the
count()
function in kotlinx.coroutines
.
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<!-- ... -->
<configuration>
<sourceLinks>
<link>
<path>src</path>
<url>https://github.com/kotlin/dokka/tree/master/src</url>
<lineSuffix>#L</lineSuffix>
</link>
</sourceLinks>
</configuration>
</plugin>
The path to the local source directory. The path must be relative to the root of the current module.
Note: Only Unix based paths are allowed, Windows-style paths will throw an error.
The URL of the source code hosting service that can be accessed by documentation readers, like GitHub, GitLab, Bitbucket, etc. This URL is used to generate source code links of declarations.
The suffix used to append source code line number to the URL. This helps readers navigate not only to the file, but to the specific line number of the declaration.
The number itself is appended to the specified suffix. For example, if this option is set
to #L
and the line number is 10, the resulting URL suffix is #L10
.
Suffixes used by popular services:
- GitHub:
#L
- GitLab:
#L
- Bitbucket:
#lines-
External documentation links configuration
The externalDocumentationLinks
block allows the creation of links that lead to the externally hosted documentation of
your dependencies.
For example, if you are using types from kotlinx.serialization
, by default they are unclickable in your
documentation, as if they are unresolved. However, since the API reference documentation for kotlinx.serialization
is built by Dokka and is published on kotlinlang.org, you can
configure external documentation links for it. Thus allowing Dokka to generate links for types from the library, making
them resolve successfully and clickable.
By default, external documentation links for Kotlin standard library and JDK are configured.
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<!-- ... -->
<configuration>
<externalDocumentationLinks>
<link>
<url>https://kotlinlang.org/api/kotlinx.serialization/</url>
<packageListUrl>file:/${project.basedir}/serialization.package.list</packageListUrl>
</link>
</externalDocumentationLinks>
</configuration>
</plugin>
The root URL of documentation to link to. It must contain a trailing slash.
Dokka does its best to automatically find the package-list
for the given URL,
and link declarations together.
If automatic resolution fails or if you want to use locally cached files instead,
consider setting the packageListUrl
option.
The exact location of a package-list
. This is an alternative to relying on Dokka
automatically resolving it.
Package lists contain information about the documentation and the project itself, such as module and package names.
This can also be a locally cached file to avoid network calls.
Package options
The perPackageOptions
configuration block allows setting some options for specific packages matched by matchingRegex
.
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<!-- ... -->
<configuration>
<perPackageOptions>
<packageOptions>
<matchingRegex>.*api.*</matchingRegex>
<suppress>false</suppress>
<reportUndocumented>false</reportUndocumented>
<skipDeprecated>false</skipDeprecated>
<documentedVisibilities>
<visibility>PUBLIC</visibility>
<visibility>PRIVATE</visibility>
<visibility>PROTECTED</visibility>
<visibility>INTERNAL</visibility>
<visibility>PACKAGE</visibility>
</documentedVisibilities>
</packageOptions>
</perPackageOptions>
</configuration>
</plugin>
The regular expression that is used to match the package.
Default: .*
Whether this package should be skipped when generating documentation.
Default: false
The set of visibility modifiers that should be documented.
This can be used if you want to document protected
/internal
/private
declarations within this package,
as well as if you want to exclude public
declarations and only document internal API.
Default: PUBLIC
Whether to document declarations annotated with @Deprecated
.
This can be set on project/module level.
Default: false
Whether to emit warnings about visible undocumented declarations, that is declarations without KDocs
after they have been filtered by documentedVisibilities
and other filters.
This setting works well with failOnWarning
.
Default: false
Complete configuration
Below you can see all the possible configuration options applied at the same time.
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<!-- ... -->
<configuration>
<skip>false</skip>
<moduleName>${project.artifactId}</moduleName>
<outputDir>${project.basedir}/target/documentation</outputDir>
<failOnWarning>false</failOnWarning>
<suppressObviousFunctions>true</suppressObviousFunctions>
<suppressInheritedMembers>false</suppressInheritedMembers>
<offlineMode>false</offlineMode>
<sourceDirectories>
<dir>${project.basedir}/src</dir>
</sourceDirectories>
<documentedVisibilities>
<visibility>PUBLIC</visibility>
<visibility>PRIVATE</visibility>
<visibility>PROTECTED</visibility>
<visibility>INTERNAL</visibility>
<visibility>PACKAGE</visibility>
</documentedVisibilities>
<reportUndocumented>false</reportUndocumented>
<skipDeprecated>false</skipDeprecated>
<skipEmptyPackages>true</skipEmptyPackages>
<suppressedFiles>
<file>/path/to/dir</file>
<file>/path/to/file</file>
</suppressedFiles>
<jdkVersion>8</jdkVersion>
<languageVersion>1.7</languageVersion>
<apiVersion>1.7</apiVersion>
<noStdlibLink>false</noStdlibLink>
<noJdkLink>false</noJdkLink>
<includes>
<include>packages.md</include>
<include>extra.md</include>
</includes>
<classpath>${project.compileClasspathElements}</classpath>
<samples>
<dir>${project.basedir}/samples</dir>
</samples>
<sourceLinks>
<link>
<path>src</path>
<url>https://github.com/kotlin/dokka/tree/master/src</url>
<lineSuffix>#L</lineSuffix>
</link>
</sourceLinks>
<externalDocumentationLinks>
<link>
<url>https://kotlinlang.org/api/latest/jvm/stdlib/</url>
<packageListUrl>file:/${project.basedir}/stdlib.package.list</packageListUrl>
</link>
</externalDocumentationLinks>
<perPackageOptions>
<packageOptions>
<matchingRegex>.*api.*</matchingRegex>
<suppress>false</suppress>
<reportUndocumented>false</reportUndocumented>
<skipDeprecated>false</skipDeprecated>
<documentedVisibilities>
<visibility>PUBLIC</visibility>
<visibility>PRIVATE</visibility>
<visibility>PROTECTED</visibility>
<visibility>INTERNAL</visibility>
<visibility>PACKAGE</visibility>
</documentedVisibilities>
</packageOptions>
</perPackageOptions>
</configuration>
</plugin>