Class AbstractCompilerMojo

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

public abstract class AbstractCompilerMojo extends org.apache.maven.plugin.AbstractMojo
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
  • Field Details

    • PS

      protected static final String PS
    • INPUT_FILES_LST_FILENAME

      private static final String INPUT_FILES_LST_FILENAME
      See Also:
    • DEFAULT_SOURCE

      static final String DEFAULT_SOURCE
      See Also:
    • DEFAULT_TARGET

      static final String DEFAULT_TARGET
      See Also:
    • MODULE_INFO_TARGET

      static final String MODULE_INFO_TARGET
      See Also:
    • failOnError

      @Parameter(property="maven.compiler.failOnError", defaultValue="true") private 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") private 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") private boolean debug
      Set to true to include debugging information in the compiled class files.
    • parameters

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

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

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

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

      @Deprecated @Parameter(property="maven.compiler.optimize", defaultValue="false") private 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") private 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

    • 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

    • release

      @Parameter(property="maven.compiler.release") protected String release
      The -release argument for the Java compiler, supported since Java9
      Since:
      3.6
    • encoding

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

      @Parameter(property="lastModGranularityMs", defaultValue="0") private 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") private String compilerId
      The compiler id of the compiler to use. See this guide for more information.
    • compilerVersion

      @Parameter(property="maven.compiler.compilerVersion") private String compilerVersion
      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") private 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") private 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") private 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") private String executable
      Sets the executable of the compiler to use when fork is true.
    • proc

      @Parameter private 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.
      Since:
      2.2
    • annotationProcessors

      @Parameter private 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
    • annotationProcessorPaths

      @Parameter private 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
    • annotationProcessorPathsUseDepMgmt

      @Parameter(defaultValue="false") private 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 o transitive dependencies of those top-level paths.

      Since:
      3.12.0
    • compilerArguments

      @Parameter @Deprecated protected Map<String,String> compilerArguments
      Deprecated.
      use compilerArgs instead.

      Sets the arguments to be passed to the compiler (prepending a dash).

      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.

      To pass -Xmaxerrs 1000 -Xlint -Xlint:-path -Averbose=true you should include the following:

       <compilerArguments>
         <Xmaxerrs>1000</Xmaxerrs>
         <Xlint/>
         <Xlint:-path/>
         <Averbose>true</Averbose>
       </compilerArguments>
       
      Since:
      2.0.1
    • 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
    • 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.

    • outputFileName

      @Parameter private String outputFileName
      Sets the name of the output file when compiling a set of sources to a single file.

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

    • debuglevel

      @Parameter(property="maven.compiler.debuglevel") private String debuglevel
      Keyword list to be appended to the -g command-line switch. Legal values are none or a comma-separated list of the following keywords: lines, vars, and source. If debug level is not specified, by default, nothing will be appended to -g. If debug is not turned on, this attribute will be ignored.
      Since:
      2.1
    • implicit

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

      @Component private org.apache.maven.toolchain.ToolchainManager toolchainManager
    • jdkToolchain

      @Parameter private 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="${basedir}", required=true, readonly=true) private File basedir
      The directory to run the compiler from if fork is true.
    • buildDirectory

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

      @Component private org.codehaus.plexus.compiler.manager.CompilerManager compilerManager
      Plexus compiler manager.
    • session

      @Parameter(defaultValue="${session}", readonly=true, required=true) private org.apache.maven.execution.MavenSession session
      The current build session instance. This is used for toolchain manager API calls.
    • project

      @Parameter(defaultValue="${project}", readonly=true, required=true) private org.apache.maven.project.MavenProject 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") private 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") private boolean skipMultiThreadWarning
      Since:
      2.5
    • forceJavacCompilerUse

      @Parameter(defaultValue="false", property="maven.compiler.forceJavacCompilerUse") private boolean forceJavacCompilerUse
      compiler can now use javax.tools if available in your current jdk, you can disable this feature using -Dmaven.compiler.forceJavacCompilerUse=true or in the plugin configuration
      Since:
      3.0
    • mojoExecution

      @Parameter(defaultValue="${mojoExecution}", readonly=true, required=true) private org.apache.maven.plugin.MojoExecution mojoExecution
      Since:
      3.0 needed for storing the status for the incremental build support.
    • fileExtensions

      @Parameter(defaultValue="class,jar") private Set<String> fileExtensions
      File extensions to check timestamp for incremental build.
      Since:
      3.1
    • useIncrementalCompilation

      @Parameter(defaultValue="true", property="maven.compiler.useIncrementalCompilation") private 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") private 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") private boolean showCompilationChanges
    • outputTimestamp

      @Parameter(defaultValue="${project.build.outputTimestamp}") private 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
    • repositorySystem

      @Component private org.eclipse.aether.RepositorySystem repositorySystem
      Resolves the artifacts needed.
    • artifactHandlerManager

      @Component private org.apache.maven.artifact.handler.manager.ArtifactHandlerManager artifactHandlerManager
      Artifact handler manager.
    • targetOrReleaseSet

      private boolean targetOrReleaseSet
  • 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<String> getCompileSourceRoots()
    • preparePaths

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

      protected abstract File getOutputDirectory()
    • getSource

      protected abstract String getSource()
    • getTarget

      protected abstract String getTarget()
    • getRelease

      protected abstract String getRelease()
    • getCompilerArgument

      protected abstract String getCompilerArgument()
    • getCompilerArguments

      protected abstract Map<String,String> getCompilerArguments()
    • getGeneratedSourcesDirectory

      protected abstract File getGeneratedSourcesDirectory()
    • getDebugFileName

      protected abstract String getDebugFileName()
    • getProject

      protected final org.apache.maven.project.MavenProject getProject()
    • getModuleDeclaration

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

      public void execute() throws org.apache.maven.plugin.MojoExecutionException, CompilationFailureException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
      CompilationFailureException
    • createMissingPackageInfoClasses

      private void createMissingPackageInfoClasses(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping sourceMapping, Set<File> sources) throws org.codehaus.plexus.compiler.util.scan.InclusionScanException, IOException
      Throws:
      org.codehaus.plexus.compiler.util.scan.InclusionScanException
      IOException
    • generatePackage

      private byte[] generatePackage(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, String javaFile)
    • getOpcode

      private int getOpcode(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration)
    • isTestCompile

      protected boolean isTestCompile()
    • getCompileSources

      private Set<File> getCompileSources(org.codehaus.plexus.compiler.Compiler compiler, org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration) throws org.apache.maven.plugin.MojoExecutionException, org.codehaus.plexus.compiler.CompilerException
      Returns:
      all source files for the compiler
      Throws:
      org.apache.maven.plugin.MojoExecutionException
      org.codehaus.plexus.compiler.CompilerException
    • getIncludes

      protected abstract Set<String> getIncludes()
    • getExcludes

      protected abstract Set<String> getExcludes()
    • isSourceChanged

      private boolean isSourceChanged(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler)
      Parameters:
      compilerConfiguration -
      compiler -
      Returns:
      true if at least a single source file is newer than it's class file
    • 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 Date getBuildStartTime()
    • getBuildStartTimeInstant

      private Optional<Instant> getBuildStartTimeInstant()
    • getMemoryValue

      private String getMemoryValue(String setting)
    • getToolchain

      protected final org.apache.maven.toolchain.Toolchain getToolchain()
    • isDigits

      private boolean isDigits(String string)
    • computeStaleSources

      private Set<File> computeStaleSources(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler, org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner scanner) throws org.apache.maven.plugin.MojoExecutionException, org.codehaus.plexus.compiler.CompilerException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
      org.codehaus.plexus.compiler.CompilerException
    • getSourceMapping

      private org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping getSourceMapping(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler) throws org.codehaus.plexus.compiler.CompilerException, org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.codehaus.plexus.compiler.CompilerException
      org.apache.maven.plugin.MojoExecutionException
    • removeEmptyCompileSourceRoots

      private static List<String> removeEmptyCompileSourceRoots(List<String> compileSourceRootsList)
    • 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.
    • hasNewFile

      private boolean hasNewFile(Path file, Instant buildStartTime)
      Parameters:
      file - entry to check
      buildStartTime - time build start
      Returns:
      if any changes occurred
    • resolveProcessorPathEntries

      private List<String> resolveProcessorPathEntries() throws org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • convertToDependencies

      private List<org.eclipse.aether.graph.Dependency> convertToDependencies(List<DependencyCoordinate> annotationProcessorPaths) throws org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • getAnnotationProcessorPathVersion

      private String getAnnotationProcessorPathVersion(DependencyCoordinate annotationProcessorPath) throws org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • findManagedVersion

      private Optional<String> findManagedVersion(DependencyCoordinate dependencyCoordinate, List<org.apache.maven.model.Dependency> managedDependencies)
    • getManagedDependenciesForAnnotationProcessorPaths

      private List<org.eclipse.aether.graph.Dependency> getManagedDependenciesForAnnotationProcessorPaths()
    • getProjectManagedDependencies

      private List<org.apache.maven.model.Dependency> getProjectManagedDependencies()
    • convertToAetherExclusions

      private Set<org.eclipse.aether.graph.Exclusion> convertToAetherExclusions(Set<DependencyExclusion> exclusions)
    • writePlugin

      private void writePlugin(org.apache.maven.shared.utils.logging.MessageBuilder mb)
    • writeConfig

      private void writeConfig(org.apache.maven.shared.utils.logging.MessageBuilder mb)
    • getMavenCompilerPluginVersion

      private String getMavenCompilerPluginVersion()
    • hasInputFileTreeChanged

      private boolean hasInputFileTreeChanged(org.apache.maven.shared.incremental.IncrementalBuildHelper ibh, Set<File> inputFiles)
    • setTarget

      public void setTarget(String target)
    • setRelease

      public void setRelease(String release)
    • getImplicit

      final String getImplicit()
    • patchJdkModuleVersion

      private void patchJdkModuleVersion(org.codehaus.plexus.compiler.CompilerResult compilerResult, Set<File> sources) throws org.apache.maven.plugin.MojoExecutionException
      JDK-8318913 workaround: Patch module-info.class to set the java release version for java/jdk modules.
      Parameters:
      compilerResult - should succeed.
      sources - the list of the source files to check for the "module-info.java"
      Throws:
      org.apache.maven.plugin.MojoExecutionException
      See Also: