com.scireum.VerifyReleaseTagMojo Maven / Gradle / Ivy
Show all versions of github-tag-plugin Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package com.scireum;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.FileNotFoundException;
/**
* Verifies that no tag with the current (to be deployed) project version already exisits.
*
* This can be used to block accidential redeployments of maven artifacts as these will not be picked by the
* client.
*/
@Mojo(name = "verifyReleaseTag", defaultPhase = LifecyclePhase.VALIDATE)
public class VerifyReleaseTagMojo extends AbstractGithubTagMojo {
/**
* Determines if the goal should be skipped
*/
@Parameter(property = "github.verifyReleaseTag.skip")
private boolean skip;
/**
* The current session to check if a deploy is scheduled
*/
@Parameter(defaultValue = "${session}")
private MavenSession session;
@Override
protected void executeWithConfig() throws MojoExecutionException {
if (skip) {
getLog().info("Skipping (github.tag.verifyReleaseTag is true).");
return;
}
if (!session.getGoals().contains("deploy")) {
getLog().info("Skipping as no deploy is scheduled...");
return;
}
if (project.getVersion().endsWith("SNAPSHOT")) {
getLog().info("Not going to verify a SNAPSHOT version. Skipping...");
return;
}
if (Strings.isNullOrEmpty(getCurrentCommit())) {
throw new MojoExecutionException(
"No commit hash (github.commitHash) is available. I would not be able to tag this release and therefore abort!");
}
try {
JSONObject obj = call("GET", "/git/refs/tags/" + project.getVersion(), null);
if (Integer.valueOf(200).equals(obj.get("state"))) {
if (("refs/tags/" + project.getVersion()).equals(obj.get("ref"))) {
throw new MojoExecutionException("A release tag for "
+ project.getVersion()
+ " is already present! Aborting build");
}
}
} catch (MojoExecutionException e) {
throw e;
} catch (FileNotFoundException e) {
getLog().debug("Ignoring: " + e.getMessage());
} catch (Exception e) {
throw new MojoExecutionException("Cannot create tag: " + e.getMessage(), e);
}
getLog().info("Version " + project.getVersion() + " seems uneleased so far...");
}
}