Maven Hello World

Getting started with Maven Release Plugin and Github Action

Links

Login to your sonatype account to release the version

Type URL Description
Snapshot sonatype Sonatype Snapshot versions
Release maven Maven Release versions
Release central-sonatype Sonatype Release versions

Documentation

Secrets

  • MAVEN_GPG_PRIVATE_KEY - Take it from the private.gpg
  • OSSRH_USERNAME - Created here
  • OSSRH_TOKEN - Created here
  • MAVEN_GPG_PASSPHRASE - Create here
    • This passphrase and your private key are all that is needed to sign artifacts with your signature.
  • GITHUB_TOKEN - Github token

Demo

How do you release a version?

Manually:
   1. Check the version in the pom.xml
      * 0.0.1-SNAPSHOT
   2. Go to Github action -> Run workflow
      * Release: 0.0.1
      * Snapshot: 0.0.2-SNAPSHOT

Automatically:
Push to Github will create a release and a snapshot but it won't publish the release.
Snapshot will be available through here

<dependency>
    <groupId>com.chenspoul</groupId>
    <artifactId>maven-hello-world</artifactId>
    <version>0.0.1</version>
</dependency>

Maven in 5 Minutes

Follow after the documentation here to start your first maven.

Expend your ./src/main/java/com/mycompany/app/App.java with:

package com.mycompany.app;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class App {
    public static void main( String[] args ) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "<h1> Hello World! </h1>";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}

GPG

After generating gpg key following by here

gpg --gen-key

You can list the local key that you created:

gpg --list-secret-keys --keyid-format=long

Then you can export the private key to your local machine in order to upload it later to Github secret:

gpg --armor --export-secret-keys <YOUR_KEY> > private.gpg
  • YOUR_KEY=‘long number’

Local commands

Check your maven settings file ~/.m2/settings.xml:

    <server>
        <id>gpg.passphrase</id>
        <passphrase><PASSPHRASE_GPG></passphrase>
    </server>
    <server>
        <id>ossrh</id>
        <username><OSSRH_USERNAME></username>
        <password><OSSRH_TOKEN></password>
    </server>

According to publish-maven, you can do these as below.

Performing a Snapshot Deployment

Snapshot deployment are performed when your version ends in -SNAPSHOT . You do not need to fulfill the requirements when performing snapshot deployments and can simply run

mvn -B clean deploy

SNAPSHOT versions are not synchronized to the Central Repository. If you wish your users to consume your SNAPSHOT versions, they would need to add the snapshot repository to their Nexus Repository Manager, settings.xml, or pom.xml. Successfully deployed SNAPSHOT versions will be found in https://s01.oss.sonatype.org/content/repositories/snapshots/

Performing a Release Deployment

In order to perform a release deployment you have to edit your version in all your POM files to use release versions.

mvn versions:set -DnewVersion=1.2.3 versions:commit

Install to local repository ~/.m2/repository:

mvn clean install

Release to the staging repositorywith the release profile:

mvn clean deploy -P release

And then login to the staging repository https://s01.oss.sonatype.org/#stagingRepositories , release and drop the repository. Wait a few minutes and then you can find the release version in https://central.sonatype.com/artifact/com.chensoul/maven-hello-world/ or https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/chensoul/maven-hello-world/ .

Manually Publish the site to github pages

Publish to github pages:

mvn clean site scm-publish:publish-scm