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