Class ToolExecutor

java.lang.Object
org.apache.maven.plugin.compiler.ToolExecutor

public class ToolExecutor extends Object
A task which configures and executes a Java tool such as the Java compiler. This class takes a snapshot of the information provided in the MOJO. Then, it collects additional information such as the source files and the dependencies. The set of source files to compile can optionally be filtered for keeping only the files that changed since the last build with the incremental build.

Thread safety

This class is not thread-safe. However, it is independent of the AbstractCompilerMojo instance given in argument to the constructor and to the incremental build. After all methods with an AbstractCompilerMojo argument have been invoked, ToolExecutor can safety be used in a background thread for launching the compilation (but must still be used by only only thread at a time).
Author:
Martin Desruisseaux
  • Field Details

    • encoding

      protected final Charset encoding
      The character encoding of source files, or null for the platform default encoding.
      See Also:
    • generatedSourceDirectories

      protected final Set<Path> generatedSourceDirectories
      The directories where to write generated source files. This set is either empty or a singleton.
      See Also:
    • hasModuleDeclaration

      protected final boolean hasModuleDeclaration
      Whether the project contains or is assumed to contain a module-info.java file. If the user specified explicitly whether the project is a modular or a classpath JAR, then this flag is set to the user's specification without verification. Otherwise, this flag is determined by scanning the list of source files.
    • dependencies

      protected final Map<org.apache.maven.api.PathType,List<Path>> dependencies
      All dependencies grouped by the path types where to place them, together with the modules to patch. The path type can be the class-path, module-path, annotation processor path, patched path, etc. Some path types include a module name.

      Modifications during the build of multi-release project

      When building a multi-release project, values associated to --class-path, --module-path or --patch-module options are modified every time that ToolExecutor compiles for a new Java release. The output directories for the previous Java releases are inserted as the first elements of their lists, or new entries are created if no list existed previously for an option.
      See Also:
    • outputDirectory

      protected final Path outputDirectory
      The destination directory (or class output directory) for class files. This directory will be given to the -d Java compiler option when compiling the classes for the base Java release.
      See Also:
    • listener

      protected final DiagnosticListener<? super JavaFileObject> listener
      Where to send the compilation warning (never null). If a null value was specified to the constructor, then this listener sends the warnings to the Maven logger.
    • logger

      protected final org.apache.maven.api.plugin.Log logger
      The Maven logger for reporting information or warnings to the user. Used for messages emitted directly by the Maven compiler plugin. Not necessarily used for messages emitted by the Java compiler.

      Thread safety

      This logger should be thread-safe if this ToolExecutor is executed in a background thread.
      See Also:
  • Constructor Details

    • ToolExecutor

      protected ToolExecutor(AbstractCompilerMojo mojo, DiagnosticListener<? super JavaFileObject> listener) throws IOException
      Creates a new task by taking a snapshot of the current configuration of the given MOJO. This constructor creates the output directory if it does not already exist.
      Parameters:
      mojo - the MOJO from which to take a snapshot
      listener - where to send compilation warnings, or null for the Maven logger
      Throws:
      org.apache.maven.api.plugin.MojoException - if this constructor identifies an invalid parameter in the MOJO
      IOException - if an error occurred while creating the output directory or scanning the source directories
      org.apache.maven.api.services.MavenException - if an error occurred while fetching dependencies
      See Also:
  • Method Details

    • applyIncrementalBuild

      public boolean applyIncrementalBuild(AbstractCompilerMojo mojo, Options configuration) throws IOException
      Filters the source files to recompile, or cleans the output directory if everything should be rebuilt. If the directory structure of the source files has changed since the last build, or if a compiler option changed, or if a dependency changed, then this method keeps all source files and cleans the output directory. Otherwise, the source files that did not changed since the last build are removed from the list of sources to compile. If all source files have been removed, then this method returns false for notifying the caller that it can skip the build.

      If this method is invoked many times, all invocations after this first one have no effect.

      Parameters:
      mojo - the MOJO from which to take the incremental build configuration
      configuration - the options which should match the options used during the last build
      Returns:
      whether there is at least one file to recompile
      Throws:
      IOException - if an error occurred while accessing the cache file or walking through the directory tree
    • dependencies

      protected List<Path> dependencies(org.apache.maven.api.PathType pathType)
      Returns a modifiable list of paths to all dependencies of the given type. The returned list is intentionally live: elements can be added or removed from the list for changing the state of this executor.
      Parameters:
      pathType - type of path for which to get the dependencies
      Returns:
      a modifiable list of paths to all dependencies of the given type
    • prependDependency

      protected List<Path> prependDependency(org.apache.maven.api.PathType pathType, Path first)
      Inserts the given path as the first element of the list of paths of the given type. The main purpose of this method is during the build of a multi-release project, for adding the output directory of the code targeting the previous Java release before to compile the code targeting the next Java release. In this context, the type argument usually identifies a --class-path, --module-path or --patch-module option.
      Parameters:
      pathType - type of path for which to add an element
      first - the path to put first
      Returns:
      the new paths for the given type, as a modifiable list
    • compile

      public boolean compile(JavaCompiler compiler, Options configuration, Writer otherOutput) throws IOException
      Runs the compilation task.
      Parameters:
      compiler - the compiler
      configuration - the options to give to the Java compiler
      otherOutput - where to write additional output from the compiler
      Returns:
      whether the compilation succeeded
      Throws:
      IOException - if an error occurred while reading or writing a file
      org.apache.maven.api.plugin.MojoException - if the compilation failed for a reason identified by this method
      RuntimeException - if any other kind of error occurred