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:
AbstractCompilerMojo
,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 Summary
Fields Modifier and Type Field Description private java.util.List<DependencyCoordinate>
annotationProcessorPaths
Classpath elements to supply as annotation processor path.private java.lang.String[]
annotationProcessors
Names of annotation processors to run.private org.apache.maven.artifact.handler.manager.ArtifactHandlerManager
artifactHandlerManager
Artifact handler manager.private java.io.File
basedir
The directory to run the compiler from if fork is true.private java.io.File
buildDirectory
The target directory of the compiler if fork is true.protected java.util.List<java.lang.String>
compilerArgs
Sets the arguments to be passed to the compiler iffork
is set totrue
.protected java.lang.String
compilerArgument
Sets the unformatted single argument string to be passed to the compiler iffork
is set totrue
.protected java.util.Map<java.lang.String,java.lang.String>
compilerArguments
Deprecated.usecompilerArgs
instead.private java.lang.String
compilerId
The compiler id of the compiler to use.private org.codehaus.plexus.compiler.manager.CompilerManager
compilerManager
Plexus compiler manager.private java.lang.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 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 java.lang.String
compilerVersion
Version of the compiler to use, ex.private boolean
debug
Set totrue
to include debugging information in the compiled class files.private java.lang.String
debuglevel
Keyword list to be appended to the-g
command-line switch.(package private) static java.lang.String
DEFAULT_SOURCE
(package private) static java.lang.String
DEFAULT_TARGET
private java.lang.String
encoding
The -encoding argument for the Java compiler.private java.lang.String
executable
Sets the executable of the compiler to use whenfork
istrue
.private boolean
failOnError
Indicates whether the build will continue even if there are compilation errors.private boolean
failOnWarning
Indicates whether the build will continue even if there are compilation warnings.private java.util.List<java.lang.String>
fileExtensions
file extensions to check timestamp for incremental build default contains only.class
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 configurationprivate boolean
fork
Allows running the compiler in a separate process.private java.util.Map<java.lang.String,java.lang.String>
jdkToolchain
Specify the requirements for this jdk toolchain.private java.lang.String
maxmem
Sets the maximum size, in megabytes, of the memory allocation pool, ex.private java.lang.String
meminitial
Initial size, in megabytes, of the memory allocation pool, ex.(package private) static java.lang.String
MODULE_INFO_TARGET
private org.apache.maven.plugin.MojoExecution
mojoExecution
private boolean
optimize
Deprecated.This property is a no-op injavac
.private java.lang.String
outputFileName
Sets the name of the output file when compiling a set of sources to a single file.private boolean
parameters
Set totrue
to generate metadata for reflection on method parameters.private java.lang.String
proc
Sets whether annotation processing is performed or not.private org.apache.maven.project.MavenProject
project
The current project instance.protected static java.lang.String
PS
protected java.lang.String
release
The -release argument for the Java compiler, supported since Java9private org.apache.maven.repository.RepositorySystem
repositorySystem
Resolves the artifacts needed.private org.apache.maven.artifact.resolver.ResolutionErrorHandler
resolutionErrorHandler
Throws an exception on artifact resolution errors.private org.apache.maven.execution.MavenSession
session
The current build session instance.private boolean
showDeprecation
Sets whether to show source locations where deprecated APIs are used.private boolean
showWarnings
Set totrue
to show compilation warnings.private boolean
skipMultiThreadWarning
protected java.lang.String
source
The -source argument for the Java compiler.private int
staleMillis
Sets the granularity in milliseconds of the last modification date for testing whether a source needs recompilation.protected java.lang.String
target
The -target argument for the Java compiler.private boolean
targetOrReleaseSet
private org.apache.maven.toolchain.ToolchainManager
toolchainManager
private boolean
useIncrementalCompilation
to enable/disable incrementation compilation featureprivate boolean
verbose
Set totrue
to show messages about what the compiler is doing.
-
Constructor Summary
Constructors Constructor Description AbstractCompilerMojo()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private java.util.Set<java.io.File>
computeStaleSources(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.CompilerResult
convertToCompilerResult(java.util.List<org.codehaus.plexus.compiler.CompilerError> compilerErrors)
void
execute()
protected java.util.Date
getBuildStartTime()
protected abstract java.util.List<java.lang.String>
getClasspathElements()
protected abstract java.lang.String
getCompilerArgument()
protected abstract java.util.Map<java.lang.String,java.lang.String>
getCompilerArguments()
protected abstract java.util.List<java.lang.String>
getCompileSourceRoots()
private java.util.Set<java.io.File>
getCompileSources(org.codehaus.plexus.compiler.Compiler compiler, org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration)
protected abstract java.io.File
getGeneratedSourcesDirectory()
private java.lang.String
getMavenCompilerPluginVersion()
private java.lang.String
getMemoryValue(java.lang.String setting)
protected abstract java.util.List<java.lang.String>
getModulepathElements()
protected abstract java.io.File
getOutputDirectory()
protected abstract java.util.Map<java.lang.String,org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor>
getPathElements()
protected org.apache.maven.project.MavenProject
getProject()
protected abstract java.lang.String
getRelease()
protected int
getRequestThreadCount()
try to get thread count if a Maven 3 build, using reflection as the plugin must not be maven3 api dependentprotected abstract java.lang.String
getSource()
protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner
getSourceInclusionScanner(int staleMillis)
protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner
getSourceInclusionScanner(java.lang.String inputFileEnding)
private org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping
getSourceMapping(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler)
protected abstract java.lang.String
getTarget()
protected org.apache.maven.toolchain.Toolchain
getToolchain()
private boolean
hasNewFile(java.io.File classPathEntry, java.util.Date buildStartTime)
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 buid-started timestamp, then we catched a file which got changed during this build.private boolean
isDigits(java.lang.String string)
private boolean
isSourceChanged(org.codehaus.plexus.compiler.CompilerConfiguration compilerConfiguration, org.codehaus.plexus.compiler.Compiler compiler)
protected boolean
isTestCompile()
protected abstract void
preparePaths(java.util.Set<java.io.File> sourceFiles)
private static java.util.List<java.lang.String>
removeEmptyCompileSourceRoots(java.util.List<java.lang.String> compileSourceRootsList)
private java.util.List<java.lang.String>
resolveProcessorPathEntries()
void
setRelease(java.lang.String release)
void
setTarget(java.lang.String target)
private void
writeConfig(org.apache.maven.shared.utils.logging.MessageBuilder mb)
private void
writePlugin(org.apache.maven.shared.utils.logging.MessageBuilder mb)
-
-
-
Field Detail
-
PS
protected static final java.lang.String PS
-
DEFAULT_SOURCE
static final java.lang.String DEFAULT_SOURCE
- See Also:
- Constant Field Values
-
DEFAULT_TARGET
static final java.lang.String DEFAULT_TARGET
- See Also:
- Constant Field Values
-
MODULE_INFO_TARGET
static final java.lang.String MODULE_INFO_TARGET
- See Also:
- Constant Field Values
-
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 totrue
to include debugging information in the compiled class files.
-
parameters
@Parameter(property="maven.compiler.parameters", defaultValue="false") private boolean parameters
Set totrue
to generate metadata for reflection on method parameters.- Since:
- 3.6.2
-
verbose
@Parameter(property="maven.compiler.verbose", defaultValue="false") private boolean verbose
Set totrue
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 injavac
.Set totrue
to optimize the compiled code using the compiler's optimization methods.
-
showWarnings
@Parameter(property="maven.compiler.showWarnings", defaultValue="false") private boolean showWarnings
Set totrue
to show compilation warnings.
-
source
@Parameter(property="maven.compiler.source", defaultValue="1.6") protected java.lang.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
-
target
@Parameter(property="maven.compiler.target", defaultValue="1.6") protected java.lang.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
-
release
@Parameter(property="maven.compiler.release") protected java.lang.String release
The -release argument for the Java compiler, supported since Java9- Since:
- 3.6
-
encoding
@Parameter(property="encoding", defaultValue="${project.build.sourceEncoding}") private java.lang.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 java.lang.String compilerId
The compiler id of the compiler to use. See this guide for more information.
-
compilerVersion
@Parameter(property="maven.compiler.compilerVersion") private java.lang.String compilerVersion
Version of the compiler to use, ex. "1.3", "1.5", iffork
is set totrue
.
-
fork
@Parameter(property="maven.compiler.fork", defaultValue="false") private boolean fork
Allows running the compiler in a separate process. Iffalse
it uses the built in compiler, while iftrue
it will use an executable.
-
meminitial
@Parameter(property="maven.compiler.meminitial") private java.lang.String meminitial
Initial size, in megabytes, of the memory allocation pool, ex. "64", "64m" iffork
is set totrue
.- Since:
- 2.0.1
-
maxmem
@Parameter(property="maven.compiler.maxmem") private java.lang.String maxmem
Sets the maximum size, in megabytes, of the memory allocation pool, ex. "128", "128m" iffork
is set totrue
.- Since:
- 2.0.1
-
executable
@Parameter(property="maven.compiler.executable") private java.lang.String executable
Sets the executable of the compiler to use whenfork
istrue
.
-
proc
@Parameter private java.lang.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 java.lang.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 java.util.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. 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
@Parameter @Deprecated protected java.util.Map<java.lang.String,java.lang.String> compilerArguments
Deprecated.usecompilerArgs
instead.Sets the arguments to be passed to the compiler (prepending a dash) if
fork
is set totrue
.This is because the list of valid arguments passed to a Java compiler varies based on the compiler version.
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 java.util.List<java.lang.String> compilerArgs
Sets the arguments to be passed to the compiler if
fork
is set totrue
. 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 java.lang.String compilerArgument
Sets the unformatted single argument string to be passed to the compiler if
fork
is set totrue
. 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.
-
outputFileName
@Parameter private java.lang.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 java.lang.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
, 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
@Parameter private java.util.Map<java.lang.String,java.lang.String> jdkToolchain
Specify the requirements for this jdk toolchain. This overrules the toolchain selected by the maven-toolchain-plugin.
note: requires at least Maven 3.3.1- Since:
- 3.6
-
basedir
@Parameter(defaultValue="${basedir}", required=true, readonly=true) private java.io.File basedir
The directory to run the compiler from if fork is true.
-
buildDirectory
@Parameter(defaultValue="${project.build.directory}", required=true, readonly=true) private java.io.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 java.lang.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 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 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 private java.util.List<java.lang.String> fileExtensions
file extensions to check timestamp for incremental build default contains only.class
- Since:
- 3.1
-
useIncrementalCompilation
@Parameter(defaultValue="true", property="maven.compiler.useIncrementalCompilation") private boolean useIncrementalCompilation
to enable/disable incrementation compilation feature- Since:
- 3.1
-
repositorySystem
@Component private org.apache.maven.repository.RepositorySystem repositorySystem
Resolves the artifacts needed.
-
artifactHandlerManager
@Component private org.apache.maven.artifact.handler.manager.ArtifactHandlerManager artifactHandlerManager
Artifact handler manager.
-
resolutionErrorHandler
@Component private org.apache.maven.artifact.resolver.ResolutionErrorHandler resolutionErrorHandler
Throws an exception on artifact resolution errors.
-
targetOrReleaseSet
private boolean targetOrReleaseSet
-
-
Method Detail
-
getSourceInclusionScanner
protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner getSourceInclusionScanner(int staleMillis)
-
getSourceInclusionScanner
protected abstract org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner getSourceInclusionScanner(java.lang.String inputFileEnding)
-
getClasspathElements
protected abstract java.util.List<java.lang.String> getClasspathElements()
-
getModulepathElements
protected abstract java.util.List<java.lang.String> getModulepathElements()
-
getPathElements
protected abstract java.util.Map<java.lang.String,org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor> getPathElements()
-
getCompileSourceRoots
protected abstract java.util.List<java.lang.String> getCompileSourceRoots()
-
preparePaths
protected abstract void preparePaths(java.util.Set<java.io.File> sourceFiles)
-
getOutputDirectory
protected abstract java.io.File getOutputDirectory()
-
getSource
protected abstract java.lang.String getSource()
-
getTarget
protected abstract java.lang.String getTarget()
-
getRelease
protected abstract java.lang.String getRelease()
-
getCompilerArgument
protected abstract java.lang.String getCompilerArgument()
-
getCompilerArguments
protected abstract java.util.Map<java.lang.String,java.lang.String> getCompilerArguments()
-
getGeneratedSourcesDirectory
protected abstract java.io.File getGeneratedSourcesDirectory()
-
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.MojoExecutionException
CompilationFailureException
-
isTestCompile
protected boolean isTestCompile()
-
convertToCompilerResult
protected org.codehaus.plexus.compiler.CompilerResult convertToCompilerResult(java.util.List<org.codehaus.plexus.compiler.CompilerError> compilerErrors)
-
getCompileSources
private java.util.Set<java.io.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
-
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:
true
if at least a single source file is newer than it's class file- Throws:
org.codehaus.plexus.compiler.CompilerException
org.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
protected java.util.Date getBuildStartTime()
-
getMemoryValue
private java.lang.String getMemoryValue(java.lang.String setting)
-
getToolchain
protected final org.apache.maven.toolchain.Toolchain getToolchain()
-
isDigits
private boolean isDigits(java.lang.String string)
-
computeStaleSources
private java.util.Set<java.io.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 java.util.List<java.lang.String> removeEmptyCompileSourceRoots(java.util.List<java.lang.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 buid-started timestamp, then we catched a file which got changed during this build.- Returns:
true
if at least one single dependency has changed.
-
hasNewFile
private boolean hasNewFile(java.io.File classPathEntry, java.util.Date buildStartTime)
- Parameters:
classPathEntry
- entry to checkbuildStartTime
- time build start- Returns:
- if any changes occurred
-
resolveProcessorPathEntries
private java.util.List<java.lang.String> resolveProcessorPathEntries() throws org.apache.maven.plugin.MojoExecutionException
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
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 java.lang.String getMavenCompilerPluginVersion()
-
setTarget
public void setTarget(java.lang.String target)
-
setRelease
public void setRelease(java.lang.String release)
-
-