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;
20  
21  import java.util.List;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.model.Plugin;
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.plugin.descriptor.PluginDescriptor;
27  import org.apache.maven.project.MavenProject;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.graph.DependencyFilter;
30  import org.eclipse.aether.repository.RemoteRepository;
31  
32  /**
33   * Provides basic services to manage Maven plugins and their mojos. This component is kept general in its design such
34   * that the plugins/mojos can be used in arbitrary contexts. In particular, the mojos can be used for ordinary build
35   * plugins as well as special purpose plugins like reports.
36   *
37   * @since 3.0
38   * @author Benjamin Bentmann
39   */
40  public interface MavenPluginManager {
41  
42      /**
43       * Retrieves the descriptor for the specified plugin from its main artifact.
44       *
45       * @param plugin The plugin whose descriptor should be retrieved, must not be {@code null}.
46       * @param repositories The plugin repositories to use for resolving the plugin's main artifact, must not be {@code
47       *            null}.
48       * @param session The repository session to use for resolving the plugin's main artifact, must not be {@code null}.
49       * @return The plugin descriptor, never {@code null}.
50       */
51      PluginDescriptor getPluginDescriptor(
52              Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session)
53              throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException;
54  
55      /**
56       * Retrieves the descriptor for the specified plugin goal from the plugin's main artifact.
57       *
58       * @param plugin The plugin whose mojo descriptor should be retrieved, must not be {@code null}.
59       * @param goal The simple name of the mojo whose descriptor should be retrieved, must not be {@code null}.
60       * @param repositories The plugin repositories to use for resolving the plugin's main artifact, must not be {@code
61       *            null}.
62       * @param session The repository session to use for resolving the plugin's main artifact, must not be {@code null}.
63       * @return The mojo descriptor, never {@code null}.
64       */
65      MojoDescriptor getMojoDescriptor(
66              Plugin plugin, String goal, List<RemoteRepository> repositories, RepositorySystemSession session)
67              throws MojoNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
68                      InvalidPluginDescriptorException;
69  
70      /**
71       * Verifies the specified plugin is compatible with the current Maven runtime.
72       *
73       * @param pluginDescriptor The descriptor of the plugin to check, must not be {@code null}.
74       * @deprecated Use {@link #checkPrerequisites(PluginDescriptor)} instead.
75       */
76      @Deprecated
77      default void checkRequiredMavenVersion(PluginDescriptor pluginDescriptor) throws PluginIncompatibleException {
78          checkPrerequisites(pluginDescriptor);
79      }
80  
81      /**
82       * Verifies that the specified plugin's prerequisites are met.
83       *
84       * @param pluginDescriptor The descriptor of the plugin to check, must not be {@code null}.
85       * @since 3.9.12
86       */
87      default void checkPrerequisites(PluginDescriptor pluginDescriptor) throws PluginIncompatibleException {
88          checkRequiredMavenVersion(pluginDescriptor);
89      }
90  
91      /**
92       * Sets up the class realm for the specified plugin. Both the class realm and the plugin artifacts that constitute
93       * it will be stored in the plugin descriptor.
94       *
95       * @param pluginDescriptor The plugin descriptor in which to save the class realm and the plugin artifacts, must not
96       *            be {@code null}.
97       * @param session The build session from which to pick the current project and repository settings, must not be
98       *            {@code null}.
99       * @param parent The parent class realm for the plugin, may be {@code null} to use the Maven core realm.
100      * @param imports The packages/types to import from the parent realm, may be {@code null}.
101      * @param filter The filter used to exclude certain plugin dependencies, may be {@code null}.
102      */
103     void setupPluginRealm(
104             PluginDescriptor pluginDescriptor,
105             MavenSession session,
106             ClassLoader parent,
107             List<String> imports,
108             DependencyFilter filter)
109             throws PluginResolutionException, PluginContainerException;
110 
111     /**
112      * Sets up class realm for the specified build extensions plugin.
113      *
114      * @since 3.3.0
115      */
116     ExtensionRealmCache.CacheRecord setupExtensionsRealm(
117             MavenProject project, Plugin plugin, RepositorySystemSession session) throws PluginManagerException;
118 
119     /**
120      * Looks up the mojo for the specified mojo execution and populates its parameters from the configuration given by
121      * the mojo execution. The mojo/plugin descriptor associated with the mojo execution provides the class realm to
122      * lookup the mojo from. <strong>Warning:</strong> The returned mojo instance must be released via
123      * {@link #releaseMojo(Object, MojoExecution)} when the mojo is no longer needed to free any resources allocated for
124      * it.
125      *
126      * @param mojoInterface The component role of the mojo, must not be {@code null}.
127      * @param session The build session in whose context the mojo will be used, must not be {@code null}.
128      * @param mojoExecution The mojo execution to retrieve the mojo for, must not be {@code null}.
129      * @return The ready-to-execute mojo, never {@code null}.
130      */
131     <T> T getConfiguredMojo(Class<T> mojoInterface, MavenSession session, MojoExecution mojoExecution)
132             throws PluginConfigurationException, PluginContainerException;
133 
134     /**
135      * Releases the specified mojo back to the container.
136      *
137      * @param mojo The mojo to release, may be {@code null}.
138      * @param mojoExecution The mojo execution the mojo was originally retrieved for, must not be {@code null}.
139      */
140     void releaseMojo(Object mojo, MojoExecution mojoExecution);
141 }