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.api;
20  
21  import java.nio.file.Path;
22  import java.time.Instant;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.NoSuchElementException;
27  import java.util.Optional;
28  
29  import org.apache.maven.api.annotations.Experimental;
30  import org.apache.maven.api.annotations.Nonnull;
31  import org.apache.maven.api.annotations.ThreadSafe;
32  import org.apache.maven.api.model.Repository;
33  import org.apache.maven.api.services.DependencyCoordinateFactory;
34  import org.apache.maven.api.settings.Settings;
35  
36  /**
37   * The session to install / deploy / resolve artifacts and dependencies.
38   *
39   * @since 4.0.0
40   */
41  @Experimental
42  @ThreadSafe
43  public interface Session {
44  
45      @Nonnull
46      Settings getSettings();
47  
48      @Nonnull
49      LocalRepository getLocalRepository();
50  
51      @Nonnull
52      List<RemoteRepository> getRemoteRepositories();
53  
54      @Nonnull
55      SessionData getData();
56  
57      /**
58       * Gets the user properties to use for interpolation. The user properties have been configured directly by the user,
59       * e.g. via the {@code -Dkey=value} parameter on the command line.
60       *
61       * @return the user properties, never {@code null}
62       */
63      @Nonnull
64      Map<String, String> getUserProperties();
65  
66      /**
67       * Gets the system properties to use for interpolation. The system properties are collected from the runtime
68       * environment such as {@link System#getProperties()} and environment variables.
69       *
70       * @return the system properties, never {@code null}
71       */
72      @Nonnull
73      Map<String, String> getSystemProperties();
74  
75      /**
76       * Returns the current maven version
77       * @return the maven version, never {@code null}
78       */
79      @Nonnull
80      String getMavenVersion();
81  
82      int getDegreeOfConcurrency();
83  
84      @Nonnull
85      Instant getStartTime();
86  
87      /**
88       * Gets the directory of the topmost project being built, usually the current directory or the
89       * directory pointed at by the {@code -f/--file} command line argument.
90       */
91      @Nonnull
92      Path getTopDirectory();
93  
94      /**
95       * Gets the root directory of the session, which is the root directory for the top directory project.
96       *
97       * @throws IllegalStateException if the root directory could not be found
98       * @see #getTopDirectory()
99       * @see Project#getRootDirectory()
100      */
101     @Nonnull
102     Path getRootDirectory();
103 
104     @Nonnull
105     List<Project> getProjects();
106 
107     /**
108      * Returns the plugin context for mojo being executed and the specified
109      * {@link Project}, never returns {@code null} as if context not present, creates it.
110      *
111      * <strong>Implementation note:</strong> while this method return type is {@link Map}, the
112      * returned map instance implements {@link java.util.concurrent.ConcurrentMap} as well.
113      *
114      * @throws org.apache.maven.api.services.MavenException if not called from the within a mojo execution
115      */
116     @Nonnull
117     Map<String, Object> getPluginContext(@Nonnull Project project);
118 
119     /**
120      * Retrieves the service for the interface
121      *
122      * @throws NoSuchElementException if the service could not be found
123      */
124     @Nonnull
125     <T extends Service> T getService(@Nonnull Class<T> clazz);
126 
127     /**
128      * Creates a derived session using the given local repository.
129      *
130      * @param localRepository the new local repository
131      * @return the derived session
132      * @throws NullPointerException if {@code localRepository} is null
133      */
134     @Nonnull
135     Session withLocalRepository(@Nonnull LocalRepository localRepository);
136 
137     /**
138      * Creates a derived session using the given remote repositories.
139      *
140      * @param repositories the new list of remote repositories
141      * @return the derived session
142      * @throws NullPointerException if {@code repositories} is null
143      */
144     @Nonnull
145     Session withRemoteRepositories(@Nonnull List<RemoteRepository> repositories);
146 
147     /**
148      * Register the given listener which will receive all events.
149      *
150      * @param listener the listener to register
151      * @throws NullPointerException if {@code listener} is null
152      */
153     void registerListener(@Nonnull Listener listener);
154 
155     /**
156      * Unregisters a previously registered listener.
157      *
158      * @param listener the listener to unregister
159      * @throws NullPointerException if {@code listener} is null
160      */
161     void unregisterListener(@Nonnull Listener listener);
162 
163     /**
164      * Returns the list of registered listeners.
165      *
166      * @return an immutable collection of listeners, never {@code null}
167      */
168     @Nonnull
169     Collection<Listener> getListeners();
170 
171     /**
172      * Shortcut for <code>getService(RepositoryFactory.class).createLocal(...)</code>
173      * @see org.apache.maven.api.services.RepositoryFactory#createLocal(Path)
174      */
175     LocalRepository createLocalRepository(Path path);
176 
177     /**
178      * Shortcut for <code>getService(RepositoryFactory.class).createRemote(...)</code>
179      * @see org.apache.maven.api.services.RepositoryFactory#createRemote(String, String)
180      */
181     @Nonnull
182     RemoteRepository createRemoteRepository(@Nonnull String id, @Nonnull String url);
183 
184     /**
185      * Shortcut for <code>getService(RepositoryFactory.class).createRemote(...)</code>
186      * @see org.apache.maven.api.services.RepositoryFactory#createRemote(Repository)
187      */
188     @Nonnull
189     RemoteRepository createRemoteRepository(@Nonnull Repository repository);
190 
191     /**
192      * Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
193      * @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String)
194      */
195     ArtifactCoordinate createArtifactCoordinate(String groupId, String artifactId, String version, String extension);
196 
197     /**
198      * Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
199      * @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String, String, String)
200      */
201     ArtifactCoordinate createArtifactCoordinate(
202             String groupId, String artifactId, String version, String classifier, String extension, String type);
203 
204     /**
205      * Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
206      * @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String, String, String)
207      */
208     ArtifactCoordinate createArtifactCoordinate(Artifact artifact);
209 
210     /**
211      * Shortcut for <code>getService(DependencyFactory.class).create(...)</code>
212      * @see DependencyCoordinateFactory#create(Session, ArtifactCoordinate)
213      */
214     @Nonnull
215     DependencyCoordinate createDependencyCoordinate(@Nonnull ArtifactCoordinate coordinate);
216 
217     /**
218      * Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
219      * @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String)
220      */
221     Artifact createArtifact(String groupId, String artifactId, String version, String extension);
222 
223     /**
224      * Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
225      * @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String, String, String)
226      */
227     Artifact createArtifact(
228             String groupId, String artifactId, String version, String classifier, String extension, String type);
229 
230     /**
231      * Shortcut for <code>getService(ArtifactResolver.class).resolve(...)</code>
232      * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection)
233      *
234      * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed
235      */
236     Artifact resolveArtifact(ArtifactCoordinate coordinate);
237 
238     /**
239      * Shortcut for <code>getService(ArtifactResolver.class).resolve(...)</code>
240      * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection)
241      *
242      * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed
243      */
244     Collection<Artifact> resolveArtifacts(ArtifactCoordinate... coordinates);
245 
246     /**
247      * Shortcut for <code>getService(ArtifactResolver.class).resolve(...)</code>
248      * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection)
249      *
250      * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed
251      */
252     Collection<Artifact> resolveArtifacts(Collection<? extends ArtifactCoordinate> coordinates);
253 
254     /**
255      * Shortcut for <code>getService(ArtifactResolver.class).resolve(...)</code>
256      * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection)
257      *
258      * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed
259      */
260     Artifact resolveArtifact(Artifact artifact);
261 
262     /**
263      * Shortcut for <code>getService(ArtifactResolver.class).resolve(...)</code>
264      * @see org.apache.maven.api.services.ArtifactResolver#resolve(Session, Collection)
265      *
266      * @throws org.apache.maven.api.services.ArtifactResolverException if the artifact resolution failed
267      */
268     Collection<Artifact> resolveArtifacts(Artifact... artifacts);
269 
270     /**
271      * Shortcut for {@code getService(ArtifactInstaller.class).install(...)}
272      * @see org.apache.maven.api.services.ArtifactInstaller#install(Session, Collection)
273      *
274      * @throws org.apache.maven.api.services.ArtifactInstallerException if the artifacts installation failed
275      */
276     void installArtifacts(Artifact... artifacts);
277 
278     /**
279      * Shortcut for {@code getService(ArtifactInstaller.class).install(...)}
280      * @see org.apache.maven.api.services.ArtifactInstaller#install(Session, Collection)
281      *
282      * @throws org.apache.maven.api.services.ArtifactInstallerException if the artifacts installation failed
283      */
284     void installArtifacts(Collection<Artifact> artifacts);
285 
286     /**
287      * Shortcut for <code>getService(ArtifactDeployer.class).deploy(...)</code>
288      * @see org.apache.maven.api.services.ArtifactDeployer#deploy(Session, RemoteRepository, Collection)
289      *
290      * @throws org.apache.maven.api.services.ArtifactDeployerException if the artifacts deployment failed
291      */
292     void deployArtifact(RemoteRepository repository, Artifact... artifacts);
293 
294     /**
295      * Shortcut for <code>getService(ArtifactManager.class).setPath(...)</code>
296      * @see org.apache.maven.api.services.ArtifactManager#setPath(Artifact, Path)
297      */
298     void setArtifactPath(@Nonnull Artifact artifact, @Nonnull Path path);
299 
300     /**
301      * Shortcut for <code>getService(ArtifactManager.class).getPath(...)</code>
302      * @see org.apache.maven.api.services.ArtifactManager#getPath(Artifact)
303      */
304     @Nonnull
305     Optional<Path> getArtifactPath(@Nonnull Artifact artifact);
306 
307     /**
308      * Shortcut for <code>getService(ArtifactManager.class).isSnapshot(...)</code>
309      * @see org.apache.maven.api.services.VersionParser#isSnapshot(String)
310      */
311     boolean isVersionSnapshot(@Nonnull String version);
312 
313     /**
314      * Shortcut for <code>getService(DependencyCollector.class).collect(...)</code>
315      * @see org.apache.maven.api.services.DependencyCollector#collect(Session, Artifact)
316      *
317      * @throws org.apache.maven.api.services.DependencyCollectorException if the dependency collection failed
318      */
319     @Nonnull
320     Node collectDependencies(@Nonnull Artifact artifact);
321 
322     /**
323      * Shortcut for <code>getService(DependencyCollector.class).collect(...)</code>
324      * @see org.apache.maven.api.services.DependencyCollector#collect(Session, Project)
325      *
326      * @throws org.apache.maven.api.services.DependencyCollectorException if the dependency collection failed
327      */
328     @Nonnull
329     Node collectDependencies(@Nonnull Project project);
330 
331     /**
332      * Shortcut for <code>getService(DependencyCollector.class).resolve(...)</code>
333      * @see org.apache.maven.api.services.DependencyCollector#collect(Session, DependencyCoordinate)
334      *
335      * @throws org.apache.maven.api.services.DependencyCollectorException if the dependency collection failed
336      */
337     @Nonnull
338     Node collectDependencies(@Nonnull DependencyCoordinate dependency);
339 
340     Path getPathForLocalArtifact(@Nonnull Artifact artifact);
341 
342     Path getPathForRemoteArtifact(RemoteRepository remote, Artifact artifact);
343 
344     /**
345      * Shortcut for <code>getService(VersionParser.class).parseVersion(...)</code>
346      * @see org.apache.maven.api.services.VersionParser#parseVersion(String)
347      *
348      * @throws org.apache.maven.api.services.VersionParserException if the parsing failed
349      */
350     @Nonnull
351     Version parseVersion(@Nonnull String version);
352 
353     /**
354      * Shortcut for <code>getService(VersionParser.class).parseVersionRange(...)</code>
355      * @see org.apache.maven.api.services.VersionParser#parseVersionRange(String)
356      *
357      * @throws org.apache.maven.api.services.VersionParserException if the parsing failed
358      */
359     @Nonnull
360     VersionRange parseVersionRange(@Nonnull String versionRange);
361 }