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