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   *
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      @Override
76      public void execute()
77          throws MojoExecutionException
78      {
79          if ( skip )
80          {
81              getLog().warn( "Execution skipped" );
82              return;
83          }
84          Artifact projectArtifact = project.getArtifact();
85  
86          Versioning versioning = new Versioning();
87          versioning.setLatest( projectArtifact.getVersion() );
88          versioning.updateTimestamp();
89          ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning );
90          projectArtifact.addMetadata( metadata );
91  
92          GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() );
93          groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() );
94  
95          projectArtifact.addMetadata( groupMetadata );
96      }
97  
98      /**
99       * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
100      */
101     private String getGoalPrefix()
102     {
103         if ( goalPrefix == null )
104         {
105             goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
106         }
107 
108         return goalPrefix;
109     }
110 }