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