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
TODO: At least one step could be optimized, currently the plugin will do two scans of all the source code if the compiler has to have the entire set of sources. This is currently the case for at least the C# compiler and most likely all the other .NET compilers too.
Since:
2.0
Author:
others, Trygve Laugstøl
  • Field Details

    • PS

      protected static final String PS
    • failOnError

      @Parameter(property="maven.compiler.failOnError", defaultValue="true") protected boolean failOnError
      Indicates whether the build will continue even if there are compilation errors.
      Since:
      2.0.2
    • failOnWarning

      @Parameter(property="maven.compiler.failOnWarning", defaultValue="false") protected boolean failOnWarning
      Indicates whether the build will continue even if there are compilation warnings.
      Since:
      3.6
    • debug

      @Parameter(property="maven.compiler.debug", defaultValue="true") protected boolean debug
      Set to true to include debugging information in the compiled class files.
      See Also:
    • parameters

      @Parameter(property="maven.compiler.parameters", defaultValue="false") protected boolean parameters
      Set to true to generate metadata for reflection on method parameters.
      Since:
      3.6.2
      See Also:
    • enablePreview

      @Parameter(property="maven.compiler.enablePreview", defaultValue="false") protected boolean enablePreview
      Set to true to enable preview language features of the java compiler
      Since:
      3.10.1
      See Also:
    • verbose

      @Parameter(property="maven.compiler.verbose", defaultValue="false") protected boolean verbose
      Set to true to show messages about what the compiler is doing.
      See Also:
    • showDeprecation

      @Parameter(property="maven.compiler.showDeprecation", defaultValue="false") protected boolean showDeprecation
      Sets whether to show source locations where deprecated APIs are used.
    • optimize

      @Deprecated @Parameter(property="maven.compiler.optimize", defaultValue="false") protected boolean optimize
      Deprecated.
      This property is a no-op in javac.
      Set to true to optimize the compiled code using the compiler's optimization methods.
    • showWarnings

      @Parameter(property="maven.compiler.showWarnings", defaultValue="true") protected boolean showWarnings
      Set to false to disable warnings during compilation.
    • source

      @Parameter(property="maven.compiler.source", defaultValue="1.8") protected String source

      The -source argument for the Java compiler.

      NOTE:

      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

      See Also:
    • target

      @Parameter(property="maven.compiler.target", defaultValue="1.8") protected String target

      The -target argument for the Java compiler.

      NOTE:

      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

      See Also:
    • release

      @Parameter(property="maven.compiler.release") protected String release
      The -release argument for the Java compiler, supported since Java9
      Since:
      3.6
      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:
    • staleMillis

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

      @Parameter(property="maven.compiler.compilerId", defaultValue="javac") protected String compilerId
      The compiler id of the compiler to use. See this guide for more information.
    • compilerVersion

      @Deprecated @Parameter(property="maven.compiler.compilerVersion") protected String compilerVersion
      Deprecated.
      This parameter is no longer evaluated by the underlying compilers, instead the actual version of the javac binary is automatically retrieved.
      Version of the compiler to use, ex. "1.3", "1.5", if fork is set to true.
    • fork

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

      @Parameter(property="maven.compiler.meminitial") protected String meminitial
      Initial size, in megabytes, of the memory allocation pool, ex. "64", "64m" if fork is set to true.
      Since:
      2.0.1
    • maxmem

      @Parameter(property="maven.compiler.maxmem") protected String maxmem
      Sets the maximum size, in megabytes, of the memory allocation pool, ex. "128", "128m" if fork is set to true.
      Since:
      2.0.1
    • executable

      @Parameter(property="maven.compiler.executable") protected String executable
      Sets the executable of the compiler to use when fork is true.
    • proc

      @Parameter(property="maven.compiler.proc") protected String proc

      Sets whether annotation processing is performed or not. Only applies to JDK 1.6+ If not set, both compilation and annotation processing are performed at the same time.

      Allowed values are:

      • none - no annotation processing is performed.
      • only - only annotation processing is done, no compilation.
      • full - annotation processing and compilation.
      full is the default. Starting with JDK 21, this option must be set explicitly.
      Since:
      2.2
      See Also:
    • annotationProcessors

      @Parameter protected String[] annotationProcessors

      Names of annotation processors to run. Only applies to JDK 1.6+ If not set, the default annotation processors discovery process applies.

      Since:
      2.2
      See Also:
    • annotationProcessorPaths

      @Parameter protected List<DependencyCoordinate> annotationProcessorPaths

      Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation processors only in those classpath elements. If omitted, the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors.

      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

      @Parameter(defaultValue="false") protected boolean annotationProcessorPathsUseDepMgmt

      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
    • compilerArgs

      @Parameter protected List<String> compilerArgs

      Sets the arguments to be passed to the compiler.

      Note that -J options are only passed through if fork is set to true.

      Example:
       <compilerArgs>
         <arg>-Xmaxerrs</arg>
         <arg>1000</arg>
         <arg>-Xlint</arg>
         <arg>-J-Duser.language=en_us</arg>
       </compilerArgs>
       
      Since:
      3.1
      See Also:
    • compilerArgument

      @Parameter protected String compilerArgument

      Sets the unformatted single argument string to be passed to the compiler. To pass multiple arguments such as -Xmaxerrs 1000 (which are actually two arguments) you have to use compilerArgs.

      This is because the list of valid arguments passed to a Java compiler varies based on the compiler version.

      Note that -J options are only passed through if fork is set to true.

      See Also:
    • implicit

      @Parameter(property="maven.compiler.implicit") protected String implicit
      Keyword to be appended to the -implicit: command-line switch.
      Since:
      3.10.2
      See Also:
    • jdkToolchain

      @Parameter protected Map<String,String> jdkToolchain

      Specify the requirements for this jdk toolchain for using a different javac than the one of the JRE 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>
       
      note: requires at least Maven 3.3.1
      Since:
      3.6
    • basedir

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

      @Parameter(defaultValue="${project.build.directory}", required=true, readonly=true) protected Path buildDirectory
      The target directory of the compiler if fork is true.
    • compilerManager

      @Inject protected org.codehaus.plexus.compiler.manager.CompilerManager compilerManager
      Plexus compiler manager.
    • session

      @Inject protected org.apache.maven.api.Session session
      The current build session instance. This is used for toolchain manager API calls.
    • project

      @Inject protected org.apache.maven.api.Project project
      The current project instance. This is used for propagating generated-sources paths as compile/testCompile source roots.
    • compilerReuseStrategy

      @Parameter(defaultValue="${reuseCreated}", property="maven.compiler.compilerReuseStrategy") protected String compilerReuseStrategy
      Strategy to re use javacc class created:
      • 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

      @Parameter(defaultValue="false", property="maven.compiler.skipMultiThreadWarning") protected boolean skipMultiThreadWarning
      Since:
      2.5
    • forceLegacyJavacApi

      @Parameter(defaultValue="false", property="maven.compiler.forceLegacyJavacApi") protected boolean forceLegacyJavacApi
      The underlying compiler now uses javax.tools API if available in your current JDK. Set this to true to always use the legacy com.sun.tools.javac API instead.

      This only has an effect for compilerId being javac and fork being false.

      Since:
      3.13
    • mojoStatusPath

      @Parameter(defaultValue="maven-status/${mojo.plugin.descriptor.artifactId}/${mojo.goal}/${mojo.executionId}") protected String mojoStatusPath
      Since:
      3.0 needed for storing the status for the incremental build support.
    • fileExtensions

      @Parameter protected List<String> fileExtensions
      File extensions to check timestamp for incremental build. Default contains only class and jar.
      Since:
      3.1
    • useIncrementalCompilation

      @Parameter(defaultValue="true", property="maven.compiler.useIncrementalCompilation") protected boolean useIncrementalCompilation

      to enable/disable incremental compilation feature.

      This leads to two different modes depending on the underlying compiler. The default javac compiler does the following:

      • true (default) in this mode the compiler plugin determines whether any JAR files the current module depends on have changed in the current build run; or any source file was added, removed or changed since the last compilation. If this is the case, the compiler plugin recompiles all sources.
      • false (not recommended) this only compiles source files which are newer than their corresponding class files, namely which have changed since the last compilation. This does not recompile other classes which use the changed class, potentially leaving them with references to methods that no longer exist, leading to errors at runtime.
      Since:
      3.1
    • createMissingPackageInfoClass

      @Parameter(defaultValue="true", property="maven.compiler.createMissingPackageInfoClass") protected boolean createMissingPackageInfoClass
      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. This causes a file miss on the next compilations and forces an unnecessary recompilation. The default value of true causes an empty class file to be generated. This behavior can be changed by setting this parameter to false.
      Since:
      3.10
    • showCompilationChanges

      @Parameter(defaultValue="false", property="maven.compiler.showCompilationChanges") protected boolean showCompilationChanges
    • outputTimestamp

      @Parameter(defaultValue="${project.build.outputTimestamp}") protected String outputTimestamp
      Timestamp for reproducible output archive entries, 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
    • 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
  • Constructor Details

    • AbstractCompilerMojo

      public AbstractCompilerMojo()
  • Method Details

    • getSourceInclusionScanner

      protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner getSourceInclusionScanner(int staleMillis)
    • getSourceInclusionScanner

      protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner getSourceInclusionScanner(String inputFileEnding)
    • getClasspathElements

      protected abstract List<String> getClasspathElements()
    • getModulepathElements

      protected abstract List<String> getModulepathElements()
    • getPathElements

      protected abstract Map<String,org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor> getPathElements()
    • getCompileSourceRoots

      protected abstract List<Path> getCompileSourceRoots()
    • preparePaths

      protected abstract void preparePaths(Set<Path> sourceFiles)
    • getOutputDirectory

      protected abstract Path getOutputDirectory()
    • getSource

      protected abstract String getSource()
    • getTarget

      protected abstract String getTarget()
    • getRelease

      protected abstract String getRelease()
    • getCompilerArgument

      protected abstract String getCompilerArgument()
    • getGeneratedSourcesDirectory

      protected abstract Path getGeneratedSourcesDirectory()
    • getDebugFileName

      protected abstract String getDebugFileName()
    • getProject

      protected final org.apache.maven.api.Project getProject()
    • execute

      public void execute()
      Specified by:
      execute in interface org.apache.maven.api.plugin.Mojo
    • isTestCompile

      protected boolean isTestCompile()
    • getIncludes

      protected abstract Set<String> getIncludes()
    • getExcludes

      protected abstract Set<String> getExcludes()
    • getRequestThreadCount

      protected int getRequestThreadCount()
      try to get thread count if a Maven 3 build, using reflection as the plugin must not be maven3 api dependent
      Returns:
      number of thread for this build or 1 if not multi-thread build
    • getBuildStartTime

      protected Instant getBuildStartTime()
    • getToolchain

      protected final Optional<org.apache.maven.api.Toolchain> getToolchain()
    • isDependencyChanged

      protected boolean isDependencyChanged()
      We just compare the timestamps of all local dependency files (inter-module dependency classpath) and the own generated classes and if we got a file which is >= the build-started timestamp, then we caught a file which got changed during this build.
      Returns:
      true if at least one single dependency has changed.
    • setTarget

      public void setTarget(String target)
    • setRelease

      public void setRelease(String release)
    • getModuleDeclaration

      protected final Optional<Path> getModuleDeclaration(Set<Path> sourceFiles)
    • getLog

      protected org.apache.maven.api.plugin.Log getLog()