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.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 051 extends AbstractMojo 052{ 053 /** 054 * The project artifact, which should have the <code>latest</code> metadata added to it. 055 */ 056 @Parameter( defaultValue = "${project}", readonly = true ) 057 private MavenProject project; 058 059 /** 060 * The prefix for the plugin goal. 061 */ 062 @Parameter 063 private String goalPrefix; 064 065 /** 066 * Set this to "true" to skip invoking any goals or reports of the plugin. 067 * 068 * @since 2.8 069 */ 070 @Parameter( defaultValue = "false", property = "maven.plugin.skip" ) 071 private boolean skip; 072 073 /** {@inheritDoc} */ 074 @Override 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}