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.LifecyclePhase;
029import org.apache.maven.plugins.annotations.Mojo;
030import org.apache.maven.plugins.annotations.Parameter;
031import org.apache.maven.project.MavenProject;
032
033/**
034 * Inject any plugin-specific
035 * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's
036 * artifact, for subsequent installation and deployment.
037 * It is used:
038 * <ol>
039 * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's
040 *     artifact</li>
041 * <li>to define plugin mapping in the group</li>
042 * </ol>
043 *
044 * @see ArtifactRepositoryMetadata
045 * @see GroupRepositoryMetadata
046 *
047 * @since 2.0
048 */
049@Mojo(name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
050public class AddPluginArtifactMetadataMojo extends AbstractMojo {
051    /**
052     * The project artifact, which should have the <code>latest</code> metadata added to it.
053     */
054    @Parameter(defaultValue = "${project}", readonly = true)
055    private MavenProject project;
056
057    /**
058     * The prefix for the plugin goal.
059     */
060    @Parameter
061    private String goalPrefix;
062
063    /**
064     * Set this to "true" to skip invoking any goals or reports of the plugin.
065     *
066     * @since 2.8
067     */
068    @Parameter(defaultValue = "false", property = "maven.plugin.skip")
069    private boolean skip;
070
071    /** {@inheritDoc} */
072    @Override
073    public void execute() throws MojoExecutionException {
074        if (skip) {
075            getLog().warn("Execution skipped");
076            return;
077        }
078        Artifact projectArtifact = project.getArtifact();
079
080        Versioning versioning = new Versioning();
081        versioning.setLatest(projectArtifact.getVersion());
082        versioning.updateTimestamp();
083        ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata(projectArtifact, versioning);
084        projectArtifact.addMetadata(metadata);
085
086        GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata(project.getGroupId());
087        groupMetadata.addPluginMapping(getGoalPrefix(), project.getArtifactId(), project.getName());
088
089        projectArtifact.addMetadata(groupMetadata);
090    }
091
092    /**
093     * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
094     */
095    private String getGoalPrefix() {
096        if (goalPrefix == null) {
097            goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
098        }
099
100        return goalPrefix;
101    }
102}