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.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.descriptor.PluginDescriptor;
24  import org.apache.maven.plugins.annotations.Component;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.rtinfo.RuntimeInformation;
30  import org.eclipse.aether.util.version.GenericVersionScheme;
31  import org.eclipse.aether.version.InvalidVersionSpecificationException;
32  import org.eclipse.aether.version.VersionScheme;
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 org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata
46   * @see org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata
47   *
48   * @since 2.0
49   */
50  @Mojo(name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
51  public class AddPluginArtifactMetadataMojo extends AbstractMojo {
52      /**
53       * The project artifact, which should have the <code>latest</code> metadata added to it.
54       */
55      @Component
56      private MavenProject project;
57  
58      /**
59       * The prefix for the plugin goal.
60       */
61      @Parameter
62      private String goalPrefix;
63  
64      /**
65       * Set this to "true" to skip invoking any goals or reports of the plugin.
66       *
67       * @since 2.8
68       */
69      @Parameter(defaultValue = "false", property = "maven.plugin.skip")
70      private boolean skip;
71  
72      @Component
73      private RuntimeInformation runtimeInformation;
74  
75      private final VersionScheme versionScheme = new GenericVersionScheme();
76  
77      /** {@inheritDoc} */
78      @Override
79      public void execute() throws MojoExecutionException {
80          if (skip) {
81              getLog().warn("Execution skipped");
82              return;
83          }
84          // nothing if Maven is 3.9+
85          try {
86              if (versionScheme
87                              .parseVersion("3.9.0")
88                              .compareTo(versionScheme.parseVersion(runtimeInformation.getMavenVersion()))
89                      < 1) {
90                  getLog().info("This Mojo is not used in Maven version 3.9.0 and above");
91                  return;
92              }
93          } catch (InvalidVersionSpecificationException e) {
94              // not happening with generic
95              throw new MojoExecutionException(e);
96          }
97  
98          LegacySupport.execute(project, getGoalPrefix());
99      }
100 
101     /**
102      * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
103      */
104     private String getGoalPrefix() {
105         if (goalPrefix == null) {
106             goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
107         }
108 
109         return goalPrefix;
110     }
111 }