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.services;
20  
21  import java.nio.file.Path;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Optional;
26  
27  import org.apache.maven.api.Artifact;
28  import org.apache.maven.api.ProducedArtifact;
29  import org.apache.maven.api.Project;
30  import org.apache.maven.api.ProjectScope;
31  import org.apache.maven.api.RemoteRepository;
32  import org.apache.maven.api.Service;
33  import org.apache.maven.api.Session;
34  import org.apache.maven.api.annotations.Experimental;
35  import org.apache.maven.api.annotations.Nonnull;
36  import org.apache.maven.api.annotations.Nullable;
37  import org.apache.maven.api.model.Resource;
38  
39  /**
40   * Interface to manage the project during its lifecycle.
41   *
42   * @since 4.0.0
43   */
44  @Experimental
45  public interface ProjectManager extends Service {
46      /**
47       * Returns the path to the built project artifact file, if the project has been built.
48       *
49       * @return the path of the built project artifact
50       */
51      @Nonnull
52      Optional<Path> getPath(Project project);
53  
54      /**
55       * Returns an immutable collection of attached artifacts for given project.
56       */
57      @Nonnull
58      Collection<ProducedArtifact> getAttachedArtifacts(Project project);
59  
60      /**
61       * Returns project's all artifacts as immutable collection. The list contains all artifacts, even the attached ones,
62       * if any. Hence, the list returned by this method depends on which lifecycle step of the build was it invoked.
63       * The head of returned list is result of {@link Project#getArtifacts()} method, so same applies here: the list can have
64       * minimum of one element. The maximum number of elements is in turn dependent on build configuration and lifecycle
65       * phase when this method was invoked (i.e. is javadoc jar built and attached, is sources jar built attached, are
66       * all the artifact signed, etc.).
67       * <p>
68       * This method is shorthand for {@link Project#getArtifacts()} and {@link #getAttachedArtifacts(Project)} methods.
69       *
70       * @see org.apache.maven.api.services.ArtifactManager#getPath(Artifact)
71       */
72      Collection<ProducedArtifact> getAllArtifacts(Project project);
73  
74      default void attachArtifact(Session session, Project project, Path path) {
75          String name = path.getFileName().toString();
76          int dot = name.lastIndexOf('.');
77          String ext = dot >= 1 ? name.substring(dot + 1) : "";
78          ProducedArtifact artifact = session.createProducedArtifact(
79                  project.getGroupId(), project.getArtifactId(), project.getVersion(), ext);
80          attachArtifact(project, artifact, path);
81      }
82  
83      default void attachArtifact(Session session, Project project, String type, Path path) {
84          ProducedArtifact artifact = session.createProducedArtifact(
85                  project.getGroupId(), project.getArtifactId(), project.getVersion(), null, null, type);
86          attachArtifact(project, artifact, path);
87      }
88  
89      void attachArtifact(Project project, ProducedArtifact artifact, Path path);
90  
91      /**
92       * Obtain an immutable list of compile source roots for the given project and scope.
93       * Paths are absolute.
94       *
95       * @param project the project
96       * @param scope the scope, i.e. usually main or test
97       * @return the list of compile source roots
98       */
99      @Nonnull
100     List<Path> getCompileSourceRoots(@Nonnull Project project, @Nonnull ProjectScope scope);
101 
102     /**
103      * Add a compilation source root to the given project for the given scope.
104      * The path will be transformed into an absolute path and added to the list for the given scope,
105      * if not already present.
106      *
107      * @param project the project
108      * @param scope the scope, i.e. usually main or test
109      * @param sourceRoot the new source root
110      */
111     void addCompileSourceRoot(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Path sourceRoot);
112 
113     /**
114      * Get the list of resources for the given project and scope
115      *
116      * @param project the project
117      * @param scope the scope, i.e. usually main or test
118      * @return the list of resources
119      */
120     List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope);
121 
122     /**
123      * Add a resource set to the given project for the given scope.
124      *
125      * @param project the project
126      * @param scope the scope, i.e. usually main or test
127      * @param resource the resource set to add
128      */
129     void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource);
130 
131     /**
132      * Returns an immutable list of project remote repositories (directly specified or inherited).
133      *
134      * @param project the project
135      */
136     @Nonnull
137     List<RemoteRepository> getRemoteProjectRepositories(@Nonnull Project project);
138 
139     /**
140      * Returns an immutable list of project remote plugin repositories (directly specified or inherited).
141      *
142      * @param project the project
143      */
144     @Nonnull
145     List<RemoteRepository> getRemotePluginRepositories(@Nonnull Project project);
146 
147     /**
148      * Returns an immutable map of the project properties.
149      *
150      * @see #setProperty(Project, String, String)
151      */
152     @Nonnull
153     Map<String, String> getProperties(@Nonnull Project project);
154 
155     /**
156      * Set a given project property.
157      *
158      * @param project the project to modify
159      * @param key they property's key
160      * @param value the value or {@code null} to unset the property
161      */
162     void setProperty(@Nonnull Project project, @Nonnull String key, @Nullable String value);
163 
164     @Nonnull
165     Optional<Project> getExecutionProject(@Nonnull Project project);
166 }