Exercise 1.4: Linking Javadoc Documentation in Github

Author

Chuck Nelson

Published

September 8, 2025

1 Linking Javadoc to Your Project README

In this exercise, you will learn how to make the API documentation for your project accessible directly from your GitHub repository’s README file. This is a common practice that helps other developers understand and use your code.

1.1 Prerequisites

  • You have a Java project with a pom.xml file.
  • You have run mvn javadoc:javadoc and successfully generated the Javadoc site. You should see a target/site/apidocs directory in your project folder.
  • You have Git initialized for your project and are using VS Code.

1.2 Prepare the Local Environment

First, we need to create a simple index.html file in your project’s root directory that will redirect to the Javadoc. This is a common workaround for linking to documentation hosted on GitHub Pages.

  1. Open your project in VS Code.
  2. In the Explorer sidebar, click the “New File” icon in the root of your project.
  3. Name the new file index.html.
  4. Copy and paste the following HTML code into the new file:
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="refresh" content="0; url=./target/site/apidocs/index.html">
  <title>Project Documentation</title>
</head>
<body>
  <p>Redirecting to <a href="./target/site/apidocs/index.html">documentation</a>...</p>
</body>
</html>
  1. Save the index.html file. This file will be the entry point for your documentation link.

1.3 Update the .gitignore File

We need to make sure that the generated Javadoc files are tracked by Git, but we should not track the target/ directory itself, as it contains build artifacts.

  1. Open or create the .gitignore file in your project’s root.
  2. Find the line target/ and comment it out or remove it temporarily.
# target/
.idea/
.vscode/

Instead, add a specific line to ignore everything inside the target/ directory except the site/apidocs folder. Add the following lines to your .gitignore:

/target/*
!/target/site/
!/target/site/apidocs/

The ! operator tells Git to negate a previously ignored file, so !/target/site/apidocs/ tells Git to track that directory even though target/ is ignored.

Back to top