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.metadata;
020
021import javax.inject.Inject;
022
023import org.apache.maven.plugin.AbstractMojo;
024import org.apache.maven.plugin.MojoExecutionException;
025import org.apache.maven.plugin.descriptor.PluginDescriptor;
026import org.apache.maven.plugins.annotations.LifecyclePhase;
027import org.apache.maven.plugins.annotations.Mojo;
028import org.apache.maven.plugins.annotations.Parameter;
029import org.apache.maven.project.MavenProject;
030import org.apache.maven.rtinfo.RuntimeInformation;
031import org.eclipse.aether.util.version.GenericVersionScheme;
032import org.eclipse.aether.version.InvalidVersionSpecificationException;
033import org.eclipse.aether.version.VersionScheme;
034
035/**
036 * Inject any plugin-specific
037 * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's
038 * artifact, for subsequent installation and deployment.
039 * It is used:
040 * <ol>
041 * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's
042 *     artifact</li>
043 * <li>to define plugin mapping in the group</li>
044 * </ol>
045 *
046 * @see org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata
047 * @see org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata
048 *
049 * @since 2.0
050 */
051@Mojo(name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
052public class AddPluginArtifactMetadataMojo extends AbstractMojo {
053    /**
054     * The project artifact, which should have the <code>latest</code> metadata added to it.
055     */
056    private final MavenProject project;
057
058    /**
059     * The prefix for the plugin goal.
060     */
061    @Parameter
062    private String goalPrefix;
063
064    /**
065     * Set this to "true" to skip invoking any goals or reports of the plugin.
066     *
067     * @since 2.8
068     */
069    @Parameter(defaultValue = "false", property = "maven.plugin.skip")
070    private boolean skip;
071
072    private final RuntimeInformation runtimeInformation;
073
074    private final VersionScheme versionScheme = new GenericVersionScheme();
075
076    @Inject
077    public AddPluginArtifactMetadataMojo(MavenProject project, RuntimeInformation runtimeInformation) {
078        this.project = project;
079        this.runtimeInformation = runtimeInformation;
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void execute() throws MojoExecutionException {
085        if (skip) {
086            getLog().warn("Execution skipped");
087            return;
088        }
089        // nothing if Maven is 3.9+
090        try {
091            if (versionScheme
092                            .parseVersion("3.9.0")
093                            .compareTo(versionScheme.parseVersion(runtimeInformation.getMavenVersion()))
094                    < 1) {
095                getLog().info("This Mojo is not used in Maven version 3.9.0 and above");
096                return;
097            }
098        } catch (InvalidVersionSpecificationException e) {
099            // not happening with generic
100            throw new MojoExecutionException(e);
101        }
102
103        LegacySupport.execute(project, getGoalPrefix());
104    }
105
106    /**
107     * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
108     */
109    private String getGoalPrefix() {
110        if (goalPrefix == null) {
111            goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
112        }
113
114        return goalPrefix;
115    }
116}