IncrementalBuildHelper incrementalBuildHelper = new IncrementalBuildHelper( mojoExecution, mavenSession );
Set<File> sources = null;
IncrementalBuildHelperRequest incrementalBuildHelperRequest = null;
if ( useIncrementalCompilation )
{
getLog().debug( "useIncrementalCompilation enabled" );
try
{
canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
sources = getCompileSources( compiler, compilerConfiguration );
incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
if ( ( compiler.getCompilerOutputStyle().equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES )
&& !canUpdateTarget ) || isDependencyChanged() || isSourceChanged( compilerConfiguration, compiler )
|| incrementalBuildHelper.inputFileTreeChanged( incrementalBuildHelperRequest ) )
{
getLog().info( "Changes detected - recompiling the module!" );
compilerConfiguration.setSourceFiles( sources );
}
else
{
getLog().info( "Nothing to compile - all classes are up to date" );
return;
}
}
catch ( CompilerException e )
{
throw new MojoExecutionException( "Error while computing stale sources.", e );
}
}
else
{
getLog().debug( "useIncrementalCompilation disabled" );
Set<File> staleSources;
try
{
staleSources =
computeStaleSources( compilerConfiguration, compiler, getSourceInclusionScanner( staleMillis ) );
canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
if ( compiler.getCompilerOutputStyle().equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES )
&& !canUpdateTarget )
{
getLog().info( "RESCANNING!" );
// TODO: This second scan for source files is sub-optimal
String inputFileEnding = compiler.getInputFileEnding( compilerConfiguration );
sources = computeStaleSources( compilerConfiguration, compiler,
getSourceInclusionScanner( inputFileEnding ) );
compilerConfiguration.setSourceFiles( sources );
}
else
{
compilerConfiguration.setSourceFiles( staleSources );
}
}
catch ( CompilerException e )
{
throw new MojoExecutionException( "Error while computing stale sources.", e );
}
if ( staleSources.isEmpty() )
{
getLog().info( "Nothing to compile - all classes are up to date" );
return;
}
}
// ----------------------------------------------------------------------
// Dump configuration
// ----------------------------------------------------------------------
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Classpath:" );
for ( String s : getClasspathElements() )
{
getLog().debug( " " + s );
}
getLog().debug( "Source roots:" );
for ( String root : getCompileSourceRoots() )
{
getLog().debug( " " + root );
}
try
{
if ( fork )
{
if ( compilerConfiguration.getExecutable() != null )
{
getLog().debug( "Excutable: " );
getLog().debug( " " + compilerConfiguration.getExecutable() );
}
}
String[] cl = compiler.createCommandLine( compilerConfiguration );
if ( cl != null && cl.length > 0 )
{
StringBuilder sb = new StringBuilder();
sb.append( cl[0] );
for ( int i = 1; i < cl.length; i++ )
{
sb.append( " " );
sb.append( cl[i] );
}
getLog().debug( "Command line options:" );
getLog().debug( sb );
}
}
catch ( CompilerException ce )
{
getLog().debug( ce );
}
}
// ----------------------------------------------------------------------
// Compile!
// ----------------------------------------------------------------------
if ( StringUtils.isEmpty( compilerConfiguration.getSourceEncoding() ) )
{
getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
+ ", i.e. build is platform dependent!" );
}
CompilerResult compilerResult;
if ( useIncrementalCompilation )
{
incrementalBuildHelperRequest.outputDirectory( getOutputDirectory() );
incrementalBuildHelper.beforeRebuildExecution( incrementalBuildHelperRequest );
getLog().debug( "incrementalBuildHelper#beforeRebuildExecution" );
}
try
{
try
{
compilerResult = compiler.performCompile( compilerConfiguration );
}
catch ( CompilerNotImplementedException cnie )
{
List<CompilerError> messages = compiler.compile( compilerConfiguration );
compilerResult = convertToCompilerResult( messages );
}
}
catch ( Exception e )
{
// TODO: don't catch Exception
throw new MojoExecutionException( "Fatal error compiling", e );
}
if ( useIncrementalCompilation )
{
if ( incrementalBuildHelperRequest.getOutputDirectory().exists() )
{
getLog().debug( "incrementalBuildHelper#afterRebuildExecution" );
// now scan the same directory again and create a diff
incrementalBuildHelper.afterRebuildExecution( incrementalBuildHelperRequest );
}