Class AbstractCompilerMojo
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled,org.apache.maven.plugin.Mojo
- Direct Known Subclasses:
CompilerMojo,TestCompilerMojo
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List<DependencyCoordinate>Classpath elements to supply as annotation processor path.private String[]Names of annotation processors to run.private org.apache.maven.artifact.handler.manager.ArtifactHandlerManagerArtifact handler manager.private FileThe directory to run the compiler from if fork is true.private FileThe target directory of the compiler if fork is true.Sets the arguments to be passed to the compiler.protected StringSets the unformatted single argument string to be passed to the compiler.Deprecated.private StringThe compiler id of the compiler to use.private org.codehaus.plexus.compiler.manager.CompilerManagerPlexus compiler manager.private StringStrategy 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 instancereuseSame: the same Javacc class will be used for each compilation even for multi-threaded buildalwaysNew: 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.private StringVersion of the compiler to use, ex.private booleanPackage info source files that only contain javadoc and no annotation on the package can lead to no class file being generated by the compiler.private booleanSet totrueto include debugging information in the compiled class files.private StringKeyword list to be appended to the-gcommand-line switch.(package private) static final String(package private) static final Stringprivate booleanSet totrueto Enable preview language features of the java compilerprivate StringThe -encoding argument for the Java compiler.private StringSets the executable of the compiler to use whenforkistrue.private booleanIndicates whether the build will continue even if there are compilation errors.private booleanIndicates whether the build will continue even if there are compilation warnings.File extensions to check timestamp for incremental build.private booleancompiler 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 configurationprivate booleanAllows running the compiler in a separate process.Specify the requirements for this jdk toolchain for using a differentjavacthan the one of the JRE used by Maven.private StringSets the maximum size, in megabytes, of the memory allocation pool, ex.private StringInitial size, in megabytes, of the memory allocation pool, ex.(package private) static final Stringprivate org.apache.maven.plugin.MojoExecutionprivate booleanDeprecated.This property is a no-op injavac.private StringSets the name of the output file when compiling a set of sources to a single file.private booleanSet totrueto generate metadata for reflection on method parameters.private StringSets whether annotation processing is performed or not.private org.apache.maven.project.MavenProjectThe current project instance.protected static final Stringprotected StringThe -release argument for the Java compiler, supported since Java9private org.apache.maven.repository.RepositorySystemResolves the artifacts needed.private org.apache.maven.artifact.resolver.ResolutionErrorHandlerThrows an exception on artifact resolution errors.private org.apache.maven.execution.MavenSessionThe current build session instance.private booleanSets whether to show source locations where deprecated APIs are used.private booleanSet totrueto show compilation warnings.private booleanprotected StringThe -source argument for the Java compiler.private intSets the granularity in milliseconds of the last modification date for testing whether a source needs recompilation.protected StringThe -target argument for the Java compiler.private booleanprivate org.apache.maven.toolchain.ToolchainManagerprivate booleanto enable/disable incremental compilation feature.private booleanSet totrueto show messages about what the compiler is doing.Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncomputeStaleSources(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler, org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner scanner) protected org.codehaus.plexus.compiler.CompilerResultconvertToCompilerResult(List<org.codehaus.plexus.compiler.CompilerError> compilerErrors) private voidcreateMissingPackageInfoClasses(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping sourceMapping, Set<File> sources) voidexecute()private byte[]generatePackage(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, String javaFile) protected Dateprotected abstract StringgetCompileSources(org.codehaus.plexus.compiler.Compiler compiler, org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration) protected abstract Stringprotected abstract Fileprivate Stringprivate StringgetMemoryValue(String setting) private intgetOpcode(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration) protected abstract Fileprotected final org.apache.maven.project.MavenProjectprotected abstract Stringprotected inttry to get thread count if a Maven 3 build, using reflection as the plugin must not be maven3 api dependentprotected abstract Stringprotected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScannergetSourceInclusionScanner(int staleMillis) protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScannergetSourceInclusionScanner(String inputFileEnding) private org.codehaus.plexus.compiler.util.scan.mapping.SourceMappinggetSourceMapping(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler) protected abstract Stringprotected final org.apache.maven.toolchain.Toolchainprivate booleanhasNewFile(File classPathEntry, Date buildStartTime) protected booleanWe 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.private booleanprivate booleanisSourceChanged(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler) protected booleanprotected abstract voidpreparePaths(Set<File> sourceFiles) removeEmptyCompileSourceRoots(List<String> compileSourceRootsList) voidsetRelease(String release) voidprivate voidwriteConfig(org.apache.maven.shared.utils.logging.MessageBuilder mb) private voidwritePlugin(org.apache.maven.shared.utils.logging.MessageBuilder mb) Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
-
Field Details
-
PS
-
DEFAULT_SOURCE
- See Also:
-
DEFAULT_TARGET
- See Also:
-
MODULE_INFO_TARGET
- See Also:
-
failOnError
@Parameter(property="maven.compiler.failOnError", defaultValue="true") private boolean failOnErrorIndicates 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 failOnWarningIndicates whether the build will continue even if there are compilation warnings.- Since:
- 3.6
-
debug
@Parameter(property="maven.compiler.debug", defaultValue="true") private boolean debugSet totrueto include debugging information in the compiled class files. -
parameters
@Parameter(property="maven.compiler.parameters", defaultValue="false") private boolean parametersSet totrueto generate metadata for reflection on method parameters.- Since:
- 3.6.2
-
enablePreview
@Parameter(property="maven.compiler.enablePreview", defaultValue="false") private boolean enablePreviewSet totrueto Enable preview language features of the java compiler- Since:
- 3.10.1
-
verbose
@Parameter(property="maven.compiler.verbose", defaultValue="false") private boolean verboseSet totrueto show messages about what the compiler is doing. -
showDeprecation
@Parameter(property="maven.compiler.showDeprecation", defaultValue="false") private boolean showDeprecationSets whether to show source locations where deprecated APIs are used. -
optimize
@Deprecated @Parameter(property="maven.compiler.optimize", defaultValue="false") private boolean optimizeDeprecated.This property is a no-op injavac.Set totrueto optimize the compiled code using the compiler's optimization methods. -
showWarnings
@Parameter(property="maven.compiler.showWarnings", defaultValue="false") private boolean showWarningsSet totrueto show compilation warnings. -
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 -
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 -
release
The -release argument for the Java compiler, supported since Java9- Since:
- 3.6
-
encoding
@Parameter(property="encoding", defaultValue="${project.build.sourceEncoding}") private String encodingThe -encoding argument for the Java compiler.- Since:
- 2.1
-
staleMillis
@Parameter(property="lastModGranularityMs", defaultValue="0") private int staleMillisSets the granularity in milliseconds of the last modification date for testing whether a source needs recompilation. -
compilerId
The compiler id of the compiler to use. See this guide for more information. -
compilerVersion
Version of the compiler to use, ex. "1.3", "1.5", ifforkis set totrue. -
fork
@Parameter(property="maven.compiler.fork", defaultValue="false") private boolean forkAllows running the compiler in a separate process. Iffalseit uses the built in compiler, while iftrueit will use an executable. -
meminitial
Initial size, in megabytes, of the memory allocation pool, ex. "64", "64m" ifforkis set totrue.- Since:
- 2.0.1
-
maxmem
Sets the maximum size, in megabytes, of the memory allocation pool, ex. "128", "128m" ifforkis set totrue.- Since:
- 2.0.1
-
executable
Sets the executable of the compiler to use whenforkistrue. -
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
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
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. Example:
<configuration> <annotationProcessorPaths> <path> <groupId>org.sample</groupId> <artifactId>sample-annotation-processor</artifactId> <version>1.2.3</version> </path> <!-- ... more ... --> </annotationProcessorPaths> </configuration>- Since:
- 3.5
-
compilerArguments
Deprecated.usecompilerArgsinstead.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
-Joptions are only passed through ifforkis set totrue.To pass
-Xmaxerrs 1000 -Xlint -Xlint:-path -Averbose=trueyou should include the following:<compilerArguments> <Xmaxerrs>1000</Xmaxerrs> <Xlint/> <Xlint:-path/> <Averbose>true</Averbose> </compilerArguments>
- Since:
- 2.0.1
-
compilerArgs
Sets the arguments to be passed to the compiler.
Note that
Example:-Joptions are only passed through ifforkis set totrue.<compilerArgs> <arg>-Xmaxerrs</arg> <arg>1000</arg> <arg>-Xlint</arg> <arg>-J-Duser.language=en_us</arg> </compilerArgs>
- Since:
- 3.1
-
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 usecompilerArguments.This is because the list of valid arguments passed to a Java compiler varies based on the compiler version.
Note that
-Joptions are only passed through ifforkis set totrue. -
outputFileName
Sets the name of the output file when compiling a set of sources to a single file. expression="${project.build.finalName}" -
debuglevel
Keyword list to be appended to the-gcommand-line switch. Legal values are none or a comma-separated list of the following keywords:lines,vars, andsource. 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
-
toolchainManager
@Component private org.apache.maven.toolchain.ToolchainManager toolchainManager -
jdkToolchain
Specify the requirements for this jdk toolchain for using a different
(see Guide to Toolchains for more info)javacthan the one of the JRE used by Maven. This overrules the toolchain selected by the maven-toolchain-plugin.<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
The directory to run the compiler from if fork is true. -
buildDirectory
@Parameter(defaultValue="${project.build.directory}", required=true, readonly=true) private File buildDirectoryThe target directory of the compiler if fork is true. -
compilerManager
@Component private org.codehaus.plexus.compiler.manager.CompilerManager compilerManagerPlexus compiler manager. -
session
@Parameter(defaultValue="${session}", readonly=true, required=true) private org.apache.maven.execution.MavenSession sessionThe 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 projectThe 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 compilerReuseStrategyStrategy 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 instancereuseSame: the same Javacc class will be used for each compilation even for multi-threaded buildalwaysNew: a new Javacc class will be created for each compilation
- 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 forceJavacCompilerUsecompiler 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
File extensions to check timestamp for incremental build. Default contains onlyclassandjar.- Since:
- 3.1
-
useIncrementalCompilation
@Parameter(defaultValue="true", property="maven.compiler.useIncrementalCompilation") private boolean useIncrementalCompilationto 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 createMissingPackageInfoClassPackage 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 oftruecauses an empty class file to be generated. This behavior can be changed by setting this parameter tofalse.- Since:
- 3.10
-
repositorySystem
@Component private org.apache.maven.repository.RepositorySystem repositorySystemResolves the artifacts needed. -
artifactHandlerManager
@Component private org.apache.maven.artifact.handler.manager.ArtifactHandlerManager artifactHandlerManagerArtifact handler manager. -
resolutionErrorHandler
@Component private org.apache.maven.artifact.resolver.ResolutionErrorHandler resolutionErrorHandlerThrows an exception on artifact resolution errors. -
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
-
getModulepathElements
-
getPathElements
-
getCompileSourceRoots
-
preparePaths
-
getOutputDirectory
-
getSource
-
getTarget
-
getRelease
-
getCompilerArgument
-
getCompilerArguments
-
getGeneratedSourcesDirectory
-
getDebugFileName
-
getProject
protected final org.apache.maven.project.MavenProject getProject() -
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionException, CompilationFailureException- Throws:
org.apache.maven.plugin.MojoExecutionExceptionCompilationFailureException
-
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.InclusionScanExceptionIOException
-
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() -
convertToCompilerResult
protected org.codehaus.plexus.compiler.CompilerResult convertToCompilerResult(List<org.codehaus.plexus.compiler.CompilerError> compilerErrors) -
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.MojoExecutionExceptionorg.codehaus.plexus.compiler.CompilerException
-
isSourceChanged
private boolean isSourceChanged(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler) throws org.codehaus.plexus.compiler.CompilerException, org.apache.maven.plugin.MojoExecutionException - Parameters:
compilerConfiguration-compiler-- Returns:
trueif at least a single source file is newer than it's class file- Throws:
org.codehaus.plexus.compiler.CompilerExceptionorg.apache.maven.plugin.MojoExecutionException
-
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
-
getMemoryValue
-
getToolchain
protected final org.apache.maven.toolchain.Toolchain getToolchain() -
isDigits
-
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.MojoExecutionExceptionorg.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.CompilerExceptionorg.apache.maven.plugin.MojoExecutionException
-
removeEmptyCompileSourceRoots
-
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:
trueif at least one single dependency has changed.
-
hasNewFile
- Parameters:
classPathEntry- entry to checkbuildStartTime- 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
-
getMavenCompilerPluginVersion
-
setTarget
-
setRelease
-
compilerArgsinstead.