Package org.apache.maven.plugin.compiler
Class ToolExecutor
java.lang.Object
org.apache.maven.plugin.compiler.ToolExecutor
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 theAbstractCompilerMojo
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 Summary
FieldsModifier and TypeFieldDescriptionAll dependencies grouped by the path types where to place them, together with the modules to patch.protected final Charset
The character encoding of source files, ornull
for the platform default encoding.The directories where to write generated source files.protected final boolean
Whether the project contains or is assumed to contain amodule-info.java
file.protected final DiagnosticListener
<? super JavaFileObject> Where to send the compilation warning (nevernull
).protected final org.apache.maven.api.plugin.Log
The Maven logger for reporting information or warnings to the user.protected final Path
The destination directory (or class output directory) for class files. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ToolExecutor
(AbstractCompilerMojo mojo, DiagnosticListener<? super JavaFileObject> listener) Creates a new task by taking a snapshot of the current configuration of the given MOJO. -
Method Summary
Modifier and TypeMethodDescriptionboolean
applyIncrementalBuild
(AbstractCompilerMojo mojo, Options configuration) Filters the source files to recompile, or cleans the output directory if everything should be rebuilt.boolean
compile
(JavaCompiler compiler, Options configuration, Writer otherOutput) Runs the compilation task.dependencies
(org.apache.maven.api.PathType pathType) Returns a modifiable list of paths to all dependencies of the given type.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.
-
Field Details
-
encoding
The character encoding of source files, ornull
for the platform default encoding.- See Also:
-
generatedSourceDirectories
The directories where to write generated source files. This set is either empty or a singleton.- See Also:
-
hasModuleDeclaration
protected final boolean hasModuleDeclarationWhether the project contains or is assumed to contain amodule-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
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 thatToolExecutor
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
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
Where to send the compilation warning (nevernull
). 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 loggerThe 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 thisToolExecutor
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 snapshotlistener
- where to send compilation warnings, ornull
for the Maven logger- Throws:
org.apache.maven.api.plugin.MojoException
- if this constructor identifies an invalid parameter in the MOJOIOException
- if an error occurred while creating the output directory or scanning the source directoriesorg.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 returnsfalse
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 configurationconfiguration
- 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
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
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, thetype
argument usually identifies a--class-path
,--module-path
or--patch-module
option.- Parameters:
pathType
- type of path for which to add an elementfirst
- 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 compilerconfiguration
- the options to give to the Java compilerotherOutput
- where to write additional output from the compiler- Returns:
- whether the compilation succeeded
- Throws:
IOException
- if an error occurred while reading or writing a fileorg.apache.maven.api.plugin.MojoException
- if the compilation failed for a reason identified by this methodRuntimeException
- if any other kind of error occurred
-