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