org.nuiton.jredmine.plugin.UpdateVersionMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-jredmine-plugin Show documentation
Show all versions of maven-jredmine-plugin Show documentation
JRedmine maven plugin to interacts with Redmine's server
/*
* *##%
* JRedmine maven plugin
* Copyright (C) 2009 CodeLutin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* ##%*
*/
package org.nuiton.jredmine.plugin;
import java.text.ParseException;
import java.util.Date;
import org.apache.maven.plugin.MojoExecutionException;
import org.nuiton.jredmine.model.ModelHelper;
import org.nuiton.jredmine.model.Version;
import org.nuiton.plugin.PluginHelper;
/**
* Create or update a version on redmine server.
*
* Will add a the version if not existing, otherwise, will update the version.
*
* @goal update-version
*
* @author tchemit
* @since 1.0.0
*/
public class UpdateVersionMojo extends AbstractRedmineMojo {
/**
* The news description to update to redmine server.
*
* Note : if not set, no update of the description will be made.
*
* @parameter expression="${redmine.versionDescription}"
* @since 1.0.0
*/
protected String versionDescription;
/**
* A flag to close the version on redmine (will fix effectiveDate).
*
* @parameter expression="${redmine.closeVersion}" default-value="false"
* @since 1.0.0
*/
private boolean closeVersion;
/**
* The effective date to set on the version.
*
* The format of the date is {@code yyyy-mm-dd}.
*
* If not Set - will use current day date only if {@link #closeVersion}
* flag is on.
*
* @parameter expression="${redmine.effectiveDate}"
* @since 1.0.0
*/
private String effectiveDate;
/**
* A flag to skip the goal.
*
* @parameter expression="${redmine.skipUpdateVersion}" default-value="false"
* @since 1.0.0
*/
private boolean skipUpdateVersion;
/**
* A flag to test plugin but send nothing to redmine.
*
* @parameter expression="${redmine.dryRun}" default-value="false"
* @since 1.0.0
*/
protected boolean dryRun;
/**
* A flag to restirct only one run in a build (for multi-module context).
*
* @parameter expression="${redmine.runOnce}" default-value="true"
* @since 1.0.0
*/
private boolean runOnce;
/**
* effective date to set
*/
private Date date;
public UpdateVersionMojo() {
super(true, false, true);
}
@Override
protected boolean isGoalSkip() {
return skipUpdateVersion;
}
@Override
protected boolean isRunOnce() {
return runOnce;
}
@Override
protected boolean checkRunOnceDone() {
return isRunOnce() && !isExecutionRoot();
}
@Override
protected boolean init() throws Exception {
if (versionId == null || versionId.trim().isEmpty()) {
throw new MojoExecutionException("required a versionId parameter");
}
versionId = PluginHelper.removeSnapshotSuffix(versionId);
runOnceDone = false;
if (isRunOnce()) {
runOnceDone = checkRunOnceDone();
if (runOnceDone) {
return true;
}
}
if (effectiveDate != null && !effectiveDate.trim().isEmpty()) {
try {
date = dateFormat.parse(effectiveDate);
} catch (ParseException e) {
throw new MojoExecutionException("could not parse effectivate date " + effectiveDate + " for reason " + e.getMessage(), e);
}
} else if (closeVersion) {
date = new Date();
}
return super.init();
}
@Override
protected void doAction() throws Exception {
if (isRunOnceDone()) {
getLog().info("skip goal, runOnce flag is on, and was already executed.");
return;
}
if (dryRun) {
getLog().info("\n dryRun flag is on, no data will be send!\n");
}
// get version
boolean needCreateVersion;
Version[] versions = service.getVersions(projectId);
Version v = ModelHelper.byVersionName(versionId, versions);
if (v == null) {
// version must be created
needCreateVersion = true;
v = new Version();
v.setName(versionId);
} else {
needCreateVersion = false;
}
if (versionDescription != null && !versionDescription.trim().isEmpty()) {
v.setDescription(versionDescription.trim());
}
if (date != null) {
v.setEffectiveDate(date);
}
this.releaseVersion = v;
// prepare version
if (needCreateVersion) {
// create version
getLog().info("create version " + releaseVersion.getName());
if (!dryRun) {
service.addVersion(projectId, releaseVersion);
}
} else {
// update version
getLog().info("udpate version " + releaseVersion.getName());
if (!dryRun) {
service.updateVersion(projectId, releaseVersion);
}
}
}
}