From 3ab93f97ca82fe2468f31934cc42f0054eaa0a4f Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 17 Apr 2023 14:08:05 +0200 Subject: [PATCH] Make sure files are not overwritten with the same content --- pom.xml | 12 ++-- .../remote/ProcessRemoteResourcesMojo.java | 66 ++----------------- 2 files changed, 13 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index 7c58702..38e1b10 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ under the License. 2022-07-17T16:33:17Z - 3.2.0 + 3.3.2-SNAPSHOT @@ -149,13 +149,13 @@ under the License. - - - commons-io - commons-io - 2.6 + org.codehaus.plexus + plexus-utils + 3.5.1 + + org.apache.velocity velocity diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java index cedeb5d..c4b53d0 100644 --- a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java +++ b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java @@ -19,7 +19,6 @@ * under the License. */ -import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -54,7 +53,6 @@ import java.util.Set; import java.util.TreeMap; -import org.apache.commons.io.output.DeferredFileOutputStream; import org.apache.maven.ProjectDependenciesResolver; import org.apache.maven.archiver.MavenArchiver; import org.apache.maven.artifact.Artifact; @@ -92,6 +90,7 @@ import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter; import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter; import org.apache.maven.shared.artifact.filter.collection.ScopeFilter; +import org.apache.maven.shared.filtering.FilteringUtils; import org.apache.maven.shared.filtering.MavenFileFilter; import org.apache.maven.shared.filtering.MavenFileFilterRequest; import org.apache.maven.shared.filtering.MavenFilteringException; @@ -116,6 +115,7 @@ import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.plexus.util.io.CachingOutputStream; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -319,8 +319,10 @@ public class ProcessRemoteResourcesMojo * typically rely on the modification date. * * @since 1.6 + * @deprecated unused, a better mechanism is in place */ @Parameter( defaultValue = "5242880" ) + @Deprecated protected int velocityFilterInMemoryThreshold = 5 * 1024 * 1024; /** @@ -821,8 +823,7 @@ protected boolean copyResourceIfExists( File file, String relFileName, VelocityC { if ( source == templateSource ) { - try ( DeferredFileOutputStream os = - new DeferredFileOutputStream( velocityFilterInMemoryThreshold, file ) ) + try ( OutputStream os = new CachingOutputStream( file ) ) { try ( Reader reader = getReader( source ); Writer writer = getWriter( os ) ) { @@ -832,7 +833,6 @@ protected boolean copyResourceIfExists( File file, String relFileName, VelocityC { throw new MojoExecutionException( "Error rendering velocity resource: " + source, e ); } - fileWriteIfDiffers( os ); } } else if ( resource.isFiltering() ) @@ -851,7 +851,7 @@ else if ( resource.isFiltering() ) } else { - FileUtils.copyFile( source, file ); + FilteringUtils.copyFile( source, file, null, null ); } // exclude the original (so eclipse doesn't complain about duplicate resources) @@ -888,56 +888,6 @@ private Writer getWriter( OutputStream os ) throws IOException } } - /** - * If the transformation result fits in memory and the destination file already exists - * then both are compared. - *

If destination file is byte-by-byte equal, then it is not overwritten. - * This improves subsequent compilation times since upstream plugins property see that - * the resource was not modified. - *

Note: the method should be called after {@link org.apache.commons.io.output.DeferredFileOutputStream#close} - * - * @param outStream Deferred stream - * @throws IOException - */ - private void fileWriteIfDiffers( DeferredFileOutputStream outStream ) - throws IOException - { - File file = outStream.getFile(); - if ( outStream.isThresholdExceeded() ) - { - getLog().info( "File " + file + " was overwritten due to content limit threshold " - + outStream.getThreshold() + " reached" ); - return; - } - boolean needOverwrite = true; - - if ( file.exists() ) - { - try ( InputStream is = Files.newInputStream( file.toPath() ); - InputStream newContents = new ByteArrayInputStream( outStream.getData() ) ) - { - needOverwrite = !IOUtil.contentEquals( is, newContents ); - if ( getLog().isDebugEnabled() ) - { - getLog().debug( "File " + file + " contents " - + ( needOverwrite ? "differs" : "does not differ" ) ); - } - } - } - - if ( !needOverwrite ) - { - getLog().debug( "File " + file + " is up to date" ); - return; - } - getLog().debug( "Writing " + file ); - - try ( OutputStream os = Files.newOutputStream( file.toPath() ) ) - { - outStream.writeTo( os ); - } - } - private MavenFileFilterRequest setupRequest( Resource resource, File source, File file ) { MavenFileFilterRequest req = new MavenFileFilterRequest(); @@ -1238,8 +1188,7 @@ protected void processResourceBundles( ClassLoader classLoader, VelocityContext { if ( doVelocity ) { - try ( DeferredFileOutputStream os = - new DeferredFileOutputStream( velocityFilterInMemoryThreshold, f ) ) + try ( OutputStream os = new CachingOutputStream( f ) ) { try ( Writer writer = bundle.getSourceEncoding() == null ? new OutputStreamWriter( os ) : new OutputStreamWriter( os, bundle.getSourceEncoding() ) ) @@ -1256,7 +1205,6 @@ protected void processResourceBundles( ClassLoader classLoader, VelocityContext writer ); } } - fileWriteIfDiffers( os ); } } else