All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.sourcepond.maven.release.PluginException Maven / Gradle / Ivy

/*Copyright (C) 2016 Roland Hauser, 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package ch.sourcepond.maven.release;

import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;

import java.util.LinkedList;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

@SuppressWarnings("serial")
public class PluginException extends Exception {
	protected final List messages = new LinkedList<>();

	public PluginException(final Throwable cause, final String format, final Object... args) {
		super(format(format, args), cause);
	}

	public PluginException(final String format, final Object... args) {
		super(format(format, args));
	}

	public PluginException add(final String format, final Object... args) {
		messages.add(format(format, args));
		return this;
	}

	public List getMessages() {
		return unmodifiableList(messages);
	}

	private void printCause(final Log log) {
		if (getCause() != null) {
			log.error(format("Caused by %s", getCause().getClass()));
			log.error(getCause().getMessage());
		}

		if (getCause() instanceof PluginException) {
			final PluginException plex = (PluginException) getCause();
			for (final String line : plex.messages) {
				log.error(line);
			}
			log.error("");
			plex.printCause(log);
		}
	}

	protected void printBigErrorMessageAndThrow(final Log log) throws MojoExecutionException {
		log.error("");
		log.error("");
		log.error("");
		log.error("************************************");
		log.error("Could not execute the release plugin");
		log.error("************************************");
		log.error("");
		log.error("");
		log.error(getMessage());
		for (final String line : messages) {
			log.error(line);
		}
		log.error("");
		log.error("");
		printCause(log);
		throw new MojoExecutionException(getMessage());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy