001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.weaver.maven;
020
021import java.io.File;
022import java.util.List;
023import java.util.Properties;
024
025import org.apache.commons.weaver.CleanProcessor;
026import org.apache.maven.plugin.AbstractMojo;
027import org.apache.maven.plugin.MojoExecutionException;
028import org.apache.maven.plugins.annotations.Parameter;
029
030/**
031 * Defines common properties.
032 */
033public abstract class AbstractPrepareMojo extends AbstractMojo {
034
035    /**
036     * {@code verbose} parameter.
037     */
038    @Parameter(defaultValue = "false")
039    protected boolean verbose;
040
041    /**
042     * {@code weaver.config} parameter.
043     */
044    @Parameter(property = "weaver.config", required = false)
045    protected Properties weaverConfig;
046
047    /**
048     * Get the classpath for this prepare mojo.
049     * @return {@link List} of {@link String}
050     */
051    protected abstract List<String> getClasspath();
052
053    /**
054     * Get the target directory for this prepare mojo.
055     * @return {@link File}
056     */
057    protected abstract File getTarget();
058
059    /**
060     * Execute this mojo.
061     * @throws MojoExecutionException in the event of failure
062     */
063    @Override
064    public void execute() throws MojoExecutionException {
065        if (!getTarget().isDirectory()) {
066            return;
067        }
068        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
069        logRedirector.activate();
070
071        final List<String> classpath = getClasspath();
072        final File target = getTarget();
073        final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
074
075        getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
076
077        try {
078            final CleanProcessor cleanProcessor = new CleanProcessor(classpath, target, config);
079            cleanProcessor.clean();
080        } catch (Exception e) {
081            throw new MojoExecutionException("cleaning failed due to " + e.getMessage(), e);
082        } finally {
083            logRedirector.deactivate();
084        }
085    }
086
087}