fileExtensions;
-
- /**
- * 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
- */
- @Parameter( defaultValue = "true", property = "maven.compiler.useIncrementalCompilation" )
- private boolean useIncrementalCompilation = true;
-
/**
* 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
@@ -871,130 +833,31 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
compilerConfiguration.setForceJavacCompilerUse( forceJavacCompilerUse );
- boolean canUpdateTarget;
-
- IncrementalBuildHelper incrementalBuildHelper = new IncrementalBuildHelper( mojoExecution, session );
-
final Set sources;
- IncrementalBuildHelperRequest incrementalBuildHelperRequest = null;
-
- if ( useIncrementalCompilation )
+ try
{
- getLog().debug( "useIncrementalCompilation enabled" );
- try
- {
- canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
+ // MCOMPILER-366: if sources contain the module-descriptor it must be used to define the modulepath
+ sources = getCompileSources( compiler, compilerConfiguration );
- sources = getCompileSources( compiler, compilerConfiguration );
-
- preparePaths( sources );
-
- incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
-
- DirectoryScanResult dsr = computeInputFileTreeChanges( incrementalBuildHelper, sources );
-
- boolean idk = compiler.getCompilerOutputStyle()
- .equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES ) && !canUpdateTarget;
- boolean dependencyChanged = isDependencyChanged();
- boolean sourceChanged = isSourceChanged( compilerConfiguration, compiler );
- boolean inputFileTreeChanged = hasInputFileTreeChanged( dsr );
- // CHECKSTYLE_OFF: LineLength
- if ( idk
- || dependencyChanged
- || sourceChanged
- || inputFileTreeChanged )
- // CHECKSTYLE_ON: LineLength
- {
- String cause = idk ? "idk" : ( dependencyChanged ? "dependency"
- : ( sourceChanged ? "source" : "input tree" ) );
- getLog().info( "Changes detected - recompiling the module! :" + cause );
- if ( showCompilationChanges )
- {
- for ( String fileAdded : dsr.getFilesAdded() )
- {
- getLog().info( "\t+ " + fileAdded );
- }
- for ( String fileRemoved : dsr.getFilesRemoved() )
- {
- getLog().info( "\t- " + fileRemoved );
- }
- }
-
- compilerConfiguration.setSourceFiles( sources );
- }
- else
- {
- getLog().info( "Nothing to compile - all classes are up to date" );
-
- return;
- }
- }
- catch ( CompilerException e )
+ if ( getLog().isDebugEnabled() )
{
- throw new MojoExecutionException( "Error while computing stale sources.", e );
- }
- }
- else
- {
- getLog().debug( "useIncrementalCompilation disabled" );
-
- Set 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().debug( "#sources: " + sources.size() );
+ for ( File file : sources )
{
- getLog().info( "RESCANNING!" );
- // TODO: This second scan for source files is sub-optimal
- String inputFileEnding = compiler.getInputFileEnding( compilerConfiguration );
-
- staleSources = computeStaleSources( compilerConfiguration, compiler,
- getSourceInclusionScanner( inputFileEnding ) );
+ getLog().debug( file.getPath() );
}
-
}
- 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;
- }
-
- compilerConfiguration.setSourceFiles( staleSources );
-
- try
- {
- // MCOMPILER-366: if sources contain the module-descriptor it must be used to define the modulepath
- sources = getCompileSources( compiler, compilerConfiguration );
+ preparePaths( sources );
+ }
+ catch ( CompilerException e )
+ {
+ throw new MojoExecutionException( "Error while computing stale sources.", e );
+ }
- if ( getLog().isDebugEnabled() )
- {
- getLog().debug( "#sources: " + sources.size() );
- for ( File file : sources )
- {
- getLog().debug( file.getPath() );
- }
- }
+ compilerConfiguration.setSourceFiles( sources );
- preparePaths( sources );
- }
- catch ( CompilerException e )
- {
- throw new MojoExecutionException( "Error while computing stale sources.", e );
- }
- }
-
// Dividing pathElements of classPath and modulePath is based on sourceFiles
compilerConfiguration.setClasspathEntries( getClasspathElements() );
@@ -1044,6 +907,29 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
}
}
}
+
+ if ( Files.isDirectory( getOutputDirectory().getAbsoluteFile().toPath() ) )
+ {
+ try ( Stream dirs = Files.list( getOutputDirectory().getAbsoluteFile().toPath() )
+ .filter( p -> !"META-INF".equals( p.getFileName().toString() ) ) )
+ {
+ dirs.forEach( p ->
+ {
+ try
+ {
+ FileUtils.deleteDirectory( p.toFile() );
+ }
+ catch ( IOException e )
+ {
+ throw new UncheckedIOException( e );
+ }
+ } );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Could not zap output directory", e );
+ }
+ }
// ----------------------------------------------------------------------
// Dump configuration
@@ -1243,16 +1129,6 @@ else if ( !values[0].equals( descriptor.name() ) )
CompilerResult compilerResult;
-
- if ( useIncrementalCompilation )
- {
- incrementalBuildHelperRequest.outputDirectory( getOutputDirectory() );
-
- incrementalBuildHelper.beforeRebuildExecution( incrementalBuildHelperRequest );
-
- getLog().debug( "incrementalBuildHelper#beforeRebuildExecution" );
- }
-
try
{
try
@@ -1286,21 +1162,6 @@ else if ( !values[0].equals( descriptor.name() ) )
}
}
- if ( useIncrementalCompilation )
- {
- if ( incrementalBuildHelperRequest.getOutputDirectory().exists() )
- {
- getLog().debug( "incrementalBuildHelper#afterRebuildExecution" );
- // now scan the same directory again and create a diff
- incrementalBuildHelper.afterRebuildExecution( incrementalBuildHelperRequest );
- }
- else
- {
- getLog().debug(
- "skip incrementalBuildHelper#afterRebuildExecution as the output directory doesn't exist" );
- }
- }
-
List warnings = new ArrayList<>();
List errors = new ArrayList<>();
List others = new ArrayList<>();
@@ -1544,35 +1405,6 @@ private Set getCompileSources( Compiler compiler, CompilerConfiguration co
protected abstract Set getExcludes();
- /**
- * @param compilerConfiguration
- * @param compiler
- * @return true
if at least a single source file is newer than it's class file
- */
- private boolean isSourceChanged( CompilerConfiguration compilerConfiguration, Compiler compiler )
- throws CompilerException, MojoExecutionException
- {
- Set staleSources =
- computeStaleSources( compilerConfiguration, compiler, getSourceInclusionScanner( staleMillis ) );
-
- if ( getLog().isDebugEnabled() || showCompilationChanges )
- {
- for ( File f : staleSources )
- {
- if ( showCompilationChanges )
- {
- getLog().info( "Stale source detected: " + f.getAbsolutePath() );
- }
- else
- {
- getLog().debug( "Stale source detected: " + f.getAbsolutePath() );
- }
- }
- }
- return !staleSources.isEmpty();
- }
-
-
/**
* try to get thread count if a Maven 3 build, using reflection as the plugin must not be maven3 api dependent
*
@@ -1590,7 +1422,6 @@ protected Date getBuildStartTime()
return buildStartTime == null ? new Date() : buildStartTime;
}
-
private String getMemoryValue( String setting )
{
String value = null;
@@ -1660,50 +1491,6 @@ private boolean isDigits( String string )
return true;
}
- private Set computeStaleSources( CompilerConfiguration compilerConfiguration, Compiler compiler,
- SourceInclusionScanner scanner )
- throws MojoExecutionException, CompilerException
- {
- SourceMapping mapping = getSourceMapping( compilerConfiguration, compiler );
-
- File outputDirectory;
- CompilerOutputStyle outputStyle = compiler.getCompilerOutputStyle();
- if ( outputStyle == CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES )
- {
- outputDirectory = buildDirectory;
- }
- else
- {
- outputDirectory = getOutputDirectory();
- }
-
- scanner.addSourceMapping( mapping );
-
- Set staleSources = new HashSet<>();
-
- for ( String sourceRoot : getCompileSourceRoots() )
- {
- File rootFile = new File( sourceRoot );
-
- if ( !rootFile.isDirectory() )
- {
- continue;
- }
-
- try
- {
- staleSources.addAll( scanner.getIncludedSources( rootFile, outputDirectory ) );
- }
- catch ( InclusionScanException e )
- {
- throw new MojoExecutionException(
- "Error scanning source root: \'" + sourceRoot + "\' for stale files to recompile.", e );
- }
- }
-
- return staleSources;
- }
-
private SourceMapping getSourceMapping( CompilerConfiguration compilerConfiguration, Compiler compiler )
throws CompilerException, MojoExecutionException
{
@@ -1749,88 +1536,6 @@ private static List removeEmptyCompileSourceRoots( List compileS
return newCompileSourceRootsList;
}
- /**
- * 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.
- *
- * @return true
if at least one single dependency has changed.
- */
- protected boolean isDependencyChanged()
- {
- if ( session == null )
- {
- // we just cannot determine it, so don't do anything beside logging
- getLog().info( "Cannot determine build start date, skipping incremental build detection." );
- return false;
- }
-
- if ( fileExtensions == null || fileExtensions.isEmpty() )
- {
- fileExtensions = Collections.unmodifiableList( Arrays.asList( "class", "jar" ) );
- }
-
- Date buildStartTime = getBuildStartTime();
-
- List pathElements = new ArrayList<>();
- pathElements.addAll( getClasspathElements() );
- pathElements.addAll( getModulepathElements() );
-
- for ( String pathElement : pathElements )
- {
- File artifactPath = new File( pathElement );
- if ( artifactPath.isDirectory() || artifactPath.isFile() )
- {
- if ( hasNewFile( artifactPath, buildStartTime ) )
- {
- if ( showCompilationChanges )
- {
- getLog().info( "New dependency detected: " + artifactPath.getAbsolutePath() );
- }
- else
- {
- getLog().debug( "New dependency detected: " + artifactPath.getAbsolutePath() );
- }
- return true;
- }
- }
- }
-
- // obviously there was no new file detected.
- return false;
- }
-
- /**
- * @param classPathEntry entry to check
- * @param buildStartTime time build start
- * @return if any changes occurred
- */
- private boolean hasNewFile( File classPathEntry, Date buildStartTime )
- {
- if ( !classPathEntry.exists() )
- {
- return false;
- }
-
- if ( classPathEntry.isFile() )
- {
- return classPathEntry.lastModified() >= buildStartTime.getTime()
- && fileExtensions.contains( FileUtils.getExtension( classPathEntry.getName() ) );
- }
-
- File[] children = classPathEntry.listFiles();
-
- for ( File child : children )
- {
- if ( hasNewFile( child, buildStartTime ) )
- {
- return true;
- }
- }
-
- return false;
- }
-
private List resolveProcessorPathEntries()
throws MojoExecutionException
{
@@ -1939,47 +1644,6 @@ private String getMavenCompilerPluginVersion()
return pomProperties.getProperty( "version" );
}
- private DirectoryScanResult computeInputFileTreeChanges( IncrementalBuildHelper ibh, Set inputFiles )
- throws MojoExecutionException
- {
- File mojoConfigBase = ibh.getMojoStatusDirectory();
- File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME );
-
- String[] oldInputFiles = new String[0];
-
- if ( mojoConfigFile.exists() )
- {
- try
- {
- oldInputFiles = FileUtils.fileReadArray( mojoConfigFile );
- }
- catch ( IOException e )
- {
- throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e );
- }
- }
-
- String[] inputFileNames = inputFiles.stream().map( File::getAbsolutePath ).toArray( String[]::new );
-
- DirectoryScanResult dsr = DirectoryScanner.diffFiles( oldInputFiles, inputFileNames );
-
- try
- {
- FileUtils.fileWriteArray( mojoConfigFile, inputFileNames );
- }
- catch ( IOException e )
- {
- throw new MojoExecutionException( "Error while storing the mojo status", e );
- }
-
- return dsr;
- }
-
- private boolean hasInputFileTreeChanged( DirectoryScanResult dsr )
- {
- return ( dsr.getFilesAdded().length > 0 || dsr.getFilesRemoved().length > 0 );
- }
-
public void setTarget( String target )
{
this.target = target;
diff --git a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
index 6c12627d..3e748b81 100644
--- a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
+++ b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
@@ -388,7 +388,6 @@ private CompilerMojo getCompilerMojo( String pomXml )
setVariableValueToObject( mojo, "compilePath", Collections.EMPTY_LIST );
setVariableValueToObject( mojo, "session", getMockMavenSession() );
setVariableValueToObject( mojo, "project", getMockMavenProject() );
- setVariableValueToObject( mojo, "mojoExecution", getMockMojoExecution() );
setVariableValueToObject( mojo, "source", AbstractCompilerMojo.DEFAULT_SOURCE );
setVariableValueToObject( mojo, "target", AbstractCompilerMojo.DEFAULT_TARGET );
@@ -444,7 +443,6 @@ private TestCompilerMojo getTestCompilerMojo( CompilerMojo compilerMojo, String
setVariableValueToObject( mojo, "project", project );
setVariableValueToObject( mojo, "testPath", testClasspathList );
setVariableValueToObject( mojo, "session", getMockMavenSession() );
- setVariableValueToObject( mojo, "mojoExecution", getMockMojoExecution() );
setVariableValueToObject( mojo, "source", AbstractCompilerMojo.DEFAULT_SOURCE );
setVariableValueToObject( mojo, "target", AbstractCompilerMojo.DEFAULT_TARGET );
@@ -469,18 +467,4 @@ private MavenSession getMockMavenSession()
when( session.getCurrentProject() ).thenReturn( getMockMavenProject() );
return session;
}
-
- private MojoExecution getMockMojoExecution()
- {
- MojoDescriptor md = new MojoDescriptor();
- md.setGoal( "compile" );
-
- MojoExecution me = new MojoExecution( md );
-
- PluginDescriptor pd = new PluginDescriptor();
- pd.setArtifactId( "maven-compiler-plugin" );
- md.setPluginDescriptor( pd );
-
- return me;
- }
}