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 javax.lang.model.SourceVersion; 022 023import java.io.File; 024import java.util.Arrays; 025import java.util.stream.Collectors; 026 027import org.apache.maven.plugin.MojoExecutionException; 028import org.apache.maven.plugins.annotations.Component; 029import org.apache.maven.plugins.annotations.LifecyclePhase; 030import org.apache.maven.plugins.annotations.Mojo; 031import org.apache.maven.plugins.annotations.Parameter; 032import org.apache.maven.plugins.annotations.ResolutionScope; 033import org.apache.maven.tools.plugin.generator.GeneratorException; 034import org.apache.maven.tools.plugin.generator.PluginHelpGenerator; 035import org.codehaus.plexus.util.StringUtils; 036import org.codehaus.plexus.velocity.VelocityComponent; 037 038/** 039 * Generates a <code>HelpMojo</code> class. 040 * Relies at runtime on one output file from {@link DescriptorGeneratorMojo}. 041 * 042 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 043 * @since 2.4 044 */ 045@Mojo( 046 name = "helpmojo", 047 defaultPhase = LifecyclePhase.GENERATE_SOURCES, 048 threadSafe = true, 049 requiresDependencyResolution = ResolutionScope.COMPILE) 050public class HelpGeneratorMojo extends AbstractGeneratorMojo { 051 /** 052 * The directory where the generated <code>HelpMojo</code> file will be put. 053 */ 054 @Parameter(defaultValue = "${project.build.directory}/generated-sources/plugin") 055 protected File outputDirectory; 056 057 /** 058 * The name of the package for the generated <code>HelpMojo</code>. 059 * <p> 060 * By default, the package name will be calculated as <code>groupId + "." + artifactId</code> with additional 061 * <ul> 062 * <li><code>-</code> (dashes) will be replaced by <code>_</code> (underscores)</li> 063 * <li><code>_</code> (underscore) will be added before each number or Java keyword at the beginning of name</li> 064 * </ul> 065 * 066 * @since 2.6 067 */ 068 @Parameter 069 private String helpPackageName; 070 071 /** 072 * Velocity component. 073 */ 074 @Component 075 private VelocityComponent velocity; 076 077 String getHelpPackageName() { 078 String packageName = null; 079 if (StringUtils.isNotBlank(helpPackageName)) { 080 packageName = helpPackageName; 081 } 082 083 if (packageName == null) { 084 packageName = project.getGroupId() + "." + project.getArtifactId(); 085 packageName = packageName.replace("-", "_"); 086 087 String[] packageItems = packageName.split("\\."); 088 packageName = 089 Arrays.stream(packageItems).map(this::prefixSpecialCase).collect(Collectors.joining(".")); 090 } 091 092 return packageName; 093 } 094 095 private String prefixSpecialCase(String name) { 096 if (SourceVersion.isKeyword(name) || !Character.isJavaIdentifierStart(name.charAt(0))) { 097 name = "_" + name; 098 } 099 return name; 100 } 101 102 @Override 103 protected void generate() throws MojoExecutionException { 104 PluginHelpGenerator pluginHelpGenerator = new PluginHelpGenerator() 105 .setMavenProject(project) 106 .setHelpPackageName(getHelpPackageName()) 107 .setGoalPrefix(goalPrefix) 108 .setVelocityComponent(velocity); 109 110 try { 111 pluginHelpGenerator.execute(outputDirectory); 112 } catch (GeneratorException e) { 113 throw new MojoExecutionException(e.getMessage(), e); 114 } 115 116 if (!project.getCompileSourceRoots().contains(outputDirectory.getAbsolutePath())) { 117 project.addCompileSourceRoot(outputDirectory.getAbsolutePath()); 118 } 119 } 120}