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.project.MavenProject;
30  
31  /**
32   * Inject any plugin-specific
33   * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's
34   * artifact, for subsequent installation and deployment.
35   * It is used:
36   * <ol>
37   * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's artifact</li>
38   * <li>to define plugin mapping in the group</li>
39   * </ol>
40   *
41   * @see ArtifactRepositoryMetadata
42   * @see GroupRepositoryMetadata
43   * @version $Id: AddPluginArtifactMetadataMojo.java 1337501 2012-05-12 10:29:14Z olamy $
44   * @since 2.0
45   * @phase package
46   * @goal addPluginArtifactMetadata
47   * @threadSafe
48   */
49  public class AddPluginArtifactMetadataMojo
50      extends AbstractMojo
51  {
52      /**
53       * The project artifact, which should have the <code>latest</code> metadata added to it.
54       *
55       * @parameter default-value="${project}"
56       * @required
57       * @readonly
58       */
59      private MavenProject project;
60  
61      /**
62       * The prefix for the plugin goal.
63       *
64       * @parameter
65       */
66      private String goalPrefix;
67  
68      /**
69       * Set this to "true" to skip invoking any goals or reports of the plugin.
70       *
71       * @parameter default-value="false" expression="${maven.plugin.skip}"
72       * @since 2.8
73       */
74      private boolean skip;
75  
76      /** {@inheritDoc} */
77      public void execute()
78          throws MojoExecutionException
79      {
80          if ( skip )
81          {
82              getLog().warn( "Execution skipped" );
83              return;
84          }
85          Artifact projectArtifact = project.getArtifact();
86  
87          Versioning versioning = new Versioning();
88          versioning.setLatest( projectArtifact.getVersion() );
89          versioning.updateTimestamp();
90          ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( projectArtifact, versioning );
91          projectArtifact.addMetadata( metadata );
92  
93          GroupRepositoryMetadata groupMetadata = new GroupRepositoryMetadata( project.getGroupId() );
94          groupMetadata.addPluginMapping( getGoalPrefix(), project.getArtifactId(), project.getName() );
95  
96          projectArtifact.addMetadata( groupMetadata );
97      }
98  
99      /**
100      * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
101      */
102     private String getGoalPrefix()
103     {
104         if ( goalPrefix == null )
105         {
106             goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
107         }
108 
109         return goalPrefix;
110     }
111 }