Monthly Archives:January 2020

Publishing a Maven Site to GitHub Pages

In this tutorial I’d like to show how to publish aMaven SitetoGitHub Pages. You can find several documents on the web about this subject, but I decided to publish one myself because the documents I found refer to a deprecated plugin (https://github.com/github/maven-plugins/tree/master/github-site-plugin) or show complicated settings, also when using the official Maven plugin that I’m going to use myself in this post,Apache Maven SCM Publish Plugin,https://maven.apache.org/plugins/maven-scm-publish-plugin/index.html(probably because the documents I found are somehow old, and they refer to an old version of such a plugin, which has probably been improved a lot since then).

I’m going to use a very simple Java example. The final project is available here:https://github.com/LorenzoBettini/maven-site-github-example.

Create the Java Maven project

Let’s create a simple Java project using the archetype (of course, I’m going to use fake names for the groupId and artifactId, but such values should be used consistently from now on):

mvn archetype:generate \
    -DgroupId=com.mycompany.app \
    -DartifactId=my-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.4 \
    -DinteractiveMode=false

This will create themy-appfolder with the Maven project. Let’s cd into that folder and make sure it compiles fine:

We can also generate the site of this project (in its default shape):

The site will be generated in thetarget/sitefolder and can be opened with a browser; for example, let’s open itsindex.html:

GitHub setup

Let’s publish the Maven project on GitHub; in my case it’s inhttps://github.com/LorenzoBettini/maven-site-github-example.

Now we have to setupgh-pagesbranch on our Git repository. This step is required the first time only. (You might want to have a look athttps://help.github.com/en/github/working-with-github-pages/about-github-pagesif you’re note familiar with GitHub pages).

Let’s prepare thegh-pagesbranch: this will be a separate branch in the Git repository, we create it as an orphanbranch (see alsohttps://maven.apache.org/plugins/maven-scm-publish-plugin/various-tips.html).WARNING: the following commands will wipe out the current contents of the local Git repository, so make sure you don’t have any uncommitted changes! Let’s run the following commands in the main folder of the Git repository:

  1. git checkout --orphan gh-pages to create the branch locally,
  2. rm .git/index ; git clean -fdxto clean the branch content and let it empty,
  3. echo "It works" > index.htmlto create an initial site content
  4. git add . && git commit -m "initial site content" && git push origin gh-pagesto commit and push to the branch gh-pages

The corresponding web site will be published in your GitHub project’s corresponding URL; in my case it ishttps://lorenzobettini.github.io/maven-site-github-example:  This should show “It works”

Now we can go back to the masterbranch:

and remove the index.htmlwe previously created.

Maven POM configuration

Let’s see how to configure our POM to automatically publish the Maven site to GitHub pages.

First, we must specify the distribution management section:

Remember that you will have to use the URL corresponding to your own GitHub project (including your GitHub user id).

Then, we configure themaven-scm-publish-pluginconfiguration in thepluginManagementsection (we configure it there, and then we will call its goals directly from the command line – such a plugin can also be configured to be bound to the Maven lifecycle, but that’s out of the scope of this tutorial, you might want to have a look here in case:https://maven.apache.org/plugins/maven-scm-publish-plugin/usage.html). Note that we specify the gh-pagesbranch:

Publish the site

Now, publishing the Maven site to GitHub pages it’s just a matter of running:

Wait for the plugin to perform its job.

What the plugin does is

  1. It first checks out the contents of thegh-pagesbranch from GitHub into a local folder (by default,target/scmpublish-checkout);
  2. Then locally staged site content is applied to the check-out, issuing appropriate Git commands to add and delete entries, followed by a commit and a push.

Now the site is on GitHub Pages:

Improve the site contents

Just for demonstration, let’s enrich the site a bit by using the Maven Archetype Site,https://maven.apache.org/archetypes/maven-archetype-site.

We layer this archetype upon our existing project, so we must run it from the directory containing our current Maven project and specify the same groupId and artifactId we specified when we created the Maven project:

The archetype will update our project with a src/sitedirectory containing a few example contents in Markdown, APT, etc. It will also update our POM with some configuration for maven-project-info-reports-pluginand for i18n localization. Moreover, a new skinwill be used for the final site,maven-fluido-skin.

You might want to have a look locally by regenerating the site.

Let’s publish the new site, again by running

Now it’s on GitHub Pages (it might take some time for the new website to be updated on GitHub and browser reload might help):

To jump to the published website you could also use the GitHub web interface: click on the “environment” link and then on the “View deployment” button corresponding to the latest pushed commit on the gh-pages branch:

Now you could experiment by adding/changing/removing the contents of the directorysrc/siteand then publish the website again through Maven.

Remember

What’s in your Maven project (e.g.,masterbranch) contains the sources of the site, while, what’s in the gh-pagesbranch on GitHub will contain the generated website. You won’t modify the contents of gh-pagesbranch manually: theApache Maven SCM Publishwill do that for you.

Hope you enjoy this tutorial 🙂