Class Maven

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class Maven extends AnahataToolkit
A toolkit for inspecting and building Maven projects through the IntelliJ IDEA Maven integration.

This is the IntelliJ port of the NetBeans Maven toolkit. It uses MavenProjectsManager to enumerate imported Maven projects and their resolved dependencies, and MavenRunner to execute goals against a project's live configuration. Goal execution is asynchronous in the platform; this toolkit awaits completion on a latch so the model receives a definitive result. Build output streams to the IDE's Maven Run console (the platform does not expose it as a return value here).

Author:
anahata
  • Constructor Details

    • Maven

      public Maven()
      Constructs the Maven toolkit (instantiated reflectively via its public no-arg constructor).
  • Method Details

    • getSystemInstructions

      public List<String> getSystemInstructions() throws Exception
      Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.

      Notes that projects must be imported as Maven projects and that goal output appears in the IDE Maven console.

      Returns:
      A list of system instruction strings.
      Throws:
      Exception - if an error occurs during instruction generation.
    • getMavenProjects

      public String getMavenProjects()
      Lists every imported Maven project across all open IntelliJ projects.
      Returns:
      a Markdown listing of Maven coordinates, packaging and directory.
    • getDependencies

      public String getDependencies(String projectPath) throws AgiToolException
      Lists the resolved dependencies of the Maven project at the given path, grouped by scope.
      Parameters:
      projectPath - the absolute path of the project directory or its pom.xml.
      Returns:
      a Markdown listing of resolved dependencies.
      Throws:
      AgiToolException - if the path is not a recognized (imported) Maven project.
    • runGoals

      public String runGoals(String projectPath, List<String> goals, List<String> profiles) throws AgiToolException
      Executes Maven goals against the project at the given path and awaits completion.

      The goals run through the platform MavenRunner; this call blocks (off the EDT) until the build finishes or a generous timeout elapses. Console output is shown in the IDE's Maven Run tool window.

      Parameters:
      projectPath - the absolute path of the project directory or its pom.xml.
      goals - the Maven goals to run (e.g. clean, install).
      profiles - the profiles to activate, or null/empty for none.
      Returns:
      a completion summary.
      Throws:
      AgiToolException - if the project cannot be resolved or the build is interrupted.
    • searchMavenIndex

      public String searchMavenIndex(String query, Integer maxResults) throws AgiToolException
      Searches the configured Maven repository index for artifacts matching a query.

      Uses MavenArtifactSearcher against the index of the first open project. Results depend on the repository index having been built/downloaded by the IDE; an empty result usually means the index is not yet available.

      Parameters:
      query - the search query (matched against groupId/artifactId).
      maxResults - the maximum number of results, or null for 50.
      Returns:
      a Markdown listing of matching groupId:artifactId:version coordinates.
      Throws:
      AgiToolException - if no project is open.
    • addDependency

      public String addDependency(String projectPath, String groupId, String artifactId, String version, String scope) throws AgiToolException
      Adds a dependency to a project's pom.xml and triggers a Maven reimport.

      The dependency element is spliced into the existing <dependencies> block (or a new block is created before </project>) via a single undoable document edit, the file is saved, and MavenProjectsManager.forceUpdateAllProjectsOrFindAllAvailablePomFiles() refreshes the project model so the new artifact is resolved onto the classpath.

      Parameters:
      projectPath - the absolute path of the project directory or its pom.xml.
      groupId - the dependency groupId.
      artifactId - the dependency artifactId.
      version - the dependency version.
      scope - the Maven scope (e.g. compile, test), or null for default.
      Returns:
      a confirmation message.
      Throws:
      AgiToolException - if the pom cannot be resolved or edited.
    • resolveMavenProject

      private org.jetbrains.idea.maven.project.MavenProject resolveMavenProject(String projectPath) throws AgiToolException
      Resolves the MavenProject for a directory or pom path.
      Parameters:
      projectPath - the absolute project directory or pom path.
      Returns:
      the resolved Maven project.
      Throws:
      AgiToolException - if it is not a recognized imported Maven project.
    • resolveMavenContext

      private Object[] resolveMavenContext(String projectPath) throws AgiToolException
      Resolves [IntelliJ Project, MavenProject] for a directory or pom path.
      Parameters:
      projectPath - the absolute project directory or pom path.
      Returns:
      a two-element array of the host IntelliJ project and the Maven project.
      Throws:
      AgiToolException - if the pom cannot be found or is not an imported Maven project.