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 javax.inject.Inject;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.rtinfo.RuntimeInformation;
31  import org.eclipse.aether.util.version.GenericVersionScheme;
32  import org.eclipse.aether.version.InvalidVersionSpecificationException;
33  import org.eclipse.aether.version.VersionScheme;
34  
35  /**
36   * Inject any plugin-specific
37   * <a href="/ref/current/maven-repository-metadata/repository-metadata.html">artifact metadata</a> to the project's
38   * artifact, for subsequent installation and deployment.
39   * It is used:
40   * <ol>
41   * <li>to add the <code>latest</code> metadata (which is plugin-specific) for shipping alongside the plugin's
42   *     artifact</li>
43   * <li>to define plugin mapping in the group</li>
44   * </ol>
45   *
46   * @see org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata
47   * @see org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata
48   *
49   * @since 2.0
50   */
51  @Mojo(name = "addPluginArtifactMetadata", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
52  public class AddPluginArtifactMetadataMojo extends AbstractMojo {
53      /**
54       * The project artifact, which should have the <code>latest</code> metadata added to it.
55       */
56      private final 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      private final RuntimeInformation runtimeInformation;
73  
74      private final VersionScheme versionScheme = new GenericVersionScheme();
75  
76      @Inject
77      public AddPluginArtifactMetadataMojo(MavenProject project, RuntimeInformation runtimeInformation) {
78          this.project = project;
79          this.runtimeInformation = runtimeInformation;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public void execute() throws MojoExecutionException {
85          if (skip) {
86              getLog().warn("Execution skipped");
87              return;
88          }
89          // nothing if Maven is 3.9+
90          try {
91              if (versionScheme
92                              .parseVersion("3.9.0")
93                              .compareTo(versionScheme.parseVersion(runtimeInformation.getMavenVersion()))
94                      < 1) {
95                  getLog().info("This Mojo is not used in Maven version 3.9.0 and above");
96                  return;
97              }
98          } catch (InvalidVersionSpecificationException e) {
99              // not happening with generic
100             throw new MojoExecutionException(e);
101         }
102 
103         LegacySupport.execute(project, getGoalPrefix());
104     }
105 
106     /**
107      * @return the goal prefix parameter or the goal prefix from the Plugin artifactId.
108      */
109     private String getGoalPrefix() {
110         if (goalPrefix == null) {
111             goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
112         }
113 
114         return goalPrefix;
115     }
116 }