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.artifact.Artifact;
022import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
023import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
024import org.apache.maven.artifact.repository.metadata.Versioning;
025import org.apache.maven.plugin.AbstractMojo;
026import org.apache.maven.plugin.MojoExecutionException;
027import org.apache.maven.plugin.descriptor.PluginDescriptor;
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.project.MavenProject;
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 ArtifactRepositoryMetadata
046 * @see 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    /** {@inheritDoc} */
073    @Override
074    public void execute() throws MojoExecutionException {
075        if (skip) {
076            getLog().warn("Execution skipped");
077            return;
078        }
079        Artifact projectArtifact = project.getArtifact();
080
081        Versioning versioning = new Versioning();
082        versioning.setLatest(projectArtifact.getVersion());
083        versioning.updateTimestamp();
084        ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata(projectArtifact, versioning);
085        projectArtifact.addMetadata(metadata);
086
087        GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata(project.getGroupId());
088        groupMetadata.addPluginMapping(getGoalPrefix(), project.getArtifactId(), project.getName());
089
090        projectArtifact.addMetadata(groupMetadata);
091    }
092
093    /**
094     * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
095     */
096    private String getGoalPrefix() {
097        if (goalPrefix == null) {
098            goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
099        }
100
101        return goalPrefix;
102    }
103}