Apache Maven Site Plugin

The Apache Maven Site Plugin is a powerful tool for generating project documentation, including reports and other useful information about your Maven project. In this guide, we will explore how to configure and use the Maven Site Plugin, including a complete example.

Introduction to Maven Site Plugin

The Maven Site Plugin creates a site for the current project, which can include reports and documentation. This site can be deployed to a web server, making it accessible to anyone who needs to understand the project's structure, dependencies, and other details.

Latest Version

As of this writing, the latest version of the Maven Site Plugin is 3.12.0. It is recommended to use the latest version to ensure you have access to all the latest features and bug fixes.

Setting Up the Maven Site Plugin

To use the Maven Site Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a complete Maven project with the Site Plugin.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=site-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a simple Maven project with the following structure:

site-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven Site Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven Site Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>site-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.12.0</version>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-project-info-reports-plugin</artifactId>
                            <version>3.1.2</version>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: Specifies the group ID for the Maven Site Plugin.
  • <artifactId>: Specifies the Maven Site Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.
  • <configuration>: Allows for additional configuration options.
    • <reportPlugins>: Specifies the report plugins to include in the site.
      • <plugin>: Defines a specific report plugin.
        • <groupId>: Group ID for the report plugin.
        • <artifactId>: Artifact ID for the report plugin.
        • <version>: The version of the report plugin.

Step 3: Add Hello World to App.java

Open src/main/java/com/example/App.java and add a simple Hello World program:

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Step 4: Generate the Site

Run the following command to generate the site for your project:

mvn site

This command will create a target/site directory containing the generated site files. The site will include various reports and documentation.

Step 5: View the Generated Site

Open the target/site/index.html file in your web browser to view the generated site. You should see a homepage with links to various reports and documentation sections.

Step 6: Deploy the Site

To deploy the generated site to a web server, you can use the mvn site:deploy command. This requires additional configuration in your pom.xml to specify the deployment settings.

Step 7: Running the Application

Run the following command to execute the main JAR and see the "Hello, World!" output:

java -jar target/site-plugin-demo-1.0-SNAPSHOT.jar

You should see the following output:

Hello, World!

Example Project with Full Configuration

Here is the complete pom.xml file for the example project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>site-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.12.0</version>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-project-info-reports-plugin</artifactId>
                            <version>3.1.2</version>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Conclusion

The Maven Site Plugin is a powerful tool for generating project documentation and reports. This guide provided a complete example of setting up and using the Maven Site Plugin in a simple Maven project, helping you get started with creating and managing your project's site. By following these steps, you can easily generate and deploy comprehensive documentation for your Maven projects.

Comments