View Javadoc
1   package org.apache.maven.plugin.plugin.metadata;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
24  import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25  import org.apache.maven.artifact.repository.metadata.Versioning;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.project.MavenProject;
33  
34  /**
35   * Inject any plugin-specific
36   * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's
37   * artifact, for subsequent installation and deployment.
38   * It is used:
39   * <ol>
40   * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's
41   *     artifact</li>
42   * <li>to define plugin mapping in the group</li>
43   * </ol>
44   *
45   * @see ArtifactRepositoryMetadata
46   * @see GroupRepositoryMetadata
47   * @version $Id: AddPluginArtifactMetadataMojo.html 995996 2016-08-26 22:31:42Z rfscholte $
48   * @since 2.0
49   */
50  @Mojo( name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true )
51  public class AddPluginArtifactMetadataMojo
52      extends AbstractMojo
53  {
54      /**
55       * The project artifact, which should have the <code>latest</code> metadata added to it.
56       */
57      @Parameter( defaultValue = "${project}", readonly = true )
58      private MavenProject project;
59  
60      /**
61       * The prefix for the plugin goal.
62       */
63      @Parameter
64      private String goalPrefix;
65  
66      /**
67       * Set this to "true" to skip invoking any goals or reports of the plugin.
68       *
69       * @since 2.8
70       */
71      @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
72      private boolean skip;
73  
74      /** {@inheritDoc} */
75      public void execute()
76          throws MojoExecutionException
77      {
78          if ( skip )
79          {
80              getLog().warn( "Execution skipped" );
81              return;
82          }
83          Artifact projectArtifact = project.getArtifact();
84  
85          Versioning versioning = new Versioning();
86          versioning.setLatest( projectArtifact.getVersion() );
87          versioning.updateTimestamp();
88          ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning );
89          projectArtifact.addMetadata( metadata );
90  
91          GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() );
92          groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() );
93  
94          projectArtifact.addMetadata( groupMetadata );
95      }
96  
97      /**
98       * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
99       */
100     private String getGoalPrefix()
101     {
102         if ( goalPrefix == null )
103         {
104             goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
105         }
106 
107         return goalPrefix;
108     }
109 }