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.maven.plugin.plugin;
020
021import java.util.Collections;
022import java.util.List;
023
024import org.apache.maven.plugin.AbstractMojo;
025import org.apache.maven.plugin.MojoExecutionException;
026import org.apache.maven.plugins.annotations.Parameter;
027import org.apache.maven.project.MavenProject;
028
029/**
030 * Abstract class for this Plugin.
031 *
032 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
033 *
034 */
035public abstract class AbstractGeneratorMojo extends AbstractMojo {
036    /**
037     * The project currently being built.
038     */
039    @Parameter(defaultValue = "${project}", readonly = true)
040    protected MavenProject project;
041
042    /**
043     * The goal prefix that will appear before the ":".
044     */
045    @Parameter
046    protected String goalPrefix;
047
048    /**
049     * Set this to "true" to skip invoking any goals or reports of the plugin.
050     *
051     * @since 2.8
052     */
053    @Parameter(defaultValue = "false", property = "maven.plugin.skip")
054    private boolean skip;
055
056    /**
057     * Maven plugin packaging types. Default is single "maven-plugin".
058     *
059     * @since 3.3
060     */
061    @Parameter
062    private List<String> packagingTypes = Collections.singletonList("maven-plugin");
063
064    /**
065     * System/OS line separator: used to format console messages.
066     */
067    protected static final String LS = System.lineSeparator();
068
069    protected abstract void generate() throws MojoExecutionException;
070
071    @Override
072    public void execute() throws MojoExecutionException {
073        if (!packagingTypes.contains(project.getPackaging())) {
074            getLog().info("Unsupported packaging type " + project.getPackaging() + ", execution skipped");
075            return;
076        }
077
078        if (skip) {
079            getLog().warn("Execution skipped");
080            return;
081        }
082
083        if (goalPrefix == null || goalPrefix.isEmpty()) {
084            goalPrefix = getDefaultGoalPrefix(project);
085        }
086        if (goalPrefix == null || goalPrefix.isEmpty()) {
087            throw new MojoExecutionException("You need to specify a goalPrefix as it can not be correctly computed");
088        }
089
090        generate();
091    }
092
093    static String getDefaultGoalPrefix(MavenProject project) {
094        String artifactId = project.getArtifactId();
095        if (artifactId.endsWith("-maven-plugin")) {
096            return artifactId.substring(0, artifactId.length() - "-maven-plugin".length());
097        } else if (artifactId.startsWith("maven-") && artifactId.endsWith("-plugin")) {
098            return artifactId.substring("maven-".length(), artifactId.length() - "-plugin".length());
099        } else {
100            return null;
101        }
102    }
103}