Class AbstractCompilerMojo

java.lang.Object
org.apache.maven.plugin.compiler.AbstractCompilerMojo
All Implemented Interfaces:
org.apache.maven.api.plugin.Mojo
Direct Known Subclasses:
CompilerMojo, TestCompilerMojo

public abstract class AbstractCompilerMojo extends Object implements org.apache.maven.api.plugin.Mojo
Base class of Mojos compiling Java source code. This plugin uses the JavaCompiler interface from JDK 6+. Each instance shall be used only once, then discarded.

Thread-safety

This class is not thread-safe. If this class is used in a multi-thread context, users are responsible for synchronizing all accesses to this MOJO instance. However, the executor returned by createExecutor(DiagnosticListener) can safely launch the compilation in a background thread.
Since:
2.0
Author:
Trygve Laugstøl, Martin Desruisseaux
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Deprecated.
    Replaced by ordinary dependencies with <type> element set to processor, classpath-processor or modular-processor.
    protected boolean
    Deprecated.
    This flag is ignored.
    protected String[]
    Class names of annotation processors to run.
    protected org.apache.maven.api.services.ArtifactManager
     
    protected Path
    The directory to run the compiler from if fork is true.
    protected List<String>
    Additional arguments to be passed verbatim to the Java compiler.
    protected String
    Deprecated.
    Use compilerArgs instead.
    protected String
    Identifier of the compiler to use.
    protected String
    Deprecated, for removal: This API element is subject to removal in a future version.
    Not supported anymore.
    protected String
    Deprecated, for removal: This API element is subject to removal in a future version.
    This parameter is no longer used by the underlying compilers.
    protected List<String>
    Deprecated.
    Replaced by the project-wide <sources> element.
    protected boolean
    Whether to generate package-info.class even when empty.
    protected boolean
    Deprecated.
    Setting this flag to false is replaced by <debuglevel>none</debuglevel>.
    protected String
    Kinds of debugging information to include in the compiled class files.
    protected boolean
    Whether to enable preview language features of the java compiler.
    protected String
    The -encoding argument for the Java compiler.
    protected String
    Executable of the compiler to use when fork is true.
    protected boolean
    Whether the build will stop if there are compilation errors.
    protected boolean
    Whether the build will stop if there are compilation warnings.
    protected List<String>
    File extensions to check timestamp for incremental build.
    protected Boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    Ignored because java.lang.Compiler has been deprecated and removed from the JDK.
    protected Boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    Ignored because the compiler plugin now always use the javax.tools API.
    protected boolean
    Allows running the compiler in a separate process.
    protected String
    Whether to generate class files for implicitly referenced files.
    protected String
    The algorithm to use for selecting which files to compile.
    protected Map<String,String>
    Requirements for this JDK toolchain for using a different javac than the one of the JDK used by Maven.
    protected org.apache.maven.api.plugin.Log
    The logger for reporting information or warnings to the user.
    protected String
    Maximum size, in megabytes, of the memory allocation pool if fork is set to true.
    protected String
    Initial size, in megabytes, of the memory allocation pool if fork is set to true.
    protected org.apache.maven.api.services.MessageBuilderFactory
     
    protected String
    The --module-version argument for the Java compiler.
    protected Path
    Path to a file where to cache information about the last incremental build.
    protected Boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    This property is ignored.
    protected String
    Deprecated, for removal: This API element is subject to removal in a future version.
    Bundling many class files into a single file should be done by other plugins.
    protected String
    Deprecated, for removal: This API element is subject to removal in a future version.
    Not used by the compiler plugin since it does not generate archive.
    protected boolean
    Whether to generate metadata for reflection on method parameters.
    protected String
    Configures if annotation processing and/or compilation are performed by the compiler.
    protected org.apache.maven.api.Project
    The current project instance.
    protected org.apache.maven.api.services.ProjectManager
     
    protected String
    The --release argument for the Java compiler when the sources do not declare this version.
    protected org.apache.maven.api.Session
    The current build session instance.
    protected boolean
    Whether to provide more details about why a module is rebuilt.
    protected boolean
    Whether to show source locations where deprecated APIs are used.
    protected boolean
    Whether to show compilation warnings.
    protected Boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    Deprecated as a consequence of compilerReuseStrategy deprecation.
    protected String
    The --source argument for the Java compiler.
    protected int
    The granularity in milliseconds of the last modification date for testing whether a source needs recompilation.
    protected String
    The --target argument for the Java compiler.
    protected org.apache.maven.api.services.ToolchainManager
     
    protected Boolean
    Deprecated.
    protected boolean
    Whether to show messages about what the compiler is doing.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractCompilerMojo(org.apache.maven.api.PathScope compileScope)
    Creates a new MOJO.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the compiler to use for compiling the code.
    Creates a new task by taking a snapshot of the current configuration of this MOJO.
    void
    Runs the Java compiler.
    protected abstract String
    Returns the file where to dump the command-line when debug logging is enabled or when the compilation failed.
    protected abstract Set<String>
    Returns the exclusion filters for the compiler, or an empty list if none.
    protected abstract Path
    Returns the path where to place generated source files created by annotation processing.
    protected abstract Set<String>
    Returns the inclusion filters for the compiler, or an empty list for all Java source files.
    protected abstract Set<String>
    Returns the exclusion filters for the incremental calculation.
    protected abstract Path
    Returns the destination directory (or class output directory) for class files.
    protected String
    Returns the --release argument for the Java compiler.
    protected String
    Returns the --source argument for the Java compiler.
    protected String
    Returns the --target argument for the Java compiler.
    Parses the parameters declared in the MOJO.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • moduleVersion

      @Parameter(property="maven.compiler.moduleVersion", defaultValue="${project.version}") protected String moduleVersion
      The --module-version argument for the Java compiler. This is ignored if not applicable, e.g., in non-modular projects.
      Since:
      4.0.0
      See Also:
    • encoding

      @Parameter(property="encoding", defaultValue="${project.build.sourceEncoding}") protected String encoding
      The -encoding argument for the Java compiler.
      Since:
      2.1
      See Also:
    • source

      @Parameter(property="maven.compiler.source") protected String source
      The --source argument for the Java compiler.

      Notes:

      • Since 3.8.0 the default value has changed from 1.5 to 1.6.
      • Since 3.9.0 the default value has changed from 1.6 to 1.7.
      • Since 3.11.0 the default value has changed from 1.7 to 1.8.
      • Since 4.0.0-beta-2 the default value has been removed. As of Java 9, the release parameter is preferred.
      See Also:
    • target

      @Parameter(property="maven.compiler.target") protected String target
      The --target argument for the Java compiler.

      Notes:

      • Since 3.8.0 the default value has changed from 1.5 to 1.6.
      • Since 3.9.0 the default value has changed from 1.6 to 1.7.
      • Since 3.11.0 the default value has changed from 1.7 to 1.8.
      • Since 4.0.0-beta-2 the default value has been removed. As of Java 9, the release parameter is preferred.
      See Also:
    • release

      @Parameter(property="maven.compiler.release") protected String release
      The --release argument for the Java compiler when the sources do not declare this version. The suggested way to declare the target Java release is to specify it with the sources like below:
      
       <build>
         <sources>
           <source>
             <directory>src/main/java</directory>
             <targetVersion>17</targetVersion>
           </source>
         </sources>
       </build>
      If such <targetVersion> element is found, it has precedence over this release property. If a source does not declare a target Java version, then the value of this release property is used as a fallback. If omitted, the compiler will generate bytecodes for the Java version running the compiler.
      Since:
      3.6
      See Also:
    • enablePreview

      @Parameter(property="maven.compiler.enablePreview", defaultValue="false") protected boolean enablePreview
      Whether to enable preview language features of the java compiler. If true, then the --enable-preview option will be added to compiler arguments.
      Since:
      3.10.1
      See Also:
    • compileSourceRoots

      @Parameter @Deprecated(since="4.0.0") protected List<String> compileSourceRoots
      Deprecated.
      Replaced by the project-wide <sources> element.
      The root directories containing the source files to be compiled. If null or empty, the directories will be obtained from the <Source> elements declared in the project. If non-empty, the project <Source> elements are ignored. This configuration option should be used only when there is a need to override the project configuration.
    • compilerArgs

      @Parameter protected List<String> compilerArgs
      Additional arguments to be passed verbatim to the Java compiler. This parameter can be used when the Maven compiler plugin does not provide a parameter for a Java compiler option. It may happen, for example, for new or preview Java features which are not yet handled by this compiler plugin.

      If an option has a value, the option and the value shall be specified in two separated <arg> elements. For example, the -Xmaxerrs 1000 option (for setting the maximal number of errors to 1000) can be specified as below (together with other options):

      
       <compilerArgs>
         <arg>-Xlint</arg>
         <arg>-Xmaxerrs</arg>
         <arg>1000</arg>
         <arg>J-Duser.language=en_us</arg>
       </compilerArgs>
      Note that -J options should be specified only if fork is set to true. Other options can be specified regardless the fork value. The compiler plugin does not verify whether the arguments given through this parameter are valid. For this reason, the other parameters provided by the compiler plugin should be preferred when they exist, because the plugin checks whether the corresponding options are supported.
      Since:
      3.1
      See Also:
    • compilerArgument

      @Parameter @Deprecated(since="4.0.0") protected String compilerArgument
      Deprecated.
      Use compilerArgs instead.
      The single argument string to be passed to the compiler. To pass multiple arguments such as -Xmaxerrs 1000 (which are actually two arguments), compilerArgs is preferred.

      Note that -J options should be specified only if fork is set to true.

      See Also:
    • proc

      @Parameter(property="maven.compiler.proc") protected String proc
      Configures if annotation processing and/or compilation are performed by the compiler. If set, the value will be appended to the -proc: compiler option. Possible values are:
      • none – no annotation processing is performed, only compilation is done.
      • only – only annotation processing is done, no compilation.
      • full – annotation processing followed by compilation is done.
      The default value depends on the JDK used for the build. Prior to Java 23, the default was full, so annotation processing and compilation were executed without explicit configuration. For security reasons, starting with Java 23 no annotation processing is done if neither any -processor, -processor path or -processor module are set, or either only or full is set. So literally the default is none. It is recommended to always list the annotation processors you want to execute instead of using the proc configuration, to ensure that only desired processors are executed and not any "hidden" (and maybe malicious).
      Since:
      2.2
      See Also:
    • annotationProcessors

      @Parameter protected String[] annotationProcessors
      Class names of annotation processors to run. If not set, the default annotation processors discovery process applies. If set, the value will be appended to the -processor compiler option.
      Since:
      2.2
      See Also:
    • annotationProcessorPaths

      @Parameter @Deprecated(since="4.0.0") protected List<DependencyCoordinate> annotationProcessorPaths
      Deprecated.
      Replaced by ordinary dependencies with <type> element set to processor, classpath-processor or modular-processor.
      Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation processors only in those classpath elements. If omitted (and proc is set to only or full), the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors. Since JDK 23 by default no annotation processing is performed as long as no processors is listed for security reasons. Therefore, you should always list the desired processors using this configuration element or annotationProcessorPaths.

      Each classpath element is specified using their Maven coordinates (groupId, artifactId, version, classifier, type). Transitive dependencies are added automatically. Exclusions are supported as well. Example:

       <configuration>
         <annotationProcessorPaths>
           <path>
             <groupId>org.sample</groupId>
             <artifactId>sample-annotation-processor</artifactId>
             <version>1.2.3</version> <!-- Optional - taken from dependency management if not specified -->
             <!-- Optionally exclude transitive dependencies -->
             <exclusions>
               <exclusion>
                 <groupId>org.sample</groupId>
                 <artifactId>sample-dependency</artifactId>
               </exclusion>
             </exclusions>
           </path>
           <!-- ... more ... -->
         </annotationProcessorPaths>
       </configuration>
       
      Note: Exclusions are supported from version 3.11.0.
      Since:
      3.5
      See Also:
    • annotationProcessorPathsUseDepMgmt

      @Deprecated(since="4.0.0") @Parameter(defaultValue="false") protected boolean annotationProcessorPathsUseDepMgmt
      Deprecated.
      This flag is ignored. Replaced by ordinary dependencies with <type> element set to processor, classpath-processor or modular-processor.
      Whether to use the Maven dependency management section when resolving transitive dependencies of annotation processor paths.

      This flag does not enable / disable the ability to resolve the version of annotation processor paths from dependency management section. It only influences the resolution of transitive dependencies of those top-level paths.

      Since:
      3.12.0
    • createMissingPackageInfoClass

      @Parameter(property="maven.compiler.createMissingPackageInfoClass", defaultValue="false") protected boolean createMissingPackageInfoClass
      Whether to generate package-info.class even when empty. By default, package info source files that only contain javadoc and no annotation on the package can lead to no class file being generated by the compiler. It may cause a file miss on build systems that check for file existence in order to decide what to recompile.

      If true, the -Xpkginfo:always compiler option is added if the compiler supports that extra option. If the extra option is not supported, then a warning is logged and no option is added to the compiler arguments.

      Since:
      3.10
      See Also:
    • implicit

      @Parameter(property="maven.compiler.implicit") protected String implicit
      Whether to generate class files for implicitly referenced files. If set, the value will be appended to the -implicit: compiler option. Standard values are:
      • class – automatically generates class files.
      • none – suppresses class file generation.
      Since:
      3.10.2
      See Also:
    • parameters

      @Parameter(property="maven.compiler.parameters", defaultValue="false") protected boolean parameters
      Whether to generate metadata for reflection on method parameters. If true, the -parameters option will be added to compiler arguments.
      Since:
      3.6.2
      See Also:
    • debug

      @Deprecated(since="4.0.0") @Parameter(property="maven.compiler.debug", defaultValue="true") protected boolean debug
      Deprecated.
      Setting this flag to false is replaced by <debuglevel>none</debuglevel>.
      Whether to include debugging information in the compiled class files. The amount of debugging information to include is specified by the debuglevel parameter. If this debug flag is true, then the -g option may be added to compiler arguments with a value determined by the debuglevel argument. If this debug flag is false, then the -g:none option will be added to the compiler arguments.
      See Also:
    • debuglevel

      @Parameter(property="maven.compiler.debuglevel") protected String debuglevel
      Kinds of debugging information to include in the compiled class files. Legal values are lines, vars, source, all and none. Values other than all and none can be combined in a comma-separated list.

      If debug level is not specified, then the -g option will not be added, which means that the default debugging information will be generated (typically lines and source but not vars).

      If debug level is all, then only the -g option is added, which means that all debugging information will be generated. If debug level is anything else, then the comma-separated list of keywords is appended to the -g command-line switch.

      Since:
      2.1
      See Also:
    • optimize

      @Deprecated(forRemoval=true) @Parameter(property="maven.compiler.optimize") protected Boolean optimize
      Deprecated, for removal: This API element is subject to removal in a future version.
      This property is ignored.
      Whether to optimize the compiled code using the compiler's optimization methods.
    • verbose

      @Parameter(property="maven.compiler.verbose", defaultValue="false") protected boolean verbose
      Whether to show messages about what the compiler is doing. If true, then the -verbose option will be added to compiler arguments. In addition, files such as target/javac.args will be generated even on successful compilation.
      See Also:
    • showCompilationChanges

      @Parameter(property="maven.compiler.showCompilationChanges", defaultValue="false") protected boolean showCompilationChanges
      Whether to provide more details about why a module is rebuilt. This is used only if incrementalCompilation is set to something else than "none".
      See Also:
    • showDeprecation

      @Parameter(property="maven.compiler.showDeprecation", defaultValue="false") protected boolean showDeprecation
      Whether to show source locations where deprecated APIs are used. If true, then the -deprecation option will be added to compiler arguments. That option is itself a shorthand for -Xlint:deprecation.
      See Also:
    • showWarnings

      @Parameter(property="maven.compiler.showWarnings", defaultValue="true") protected boolean showWarnings
      Whether to show compilation warnings. If false, then the -nowarn option will be added to compiler arguments. That option is itself a shorthand for -Xlint:none.
      See Also:
    • failOnWarning

      @Parameter(property="maven.compiler.failOnWarning", defaultValue="false") protected boolean failOnWarning
      Whether the build will stop if there are compilation warnings. If true, then the -Werror option will be added to compiler arguments.
      Since:
      3.6
      See Also:
    • failOnError

      @Parameter(property="maven.compiler.failOnError", defaultValue="true") protected boolean failOnError
      Whether the build will stop if there are compilation errors.
      Since:
      2.0.2
      See Also:
    • outputFileName

      @Parameter @Deprecated(since="4.0.0", forRemoval=true) protected String outputFileName
      Deprecated, for removal: This API element is subject to removal in a future version.
      Bundling many class files into a single file should be done by other plugins.
      Sets the name of the output file when compiling a set of sources to a single file.

      expression="${project.build.finalName}"

    • outputTimestamp

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(defaultValue="${project.build.outputTimestamp}") protected String outputTimestamp
      Deprecated, for removal: This API element is subject to removal in a future version.
      Not used by the compiler plugin since it does not generate archive.
      Timestamp for reproducible output archive entries. It can be either formatted as ISO 8601 yyyy-MM-dd'T'HH:mm:ssXXX or as an int representing seconds since the epoch (like SOURCE_DATE_EPOCH).
      Since:
      3.12.0
    • incrementalCompilation

      @Parameter(property="maven.compiler.incrementalCompilation") protected String incrementalCompilation
      The algorithm to use for selecting which files to compile. Values can be dependencies, sources, classes, rebuild-on-change, rebuild-on-add, modules or none.

      options: recompile all source files if the compiler options changed. Changes are detected on a best-effort basis only.

      dependencies: recompile all source files if at least one dependency (JAR file) changed since the last build. This check is based on the last modification times of JAR files.

      sources: recompile source files modified since the last build. In addition, if a source file has been deleted, then all source files are recompiled. This check is based on the modification times of source files rather than the modification times of the *.class files.

      classes: recompile source files (*.java) associated to no output file (*.class) or associated to an output file older than the source. This algorithm does not check if a source file has been removed, potentially leaving non-recompiled classes with references to classes that no longer exist.

      The sources and classes values are partially redundant, doing the same work in different ways. It is usually not necessary to specify those two values.

      modules: recompile modules and let the compiler decides which individual files to recompile. The compiler plugin does not enumerate the source files to recompile (actually, it does not scan at all the source directories). Instead, it only specifies the module to recompile using the --module option. The Java compiler will scan the source directories itself and compile only those source files that are newer than the corresponding files in the output directory.

      rebuild-on-add: modifier for recompiling all source files when the addition of a new file is detected. This flag is effective only when used together with sources or classes. When used with classes, it provides a way to detect class renaming (this is not needed with sources for detecting renaming).

      rebuild-on-change: modifier for recompiling all source files when a change is detected in at least one source file. This flag is effective only when used together with sources or classes. It does not rebuild when a new source file is added without change in other files, unless rebuild-on-add is also specified.

      none: the compiler plugin unconditionally specifies all sources to the Java compiler. This option is mutually exclusive with all other incremental compilation options.

      Limitations

      In all cases, the current compiler-plugin does not detect structural changes other than file addition or removal. For example, the plugin does not detect whether a method has been removed in a class.

      Default value

      The default value depends on the context. If there is no annotation processor, then the default is "options,dependencies,sources". It means that a full rebuild will be done if the compiler options or the dependencies changed, or if a source file has been deleted. Otherwise, only the modified source files will be recompiled.

      If an annotation processor is present (e.g., proc set to a value other than "none"), then the default value is same as above with the addition of "rebuild-on-add,rebuild-on-change". It means that a full rebuild will be done if any kind of change is detected.

      Since:
      4.0.0
      See Also:
    • useIncrementalCompilation

      @Deprecated(since="4.0.0") @Parameter(property="maven.compiler.useIncrementalCompilation") protected Boolean useIncrementalCompilation
      Deprecated.
      Replaced by incrementalCompilation. A value of true in this old property is equivalent to "dependencies,sources,rebuild-on-add" in the new property, and a value of false is equivalent to "classes".
      Whether to enable/disable incremental compilation feature.
      Since:
      3.1
    • fileExtensions

      @Parameter(defaultValue="class,jar") protected List<String> fileExtensions
      File extensions to check timestamp for incremental build. Default contains only class and jar. TODO: Rename with a name making clearer that this parameter is about incremental build.
      Since:
      3.1
      See Also:
    • staleMillis

      @Parameter(property="lastModGranularityMs", defaultValue="0") protected int staleMillis
      The granularity in milliseconds of the last modification date for testing whether a source needs recompilation.
      See Also:
    • fork

      @Parameter(property="maven.compiler.fork", defaultValue="false") protected boolean fork
      Allows running the compiler in a separate process. If false, the plugin uses the built-in compiler, while if true it will use an executable.
      See Also:
    • jdkToolchain

      @Parameter protected Map<String,String> jdkToolchain
      Requirements for this JDK toolchain for using a different javac than the one of the JDK used by Maven. This overrules the toolchain selected by the maven-toolchain-plugin. See Guide to Toolchains for more info.
       <configuration>
         <jdkToolchain>
           <version>11</version>
         </jdkToolchain>
         ...
       </configuration>
      
       <configuration>
         <jdkToolchain>
           <version>1.8</version>
           <vendor>zulu</vendor>
         </jdkToolchain>
         ...
       </configuration>
       
      Since:
      3.6
      See Also:
    • compilerId

      @Parameter(property="maven.compiler.compilerId") protected String compilerId
      Identifier of the compiler to use. This identifier shall match the identifier of a compiler known to the JDK tool chain, or the name of a JavaCompiler instance registered as a service findable by ServiceLoader. See this guide for more information. If unspecified, then the system Java compiler is used. The identifier of the system Java compiler is usually javac.
      See Also:
    • compilerVersion

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(property="maven.compiler.compilerVersion") protected String compilerVersion
      Deprecated, for removal: This API element is subject to removal in a future version.
      This parameter is no longer used by the underlying compilers.
      Version of the compiler to use if fork is set to true. Examples! "1.3", "1.5".
      See Also:
    • forceLegacyJavacApi

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(property="maven.compiler.forceLegacyJavacApi") protected Boolean forceLegacyJavacApi
      Deprecated, for removal: This API element is subject to removal in a future version.
      Ignored because the compiler plugin now always use the javax.tools API.
      Whether to use the legacy com.sun.tools.javac API instead of javax.tools API.
      Since:
      3.13
      See Also:
    • forceJavacCompilerUse

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(property="maven.compiler.forceJavacCompilerUse") protected Boolean forceJavacCompilerUse
      Deprecated, for removal: This API element is subject to removal in a future version.
      Ignored because java.lang.Compiler has been deprecated and removed from the JDK.
      Whether to use legacy compiler API.
      Since:
      3.0
    • compilerReuseStrategy

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(property="maven.compiler.compilerReuseStrategy") protected String compilerReuseStrategy
      Deprecated, for removal: This API element is subject to removal in a future version.
      Not supported anymore. The reuse of JavaFileManager instance is plugin implementation details.
      Strategy to re use javacc class created. Legal values are:
      • reuseCreated (default) – will reuse already created but in case of multi-threaded builds, each thread will have its own instance.
      • reuseSame – the same Javacc class will be used for each compilation even for multi-threaded build.
      • alwaysNew – a new Javacc class will be created for each compilation.
      Note this parameter value depends on the OS/JDK you are using, but the default value should work on most of env.
      Since:
      2.5
    • skipMultiThreadWarning

      @Deprecated(since="4.0.0", forRemoval=true) @Parameter(property="maven.compiler.skipMultiThreadWarning") protected Boolean skipMultiThreadWarning
      Deprecated, for removal: This API element is subject to removal in a future version.
      Deprecated as a consequence of compilerReuseStrategy deprecation.
      Since:
      2.5
    • executable

      @Parameter(property="maven.compiler.executable") protected String executable
      Executable of the compiler to use when fork is true. If this parameter is specified, then the jdkToolchain is ignored.
      See Also:
    • meminitial

      @Parameter(property="maven.compiler.meminitial") protected String meminitial
      Initial size, in megabytes, of the memory allocation pool if fork is set to true. Examples: "64", "64M". Suffixes "k" (for kilobytes) and "G" (for gigabytes) are also accepted. If no suffix is provided, "M" is assumed.
      Since:
      2.0.1
      See Also:
    • maxmem

      @Parameter(property="maven.compiler.maxmem") protected String maxmem
      Maximum size, in megabytes, of the memory allocation pool if fork is set to true. Examples: "128", "128M". Suffixes "k" (for kilobytes) and "G" (for gigabytes) are also accepted. If no suffix is provided, "M" is assumed.
      Since:
      2.0.1
      See Also:
    • basedir

      @Parameter(defaultValue="${project.basedir}", required=true, readonly=true) protected Path basedir
      The directory to run the compiler from if fork is true.
    • mojoStatusPath

      @Parameter(defaultValue="${project.build.directory}/maven-status/${mojo.plugin.descriptor.artifactId}/${mojo.executionId}.cache", required=true, readonly=true) protected Path mojoStatusPath
      Path to a file where to cache information about the last incremental build. This is used when "incremental" builds are enabled for detecting additions or removals of source files, or changes in plugin configuration. This file should be in the output directory and can be deleted at any time
    • session

      @Inject protected org.apache.maven.api.Session session
      The current build session instance.
    • project

      @Inject protected org.apache.maven.api.Project project
      The current project instance.
    • projectManager

      @Inject protected org.apache.maven.api.services.ProjectManager projectManager
    • artifactManager

      @Inject protected org.apache.maven.api.services.ArtifactManager artifactManager
    • toolchainManager

      @Inject protected org.apache.maven.api.services.ToolchainManager toolchainManager
    • messageBuilderFactory

      @Inject protected org.apache.maven.api.services.MessageBuilderFactory messageBuilderFactory
    • logger

      @Inject protected org.apache.maven.api.plugin.Log logger
      The logger for reporting information or warnings to the user. Currently, this is also used for console output.

      Thread safety

      This logger should be thread-safe if the ToolExecutor is executed in a background thread.
  • Constructor Details

    • AbstractCompilerMojo

      protected AbstractCompilerMojo(org.apache.maven.api.PathScope compileScope)
      Creates a new MOJO.
      Parameters:
      compileScope - MAIN_COMPILE or TEST_COMPILE
  • Method Details

    • getIncludes

      protected abstract Set<String> getIncludes()
      Returns the inclusion filters for the compiler, or an empty list for all Java source files. The filter patterns are described in FileSystem.getPathMatcher(String). If no syntax is specified, the default syntax is a derivative of "glob" compatible with the behavior of Maven 3.
      Returns:
      the inclusion filters for the compiler, or an empty list for all Java source files
    • getExcludes

      protected abstract Set<String> getExcludes()
      Returns the exclusion filters for the compiler, or an empty list if none. The filter patterns are described in FileSystem.getPathMatcher(String). If no syntax is specified, the default syntax is a derivative of "glob" compatible with the behavior of Maven 3.
      Returns:
      the exclusion filters for the compiler, or an empty list if none
    • getIncrementalExcludes

      protected abstract Set<String> getIncrementalExcludes()
      Returns the exclusion filters for the incremental calculation. Updated source files, if excluded by this filter, will not cause the project to be rebuilt.
      Returns:
      the exclusion filters for the incremental calculation
      See Also:
      • SourceFile.ignoreModification
    • getOutputDirectory

      @Nonnull protected abstract Path getOutputDirectory()
      Returns the destination directory (or class output directory) for class files. This directory will be given to the -d Java compiler option.
      Returns:
      the destination directory (or class output directory) for class files
    • getSource

      @Nullable protected String getSource()
      Returns the --source argument for the Java compiler. The default implementation returns the source value.
      Returns:
      the --source argument for the Java compiler
    • getTarget

      @Nullable protected String getTarget()
      Returns the --target argument for the Java compiler. The default implementation returns the target value.
      Returns:
      the --target argument for the Java compiler
    • getRelease

      @Nullable protected String getRelease()
      Returns the --release argument for the Java compiler. The default implementation returns the release value.
      Returns:
      the --release argument for the Java compiler
    • getGeneratedSourcesDirectory

      @Nullable protected abstract Path getGeneratedSourcesDirectory()
      Returns the path where to place generated source files created by annotation processing.
      Returns:
      the path where to place generated source files created by annotation processing
    • getDebugFileName

      @Nullable protected abstract String getDebugFileName()
      Returns the file where to dump the command-line when debug logging is enabled or when the compilation failed. For example, if the value is "javac", then the Java compiler can be launched from the command-line by typing javac @target/javac.args. The debug file will contain the compiler options together with the list of source files to compile.

      Note: debug logging should not be confused with the debug flag.

      Returns:
      the file where to dump the command-line when debug logging is enabled or when the compilation failed
      See Also:
    • execute

      public void execute() throws org.apache.maven.api.plugin.MojoException
      Runs the Java compiler. This method performs the following steps:
      1. Get a Java compiler by a call to compiler().
      2. Get the options to give to the compiler by a call to parseParameters(OptionChecker).
      3. Get an executor with createExecutor(DiagnosticListener) with the default listener.
      4. Apply the incremental build if enabled.
      5. Execute the compilation.
      6. Shows messages in the logger.
      Specified by:
      execute in interface org.apache.maven.api.plugin.Mojo
      Throws:
      org.apache.maven.api.plugin.MojoException - if the compiler cannot be run
    • createExecutor

      public ToolExecutor createExecutor(DiagnosticListener<? super JavaFileObject> listener) throws IOException
      Creates a new task by taking a snapshot of the current configuration of this MOJO. This method creates the output directory if it does not already exist.

      Multi-threading

      This method and the returned objects are not thread-safe. However, this method takes a snapshot of the configuration of this MOJO. Changes in this MOJO after this method call will not affect the returned executor. Therefore, the executor can safely be executed in a background thread, provided that the logger is thread-safe.
      Parameters:
      listener - where to send compilation warnings, or null for the Maven logger
      Returns:
      the task to execute for compiling the project using the configuration in this MOJO
      Throws:
      org.apache.maven.api.plugin.MojoException - if this method identifies an invalid parameter in this 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
    • compiler

      public JavaCompiler compiler() throws org.apache.maven.api.plugin.MojoException
      Returns the compiler to use for compiling the code. If fork is true, the returned compiler will be a wrapper for a command line. Otherwise, it will be the compiler identified by compilerId if a value was supplied, or the standard compiler provided with the Java platform otherwise.
      Returns:
      the compiler to use for compiling the code
      Throws:
      org.apache.maven.api.plugin.MojoException - if no compiler was found
    • parseParameters

      public Options parseParameters(OptionChecker compiler)
      Parses the parameters declared in the MOJO. The release parameter is excluded because it is handled in a special way in order to support the compilation of multi-version projects.
      Parameters:
      compiler - the tools to use for verifying the validity of options
      Returns:
      the options after validation