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 /**
75 * Attaches an artifact to the project using the given file path. The artifact type will be
76 * determined from the file extension.
77 *
78 * @param session the current build session
79 * @param project the project to attach the artifact to
80 * @param path the path to the artifact file
81 * @throws IllegalArgumentException if the session, project or path is null
82 */
83 default void attachArtifact(@Nonnull Session session, @Nonnull Project project, @Nonnull Path path) {
84 String name = path.getFileName().toString();
85 int dot = name.lastIndexOf('.');
86 String ext = dot >= 1 ? name.substring(dot + 1) : "";
87 ProducedArtifact artifact = session.createProducedArtifact(
88 project.getGroupId(), project.getArtifactId(), project.getVersion(), ext);
89 attachArtifact(project, artifact, path);
90 }
91
92 /**
93 * Attaches an artifact to the project with an explicitly specified type.
94 *
95 * @param session the current build session
96 * @param project the project to attach the artifact to
97 * @param type the type of the artifact (e.g., "jar", "war", "sources")
98 * @param path the path to the artifact file
99 * @throws IllegalArgumentException if the session, project, type or path is null
100 * @see org.apache.maven.api.Type
101 */
102 default void attachArtifact(
103 @Nonnull Session session, @Nonnull Project project, @Nonnull String type, @Nonnull Path path) {
104 ProducedArtifact artifact = session.createProducedArtifact(
105 project.getGroupId(), project.getArtifactId(), project.getVersion(), null, null, type);
106 attachArtifact(project, artifact, path);
107 }
108
109 /**
110 * Attaches a produced artifact to the project at the specified path. This is the base method
111 * that the other attachArtifact methods delegate to.
112 *
113 * @param project the project to attach the artifact to
114 * @param artifact the produced artifact to attach
115 * @param path the path to the artifact file
116 * @throws IllegalArgumentException if the project, artifact or path is null
117 */
118 void attachArtifact(@Nonnull Project project, @Nonnull ProducedArtifact artifact, @Nonnull Path path);
119
120 /**
121 * Obtain an immutable list of compile source roots for the given project and scope.
122 * Paths are absolute.
123 *
124 * @param project the project
125 * @param scope the scope, i.e. usually main or test
126 * @return the list of compile source roots
127 */
128 @Nonnull
129 List<Path> getCompileSourceRoots(@Nonnull Project project, @Nonnull ProjectScope scope);
130
131 /**
132 * Add a compilation source root to the given project for the given scope.
133 * The path will be transformed into an absolute path and added to the list for the given scope,
134 * if not already present.
135 *
136 * @param project the project
137 * @param scope the scope, i.e. usually main or test
138 * @param sourceRoot the new source root
139 */
140 void addCompileSourceRoot(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Path sourceRoot);
141
142 /**
143 * Get the list of resources for the given project and scope
144 *
145 * @param project the project
146 * @param scope the scope, i.e. usually main or test
147 * @return the list of resources
148 */
149 List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope);
150
151 /**
152 * Add a resource set to the given project for the given scope.
153 *
154 * @param project the project
155 * @param scope the scope, i.e. usually main or test
156 * @param resource the resource set to add
157 */
158 void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource);
159
160 /**
161 * Returns an immutable list of project remote repositories (directly specified or inherited).
162 *
163 * @param project the project
164 */
165 @Nonnull
166 List<RemoteRepository> getRemoteProjectRepositories(@Nonnull Project project);
167
168 /**
169 * Returns an immutable list of project remote plugin repositories (directly specified or inherited).
170 *
171 * @param project the project
172 */
173 @Nonnull
174 List<RemoteRepository> getRemotePluginRepositories(@Nonnull Project project);
175
176 /**
177 * Returns an immutable map of the project properties.
178 *
179 * @see #setProperty(Project, String, String)
180 */
181 @Nonnull
182 Map<String, String> getProperties(@Nonnull Project project);
183
184 /**
185 * Set a given project property.
186 *
187 * @param project the project to modify
188 * @param key they property's key
189 * @param value the value or {@code null} to unset the property
190 */
191 void setProperty(@Nonnull Project project, @Nonnull String key, @Nullable String value);
192
193 @Nonnull
194 Optional<Project> getExecutionProject(@Nonnull Project project);
195 }