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