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