Skip to content

Commit 413dbb7

Browse files
olamyledoyen
andauthored
MCOMPILER 499 display recompilation causes (#143)
* Display recompilation causes Signed-off-by: Olivier Lamy <olamy@apache.org> Co-authored-by: Loïc Ledoyen <loic.ledoyen@mirakl.com>
1 parent a522b31 commit 413dbb7

File tree

1 file changed

+89
-8
lines changed

1 file changed

+89
-8
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import org.apache.maven.shared.incremental.IncrementalBuildHelperRequest;
6262
import org.apache.maven.shared.utils.ReaderFactory;
6363
import org.apache.maven.shared.utils.StringUtils;
64+
import org.apache.maven.shared.utils.io.DirectoryScanResult;
65+
import org.apache.maven.shared.utils.io.DirectoryScanner;
6466
import org.apache.maven.shared.utils.io.FileUtils;
6567
import org.apache.maven.shared.utils.logging.MessageBuilder;
6668
import org.apache.maven.shared.utils.logging.MessageUtils;
@@ -101,6 +103,8 @@ public abstract class AbstractCompilerMojo
101103
{
102104
protected static final String PS = System.getProperty( "path.separator" );
103105

106+
private static final String INPUT_FILES_LST_FILENAME = "inputFiles.lst";
107+
104108
static final String DEFAULT_SOURCE = "1.7";
105109

106110
static final String DEFAULT_TARGET = "1.7";
@@ -561,6 +565,8 @@ public abstract class AbstractCompilerMojo
561565
@Parameter( defaultValue = "true", property = "maven.compiler.createMissingPackageInfoClass" )
562566
private boolean createMissingPackageInfoClass = true;
563567

568+
@Parameter( defaultValue = "false", property = "maven.compiler.showCompilationChanges" )
569+
private boolean showCompilationChanges = false;
564570
/**
565571
* Resolves the artifacts needed.
566572
*/
@@ -876,14 +882,34 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
876882

877883
incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
878884

885+
DirectoryScanResult dsr = computeInputFileTreeChanges( incrementalBuildHelper, sources );
886+
887+
boolean idk = compiler.getCompilerOutputStyle()
888+
.equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES ) && !canUpdateTarget;
889+
boolean dependencyChanged = isDependencyChanged();
890+
boolean sourceChanged = isSourceChanged( compilerConfiguration, compiler );
891+
boolean inputFileTreeChanged = hasInputFileTreeChanged( dsr );
879892
// CHECKSTYLE_OFF: LineLength
880-
if ( ( compiler.getCompilerOutputStyle().equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES ) && !canUpdateTarget )
881-
|| isDependencyChanged()
882-
|| isSourceChanged( compilerConfiguration, compiler )
883-
|| incrementalBuildHelper.inputFileTreeChanged( incrementalBuildHelperRequest ) )
893+
if ( idk
894+
|| dependencyChanged
895+
|| sourceChanged
896+
|| inputFileTreeChanged )
884897
// CHECKSTYLE_ON: LineLength
885898
{
886-
getLog().info( "Changes detected - recompiling the module!" );
899+
String cause = idk ? "idk" : ( dependencyChanged ? "dependency"
900+
: ( sourceChanged ? "source" : "input tree" ) );
901+
getLog().info( "Changes detected - recompiling the module! :" + cause );
902+
if ( showCompilationChanges )
903+
{
904+
for ( String fileAdded : dsr.getFilesAdded() )
905+
{
906+
getLog().info( "\t+ " + fileAdded );
907+
}
908+
for ( String fileRemoved : dsr.getFilesRemoved() )
909+
{
910+
getLog().info( "\t- " + fileRemoved );
911+
}
912+
}
887913

888914
compilerConfiguration.setSourceFiles( sources );
889915
}
@@ -1519,11 +1545,18 @@ private boolean isSourceChanged( CompilerConfiguration compilerConfiguration, Co
15191545
Set<File> staleSources =
15201546
computeStaleSources( compilerConfiguration, compiler, getSourceInclusionScanner( staleMillis ) );
15211547

1522-
if ( getLog().isDebugEnabled() )
1548+
if ( getLog().isDebugEnabled() || showCompilationChanges )
15231549
{
15241550
for ( File f : staleSources )
15251551
{
1526-
getLog().debug( "Stale source detected: " + f.getAbsolutePath() );
1552+
if ( showCompilationChanges )
1553+
{
1554+
getLog().info( "Stale source detected: " + f.getAbsolutePath() );
1555+
}
1556+
else
1557+
{
1558+
getLog().debug( "Stale source detected: " + f.getAbsolutePath() );
1559+
}
15271560
}
15281561
}
15291562
return !staleSources.isEmpty();
@@ -1740,7 +1773,14 @@ protected boolean isDependencyChanged()
17401773
{
17411774
if ( hasNewFile( artifactPath, buildStartTime ) )
17421775
{
1743-
getLog().debug( "New dependency detected: " + artifactPath.getAbsolutePath() );
1776+
if ( showCompilationChanges )
1777+
{
1778+
getLog().info( "New dependency detected: " + artifactPath.getAbsolutePath() );
1779+
}
1780+
else
1781+
{
1782+
getLog().debug( "New dependency detected: " + artifactPath.getAbsolutePath() );
1783+
}
17441784
return true;
17451785
}
17461786
}
@@ -1889,6 +1929,47 @@ private String getMavenCompilerPluginVersion()
18891929
return pomProperties.getProperty( "version" );
18901930
}
18911931

1932+
private DirectoryScanResult computeInputFileTreeChanges( IncrementalBuildHelper ibh, Set<File> inputFiles )
1933+
throws MojoExecutionException
1934+
{
1935+
File mojoConfigBase = ibh.getMojoStatusDirectory();
1936+
File mojoConfigFile = new File( mojoConfigBase, INPUT_FILES_LST_FILENAME );
1937+
1938+
String[] oldInputFiles = new String[0];
1939+
1940+
if ( mojoConfigFile.exists() )
1941+
{
1942+
try
1943+
{
1944+
oldInputFiles = FileUtils.fileReadArray( mojoConfigFile );
1945+
}
1946+
catch ( IOException e )
1947+
{
1948+
throw new MojoExecutionException( "Error reading old mojo status " + mojoConfigFile, e );
1949+
}
1950+
}
1951+
1952+
String[] inputFileNames = inputFiles.stream().map( File::getAbsolutePath ).toArray( String[]::new );
1953+
1954+
DirectoryScanResult dsr = DirectoryScanner.diffFiles( oldInputFiles, inputFileNames );
1955+
1956+
try
1957+
{
1958+
FileUtils.fileWriteArray( mojoConfigFile, inputFileNames );
1959+
}
1960+
catch ( IOException e )
1961+
{
1962+
throw new MojoExecutionException( "Error while storing the mojo status", e );
1963+
}
1964+
1965+
return dsr;
1966+
}
1967+
1968+
private boolean hasInputFileTreeChanged( DirectoryScanResult dsr )
1969+
{
1970+
return ( dsr.getFilesAdded().length > 0 || dsr.getFilesRemoved().length > 0 );
1971+
}
1972+
18921973
public void setTarget( String target )
18931974
{
18941975
this.target = target;

0 commit comments

Comments
 (0)