60
60
import org .eclipse .text .edits .MalformedTreeException ;
61
61
import org .xml .sax .SAXException ;
62
62
63
- import com .google .common .base .Strings ;
64
63
import com .google .common .hash .Hashing ;
65
64
66
65
import net .revelc .code .formatter .css .CssFormatter ;
@@ -262,6 +261,14 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
262
261
@ Parameter (defaultValue = "formatter-maven-plugin/ph-css/css.properties" , property = "configcssfile" , required = true )
263
262
private String configCssFile ;
264
263
264
+ /**
265
+ * Whether the formatting cache is skipped.
266
+ *
267
+ * @since 2.23.0
268
+ */
269
+ @ Parameter (defaultValue = "false" , property = "formatter.cache.skip" )
270
+ private boolean skipFormattingCache ;
271
+
265
272
/**
266
273
* Whether the java formatting is skipped.
267
274
*/
@@ -462,9 +469,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
462
469
this .formatFile (file , rc , hashCache , basedirPath );
463
470
} else {
464
471
rc .readOnlyCount ++;
472
+ this .getLog ().warn ("File " + file + " is read only" );
465
473
}
466
474
} else {
467
475
rc .failCount ++;
476
+ this .getLog ().error ("File " + file + " does not exist" );
468
477
}
469
478
}
470
479
@@ -478,9 +487,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
478
487
final var elapsed = TimeUtil .printDuration (duration );
479
488
480
489
final String results = String .format (
481
- "Processed %d files in %s (Formatted: %d, Unchanged: %d, Failed: %d, Readonly: %d)" , numberOfFiles ,
482
- elapsed , rc .successCount , rc .skippedCount , rc .failCount , rc .readOnlyCount );
483
- this .getLog ().info (results );
490
+ "Processed %d files in %s (Formatted: %d, Skipped: %d, Unchanged: %d, Failed: %d, Readonly: %d)" ,
491
+ numberOfFiles , elapsed , rc .successCount , rc .skippedCount , rc .unchangedCount , rc .failCount ,
492
+ rc .readOnlyCount );
493
+
494
+ if (rc .failCount > 0 )
495
+ this .getLog ().error (results );
496
+ else if (rc .readOnlyCount > 0 )
497
+ this .getLog ().warn (results );
498
+ else
499
+ this .getLog ().info (results );
484
500
}
485
501
}
486
502
@@ -678,7 +694,7 @@ private void formatFile(final File file, final ResultCollector rc, final Propert
678
694
this .doFormatFile (file , rc , hashCache , basedirPath , false );
679
695
} catch (IOException | MalformedTreeException | BadLocationException e ) {
680
696
rc .failCount ++;
681
- this .getLog ().warn (e );
697
+ this .getLog ().error (e );
682
698
}
683
699
}
684
700
@@ -711,14 +727,14 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
711
727
final var log = this .getLog ();
712
728
log .debug ("Processing file: " + file );
713
729
final var originalCode = this .readFileAsString (file );
714
- final var originalHash = this .sha512hash (originalCode );
730
+ final var originalHash = this .sha512hash (originalCode + this . javaFormatter . hashCode () );
715
731
716
732
final var canonicalPath = file .getCanonicalPath ();
717
733
final var path = canonicalPath .substring (basedirPath .length ());
718
734
final var cachedHash = hashCache .getProperty (path );
719
- if (cachedHash != null && cachedHash .equals (originalHash )) {
735
+ if (! skipFormattingCache && cachedHash != null && cachedHash .equals (originalHash )) {
720
736
rc .skippedCount ++;
721
- log .debug ("File is already formatted." );
737
+ log .debug ("Cache hit: file is already formatted." );
722
738
return ;
723
739
}
724
740
@@ -786,7 +802,7 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
786
802
787
803
// Process the result type
788
804
if (Result .SKIPPED .equals (result )) {
789
- rc .skippedCount ++;
805
+ rc .unchangedCount ++;
790
806
} else if (Result .SUCCESS .equals (result )) {
791
807
rc .successCount ++;
792
808
} else if (Result .FAIL .equals (result )) {
@@ -799,14 +815,14 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
799
815
if (Result .SKIPPED .equals (result )) {
800
816
formattedHash = originalHash ;
801
817
} else {
802
- formattedHash = this .sha512hash (Strings . nullToEmpty ( formattedCode ));
818
+ formattedHash = this .sha512hash (formattedCode + this . javaFormatter . hashCode ( ));
803
819
}
804
820
hashCache .setProperty (path , formattedHash );
805
821
this .hashCacheWritten = true ;
806
822
807
823
// If we had determined to skip write, do so now after cache was written
808
824
if (Result .SKIPPED .equals (result )) {
809
- log .debug ("File is already formatted. Writing to cache only." );
825
+ log .debug ("File is already formatted. Writing to cache only." );
810
826
return ;
811
827
}
812
828
@@ -1071,9 +1087,12 @@ static class ResultCollector {
1071
1087
/** The fail count. */
1072
1088
int failCount ;
1073
1089
1074
- /** The skipped count. */
1090
+ /** The skipped count is incremented for cached files that haven't changed since being cached . */
1075
1091
int skippedCount ;
1076
1092
1093
+ /** The unchanged count is incremented on cache misses when a file remains unchanged after formatting. */
1094
+ int unchangedCount ;
1095
+
1077
1096
/** The read only count. */
1078
1097
int readOnlyCount ;
1079
1098
}
0 commit comments