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.WeaveProcessor;
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 AbstractWeaveMojo 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 weave mojo.
049     * @return {@link List} of {@link String}
050     */
051    protected abstract List<String> getClasspath();
052
053    /**
054     * Get the target directory for this weave 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        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
066        logRedirector.activate();
067
068        final List<String> classpath = getClasspath();
069        final File target = getTarget();
070        final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
071
072        getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
073
074        try {
075            final WeaveProcessor weaveProcessor = new WeaveProcessor(classpath, target, config);
076            weaveProcessor.weave();
077        } catch (Exception e) {
078            throw new MojoExecutionException("weaving failed due to " + e.getMessage(), e);
079        } finally {
080            logRedirector.deactivate();
081        }
082    }
083
084}