Search code examples
mavenmaven-3pom.xmlmaven-site-plugin

Build a site zip with Maven


actually, I generate a maven site containing the documentation of my project. It works very well, in fact if works so well that my customers wants to get that site as a deliverable (for obvious documentation purpose).

How can I tell Maven to build a zip of the whole site and deploy it to my artifacts manager (Nexus)? I've tried several things, but if I understand correctly, deploying artifacts and generating the site are using different livecycle, and the site generation occurs after the deployment of the artifacts..

I could obviously get the generated site from the location it's deployed during site-deploy, but I would greatly appreciate an automatic and centralized way...

PS: giving access to the customer to our internal site is NOT an option.


Solution

  • Here is a working solution delegated to a Maven profile to isolate the behavior (and speed-up normal builds), but which could also be integrated in the default build if required (although not recommended).

    <profiles>
        <profile>
            <id>site-zip</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>3.4</version>
                        <executions>
                            <execution>
                                <id>pack-site</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>site</goal>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <attach>false</attach>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
    
                    <plugin>
                        <groupId>com.coderplus.maven.plugins</groupId>
                        <artifactId>copy-rename-maven-plugin</artifactId>
                        <version>1.0.1</version>
                        <executions>
                            <execution>
                                <id>rename-file</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>rename</goal>
                                </goals>
                                <configuration>
                                    <sourceFile>${project.build.directory}/${project.build.finalName}-site.jar</sourceFile>
                                    <destinationFile>${project.build.directory}/${project.build.finalName}-site.zip</destinationFile>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
    
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.10</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <phase>package</phase>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/${project.build.finalName}-site.zip</file>
                                            <type>zip</type>
                                            <classifier>site</classifier>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    What the profile is actually doing:

    As such, running

    mvn clean install -Psite-zip
    

    Will also install in your local Maven cache the zipped site. The deploy phase would do the same on your target Maven repository then.

    Note that the Maven Site Plugin and the Copy Plugin must be declared in the order above to follow the required flow within the same phase.

    Also note that if zip is not a strong requirement, you can then just skip the Copy and Build Helper executions and only use the Maven Site execution. By default the jar created providing the site is already attached to the build (and hence it will be installed and deployed automatically). In order to have the zip, we had to disable this behavior (<attach>false</attach>) and re-attach it via the Build Helper plugin.

    The generated zipped has automatically a classifier, which is site in this case.