Skip to content

Commit 9648557

Browse files
committed
Make the getSourceRoots() and getEnabledSourceRoots(ProjectScope, Language) methods accessible from the Project interface.
1 parent 7000a3c commit 9648557

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/Project.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
package org.apache.maven.api;
2020

2121
import java.nio.file.Path;
22+
import java.util.Collection;
2223
import java.util.List;
2324
import java.util.Optional;
25+
import java.util.stream.Stream;
2426

2527
import org.apache.maven.api.annotations.Experimental;
2628
import org.apache.maven.api.annotations.Nonnull;
@@ -49,25 +51,25 @@
4951
public interface Project {
5052

5153
/**
52-
* Returns the project groupId.
54+
* {@return the project groupId}.
5355
*/
5456
@Nonnull
5557
String getGroupId();
5658

5759
/**
58-
* Returns the project artifactId.
60+
* {@return the project artifactId}.
5961
*/
6062
@Nonnull
6163
String getArtifactId();
6264

6365
/**
64-
* Returns the project version.
66+
* {@return the project version}.
6567
*/
6668
@Nonnull
6769
String getVersion();
6870

6971
/**
70-
* Returns the project packaging.
72+
* {@return the project packaging}.
7173
* <p>
7274
* Note: unlike in legacy code, logical checks against string representing packaging (returned by this method)
7375
* are NOT recommended (code like {@code "pom".equals(project.getPackaging)} must be avoided). Use method
@@ -79,7 +81,7 @@ public interface Project {
7981
Packaging getPackaging();
8082

8183
/**
82-
* Returns the project language. It is by default determined by {@link #getPackaging()}.
84+
* {@return the project language}. It is by default determined by {@link #getPackaging()}.
8385
*
8486
* @see #getPackaging()
8587
*/
@@ -89,7 +91,7 @@ default Language getLanguage() {
8991
}
9092

9193
/**
92-
* Returns the project POM artifact, which is the artifact of the POM of this project. Every project have a POM
94+
* {@return the project POM artifact}, which is the artifact of the POM of this project. Every project have a POM
9395
* artifact, even if the existence of backing POM file is NOT a requirement (i.e. for some transient projects).
9496
*
9597
* @see org.apache.maven.api.services.ArtifactManager#getPath(Artifact)
@@ -100,7 +102,7 @@ default ProducedArtifact getPomArtifact() {
100102
}
101103

102104
/**
103-
* Returns the project main artifact, which is the artifact produced by this project build, if applicable.
105+
* {@return the project main artifact}, which is the artifact produced by this project build, if applicable.
104106
* This artifact MAY be absent if the project is actually not producing any main artifact (i.e. "pom" packaging).
105107
*
106108
* @see #getPackaging()
@@ -113,7 +115,7 @@ default Optional<ProducedArtifact> getMainArtifact() {
113115
}
114116

115117
/**
116-
* Returns the project artifacts as immutable list. Elements are the project POM artifact and the artifact
118+
* {@return the project artifacts as immutable list}. Elements are the project POM artifact and the artifact
117119
* produced by this project build, if applicable. Hence, the returned list may have one or two elements
118120
* (never less than 1, never more than 2), depending on project packaging.
119121
* <p>
@@ -129,13 +131,15 @@ default Optional<ProducedArtifact> getMainArtifact() {
129131
List<ProducedArtifact> getArtifacts();
130132

131133
/**
132-
* Returns the project model.
134+
* {@return the project model}.
133135
*/
134136
@Nonnull
135137
Model getModel();
136138

137139
/**
138140
* Shorthand method.
141+
*
142+
* @return the build element of the project model
139143
*/
140144
@Nonnull
141145
default Build getBuild() {
@@ -170,19 +174,19 @@ default Build getBuild() {
170174
Path getBasedir();
171175

172176
/**
173-
* Returns the project direct dependencies (directly specified or inherited).
177+
* {@return the project direct dependencies (directly specified or inherited)}.
174178
*/
175179
@Nonnull
176180
List<DependencyCoordinates> getDependencies();
177181

178182
/**
179-
* Returns the project managed dependencies (directly specified or inherited).
183+
* {@return the project managed dependencies (directly specified or inherited)}.
180184
*/
181185
@Nonnull
182186
List<DependencyCoordinates> getManagedDependencies();
183187

184188
/**
185-
* Returns the project ID, usable as key.
189+
* {@return the project ID, usable as key}.
186190
*/
187191
@Nonnull
188192
default String getId() {
@@ -216,6 +220,7 @@ default String getId() {
216220
* Gets the root directory of the project, which is the parent directory
217221
* containing the {@code .mvn} directory or flagged with {@code root="true"}.
218222
*
223+
* @return the root directory of the project
219224
* @throws IllegalStateException if the root directory could not be found
220225
* @see Session#getRootDirectory()
221226
*/
@@ -234,4 +239,32 @@ default String getId() {
234239
*/
235240
@Nonnull
236241
Optional<Project> getParent();
242+
243+
/**
244+
* {@return all source root directories}, including the disabled ones, for all languages and scopes.
245+
* For listing only the {@linkplain SourceRoot#enabled() enabled} source roots,
246+
* the following code can be used:
247+
*
248+
* <pre>{@literal
249+
* List<SourceRoot> enabledRoots = project.getSourceRoots()
250+
* .stream().filter(SourceRoot::enabled).toList();
251+
* }</pre>
252+
*
253+
* The iteration order is the order in which the sources are declared in the POM file.
254+
*/
255+
@Nonnull
256+
Collection<SourceRoot> getSourceRoots();
257+
258+
/**
259+
* {@return all enabled sources that provide files in the given language for the given scope}.
260+
* If the given scope is {@code null}, then this method returns the enabled sources for all scopes.
261+
* If the given language is {@code null}, then this method returns the enabled sources for all languages.
262+
* An arbitrary number of source roots may exist for the same scope and language.
263+
* It may be, for example, the case of a multi-versions project.
264+
* The iteration order is the order in which the sources are declared in the POM file.
265+
*
266+
* @param scope the scope of the sources to return, or {@code null} for all scopes
267+
* @param language the language of the sources to return, or {@code null} for all languages
268+
*/
269+
Stream<SourceRoot> getEnabledSourceRoots(ProjectScope scope, Language language);
237270
}

impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProject.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import java.util.Collections;
2525
import java.util.List;
2626
import java.util.Optional;
27+
import java.util.stream.Stream;
2728

2829
import org.apache.maven.RepositoryUtils;
2930
import org.apache.maven.api.DependencyCoordinates;
3031
import org.apache.maven.api.DependencyScope;
3132
import org.apache.maven.api.Exclusion;
33+
import org.apache.maven.api.Language;
3234
import org.apache.maven.api.Packaging;
3335
import org.apache.maven.api.ProducedArtifact;
3436
import org.apache.maven.api.Project;
37+
import org.apache.maven.api.ProjectScope;
38+
import org.apache.maven.api.SourceRoot;
3539
import org.apache.maven.api.Type;
3640
import org.apache.maven.api.VersionConstraint;
3741
import org.apache.maven.api.annotations.Nonnull;
@@ -122,6 +126,7 @@ public Path getPomPath() {
122126
return nonNull(project.getFile(), "pomPath").toPath();
123127
}
124128

129+
@Nonnull
125130
@Override
126131
public Path getBasedir() {
127132
return nonNull(project.getBasedir(), "basedir").toPath();
@@ -164,6 +169,18 @@ public Optional<Project> getParent() {
164169
return Optional.ofNullable(session.getProject(parent));
165170
}
166171

172+
@Nonnull
173+
@Override
174+
public Collection<SourceRoot> getSourceRoots() {
175+
return project.getSourceRoots();
176+
}
177+
178+
@Nonnull
179+
@Override
180+
public Stream<SourceRoot> getEnabledSourceRoots(ProjectScope scope, Language language) {
181+
return project.getEnabledSourceRoots(scope, language);
182+
}
183+
167184
@Nonnull
168185
private DependencyCoordinates toDependency(org.apache.maven.api.model.Dependency dependency) {
169186
return new DependencyCoordinates() {

impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ public void addTestCompileSourceRoot(String path) {
426426

427427
/**
428428
* {@return all source root directories, including the disabled ones, for all languages and scopes}.
429+
* The iteration order is the order in which the sources are declared in the POM file.
429430
* The returned collection is unmodifiable.
430431
*
431432
* @see #addSourceRoot(SourceRoot)
@@ -438,6 +439,7 @@ public Collection<SourceRoot> getSourceRoots() {
438439
* {@return all enabled sources that provide files in the given language for the given scope}.
439440
* If the given scope is {@code null}, then this method returns the enabled sources for all scopes.
440441
* If the given language is {@code null}, then this method returns the enabled sources for all languages.
442+
* The iteration order is the order in which the sources are declared in the POM file.
441443
*
442444
* @param scope the scope of the sources to return, or {@code null} for all scopes
443445
* @param language the language of the sources to return, or {@code null} for all languages

0 commit comments

Comments
 (0)