Package org.apache.maven.api.services
Interface ProjectManager
- All Superinterfaces:
Service
- All Known Implementing Classes:
DefaultProjectManager
Interface to manage the project state and artifacts during the Maven build lifecycle.
This service provides operations to:
- Manage project artifacts (main and attached)
- Handle source roots and resources
- Access and modify project properties
- Manage repository configurations
- Handle project forking states
- Since:
- 4.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addSourceRoot
(Project project, ProjectScope scope, Language language, Path directory) Resolves and adds the given directory as a source with the given scope and language.void
addSourceRoot
(Project project, SourceRoot source) Adds the given source to the given project.void
attachArtifact
(Project project, ProducedArtifact artifact, Path path) Attaches a produced artifact to the project at the specified path.default void
attachArtifact
(Session session, Project project, String type, Path path) Attaches an artifact to the project with an explicitly specified type.default void
attachArtifact
(Session session, Project project, Path path) Attaches an artifact to the project using the given file path.getAllArtifacts
(Project project) Returns project's all artifacts as an immutable ordered collection.getAttachedArtifacts
(Project project) Returns an immutable collection of attached artifacts for the given project.getEnabledSourceRoots
(Project project, ProjectScope scope, Language language) Returns all enabled sources that provide files in the given language for the given scope.getExecutionProject
(Project project) Returns the original project being built when the input project is a forked project.Returns the path to the built project artifact file, if the project has been built.getProperties
(Project project) Returns an immutable map of the project properties.getRemotePluginRepositories
(Project project) Returns an immutable list of project plugin remote repositories (directly specified or inherited).getRemoteProjectRepositories
(Project project) Returns an immutable list of project remote repositories (directly specified or inherited).getSourceRoots
(Project project) Returns all source root directories.void
setProperty
(Project project, String key, String value) Set a given project property.
-
Method Details
-
getPath
Returns the path to the built project artifact file, if the project has been built. This path is only available after the artifact has been produced during the build lifecycle.- Parameters:
project
- the project to get the artifact path for- Returns:
- an Optional containing the path to the built artifact if available, or empty if the artifact hasn't been built yet
-
getAttachedArtifacts
Returns an immutable collection of attached artifacts for the given project. Attached artifacts are secondary artifacts produced during the build (e.g., sources jar, javadoc jar, test jars). These artifacts are created and attached during specific lifecycle phases, so the collection contents depend on the build phase when this method is called.- Parameters:
project
- the project to get attached artifacts for- Returns:
- an immutable collection of attached artifacts, may be empty if no artifacts have been attached yet
- Throws:
IllegalArgumentException
- if the project is null- See Also:
-
getAllArtifacts
Returns project's all artifacts as an immutable ordered collection. The collection contains:- The project's artifacts (
Project.getArtifacts()
):- The POM artifact (always present)
- The main project artifact (if applicable based on packaging)
- All attached artifacts in the order they were attached
- Parameters:
project
- the project to get artifacts for- Returns:
- an immutable ordered collection of all project artifacts
- See Also:
- The project's artifacts (
-
attachArtifact
Attaches an artifact to the project using the given file path. The artifact type will be determined from the file extension. This method is thread-safe and ensures proper synchronization of the project's artifact state.- Parameters:
session
- the current build sessionproject
- the project to attach the artifact topath
- the path to the artifact file
-
attachArtifact
default void attachArtifact(@Nonnull Session session, @Nonnull Project project, @Nonnull String type, @Nonnull Path path) Attaches an artifact to the project with an explicitly specified type.- Parameters:
session
- the current build sessionproject
- the project to attach the artifact totype
- the type of the artifact (e.g., "jar", "war", "sources")path
- the path to the artifact file- See Also:
-
attachArtifact
void attachArtifact(@Nonnull Project project, @Nonnull ProducedArtifact artifact, @Nonnull Path path) Attaches a produced artifact to the project at the specified path. This is the base method that the other attachArtifact methods delegate to.- Parameters:
project
- the project to attach the artifact toartifact
- the produced artifact to attachpath
- the path to the artifact file
-
getSourceRoots
Returns all source root directories., including the disabled ones, for all languages and scopes. For listing only the enabled source roots, the following code can be used:List<SourceRoot> enabledRoots = project.getSourceRoots() .stream().filter(SourceRoot::enabled).toList();
The iteration order is the order in which the sources are declared in the POM file.- Parameters:
project
- the project for which to get the source roots- Returns:
- all source root directories
-
getEnabledSourceRoots
@Nonnull Stream<SourceRoot> getEnabledSourceRoots(@Nonnull Project project, @Nullable ProjectScope scope, @Nullable Language language) Returns all enabled sources that provide files in the given language for the given scope.. If the given scope isnull
, then this method returns the enabled sources for all scopes. If the given language isnull
, then this method returns the enabled sources for all languages. An arbitrary number of source roots may exist for the same scope and language. It may be, for example, the case of a multi-versions project. The iteration order is the order in which the sources are declared in the POM file.- Parameters:
project
- the project for which to get the enabled source rootsscope
- the scope of the sources to return, ornull
for all scopeslanguage
- the language of the sources to return, ornull
for all languages- Returns:
- all enabled sources that provide files in the given language for the given scope
-
addSourceRoot
Adds the given source to the given project. If a source already exists for the given scope, language and directory, then the behavior depends on theProjectManager
implementation. It may do nothing or thrown IllegalArgumentException.- Parameters:
project
- the project to updatesource
- the source to add- Throws:
IllegalArgumentException
- if this project manager rejects the given source because of conflict- See Also:
-
addSourceRoot
void addSourceRoot(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Language language, @Nonnull Path directory) Resolves and adds the given directory as a source with the given scope and language. First, this method resolves the given root against the project base directory, then normalizes the path. If no source already exists for the same scope, language and normalized directory, these arguments are added as a newSourceRoot
element. Otherwise (i.e., in case of potential conflict), the behavior depends on theProjectManager
. The default implementation does nothing in the latter case.- Parameters:
project
- the project to updatescope
- scope (main or test) of the directory to addlanguage
- language of the files contained in the directory to adddirectory
- the directory to add if not already present in the source- See Also:
-
getRemoteProjectRepositories
Returns an immutable list of project remote repositories (directly specified or inherited). The repositories are ordered by declaration order, with inherited repositories appearing after directly specified ones.- Parameters:
project
- the project- Returns:
- ordered list of remote repositories
-
getRemotePluginRepositories
Returns an immutable list of project plugin remote repositories (directly specified or inherited). The repositories are ordered by declaration order, with inherited repositories appearing after directly specified ones.- Parameters:
project
- the project- Returns:
- ordered list of remote repositories
-
getProperties
Returns an immutable map of the project properties..- Parameters:
project
- the project for which to get the properties- Returns:
- an immutable map of the project properties
- See Also:
-
setProperty
Set a given project property. Properties set through this method are only valid for the current build session and do not modify the underlying project model.- Parameters:
project
- the project to modifykey
- they property's keyvalue
- the value ornull
to unset the property
-
getExecutionProject
Returns the original project being built when the input project is a forked project. During certain lifecycle phases, particularly for aggregator mojos, Maven may create a forked project (a copy of the original project) to execute a subset of the lifecycle. This method allows retrieving the original project that initiated the build.- Parameters:
project
- the potentially forked project- Returns:
- an Optional containing the original project if the input is a forked project, or an empty Optional if the input is already the original project
- Throws:
IllegalArgumentException
- if the project is null
-