Interface ProjectManager

All Superinterfaces:
Service
All Known Implementing Classes:
DefaultProjectManager

@Experimental public interface ProjectManager extends Service
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
The service maintains the mutable state of projects as they progress through their build lifecycle, ensuring thread-safety and proper state management. All implementations must be thread-safe as they may be accessed concurrently during parallel builds.
Since:
4.0.0
See Also:
  • Method Details

    • getPath

      @Nonnull Optional<Path> getPath(@Nonnull Project project)
      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

      @Nonnull Collection<ProducedArtifact> getAttachedArtifacts(@Nonnull Project project)
      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
      The contents depend on the current lifecycle phase when this method is called, as artifacts are typically attached during specific phases (e.g., sources jar during package phase).
      Parameters:
      project - the project to get artifacts for
      Returns:
      an immutable ordered collection of all project artifacts
      See Also:
    • attachArtifact

      default void attachArtifact(@Nonnull Session session, @Nonnull Project project, @Nonnull Path path)
      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 session
      project - the project to attach the artifact to
      path - 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 session
      project - the project to attach the artifact to
      type - 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 to
      artifact - the produced artifact to attach
      path - the path to the artifact file
    • getSourceRoots

      @Nonnull Collection<SourceRoot> getSourceRoots(@Nonnull Project project)
      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 is null, then this method returns the enabled sources for all scopes. If the given language is null, 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 roots
      scope - the scope of the sources to return, or null for all scopes
      language - the language of the sources to return, or null for all languages
      Returns:
      all enabled sources that provide files in the given language for the given scope
    • addSourceRoot

      void addSourceRoot(@Nonnull Project project, @Nonnull SourceRoot source)
      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 the ProjectManager implementation. It may do nothing or thrown IllegalArgumentException.
      Parameters:
      project - the project to update
      source - 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 new SourceRoot element. Otherwise (i.e., in case of potential conflict), the behavior depends on the ProjectManager. The default implementation does nothing in the latter case.
      Parameters:
      project - the project to update
      scope - scope (main or test) of the directory to add
      language - language of the files contained in the directory to add
      directory - the directory to add if not already present in the source
      See Also:
    • getRemoteProjectRepositories

      @Nonnull List<RemoteRepository> getRemoteProjectRepositories(@Nonnull Project project)
      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

      @Nonnull List<RemoteRepository> getRemotePluginRepositories(@Nonnull Project project)
      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

      @Nonnull Map<String,String> getProperties(@Nonnull Project project)
      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

      void setProperty(@Nonnull Project project, @Nonnull String key, @Nullable String value)
      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 modify
      key - they property's key
      value - the value or null to unset the property
    • getExecutionProject

      @Nonnull Optional<Project> getExecutionProject(@Nonnull Project project)
      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