001package org.apache.maven.plugin.plugin.metadata;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.artifact.Artifact;
023import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
024import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
025import org.apache.maven.artifact.repository.metadata.Versioning;
026import org.apache.maven.plugin.AbstractMojo;
027import org.apache.maven.plugin.MojoExecutionException;
028import org.apache.maven.plugin.descriptor.PluginDescriptor;
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 * @version $Id: AddPluginArtifactMetadataMojo.html 1030109 2018-05-20 14:45:18Z hboutemy $
048 * @since 2.0
049 */
050@Mojo( name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true )
051public class AddPluginArtifactMetadataMojo
052    extends AbstractMojo
053{
054    /**
055     * The project artifact, which should have the <code>latest</code> metadata added to it.
056     */
057    @Parameter( defaultValue = "${project}", readonly = true )
058    private MavenProject project;
059
060    /**
061     * The prefix for the plugin goal.
062     */
063    @Parameter
064    private String goalPrefix;
065
066    /**
067     * Set this to "true" to skip invoking any goals or reports of the plugin.
068     *
069     * @since 2.8
070     */
071    @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
072    private boolean skip;
073
074    /** {@inheritDoc} */
075    public void execute()
076        throws MojoExecutionException
077    {
078        if ( skip )
079        {
080            getLog().warn( "Execution skipped" );
081            return;
082        }
083        Artifact projectArtifact = project.getArtifact();
084
085        Versioning versioning = new Versioning();
086        versioning.setLatest( projectArtifact.getVersion() );
087        versioning.updateTimestamp();
088        ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning );
089        projectArtifact.addMetadata( metadata );
090
091        GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() );
092        groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() );
093
094        projectArtifact.addMetadata( groupMetadata );
095    }
096
097    /**
098     * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
099     */
100    private String getGoalPrefix()
101    {
102        if ( goalPrefix == null )
103        {
104            goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
105        }
106
107        return goalPrefix;
108    }
109}