This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/maven-remote-resources-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f5b057  [MRRESOURCES-145] Avoid overwriting the generated file with 
the same content (#50)
9f5b057 is described below

commit 9f5b05724b10fcb248764fb2605f01dea835303a
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Sat Mar 2 10:19:34 2024 +0100

    [MRRESOURCES-145] Avoid overwriting the generated file with the same 
content (#50)
---
 pom.xml                                            |  7 +--
 .../remote/AbstractProcessRemoteResourcesMojo.java | 61 +++-------------------
 2 files changed, 8 insertions(+), 60 deletions(-)

diff --git a/pom.xml b/pom.xml
index 99efba0..3ecff1d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -71,7 +71,7 @@ under the License.
     
<project.build.outputTimestamp>2024-02-26T21:22:56Z</project.build.outputTimestamp>
 
     <!-- Used by site documentation as well, do not remove -->
-    <mavenFilteringVersion>3.3.1</mavenFilteringVersion>
+    <mavenFilteringVersion>3.3.2</mavenFilteringVersion>
   </properties>
 
   <dependencyManagement>
@@ -171,11 +171,6 @@ under the License.
     </dependency>
 
     <!-- other -->
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-      <version>2.15.1</version>
-    </dependency>
     <dependency>
       <groupId>org.apache.velocity</groupId>
       <artifactId>velocity-engine-core</artifactId>
diff --git 
a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java
 
b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java
index 1e10fe8..e2eaa37 100644
--- 
a/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java
+++ 
b/src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java
@@ -18,7 +18,6 @@
  */
 package org.apache.maven.plugin.resources.remote;
 
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.FileReader;
@@ -51,7 +50,6 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.TreeMap;
 
-import org.apache.commons.io.output.DeferredFileOutputStream;
 import org.apache.maven.RepositoryUtils;
 import org.apache.maven.archiver.MavenArchiver;
 import org.apache.maven.artifact.Artifact;
@@ -80,6 +78,7 @@ import 
org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
 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;
@@ -98,6 +97,7 @@ import org.codehaus.plexus.util.IOUtil;
 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;
 import org.eclipse.aether.RepositorySystem;
@@ -255,8 +255,10 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
      * to eliminate unnecessary destination file overwrite. This improves 
build times since further build steps
      * typically rely on the modification date.
      *
+     * @deprecated not used anymore
      * @since 1.6
      */
+    @Deprecated
     @Parameter(defaultValue = "5242880")
     protected int velocityFilterInMemoryThreshold = 5 * 1024 * 1024;
 
@@ -620,17 +622,13 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
 
             if (source.exists() && !source.equals(file)) {
                 if (source == templateSource) {
-                    try (DeferredFileOutputStream os = 
DeferredFileOutputStream.builder()
-                            .setThreshold(velocityFilterInMemoryThreshold)
-                            .setOutputFile(file)
-                            .get()) {
+                    try (CachingOutputStream os = new 
CachingOutputStream(file)) {
                         try (Reader reader = getReader(source);
                                 Writer writer = getWriter(os)) {
                             velocity.evaluate(context, writer, "", reader);
                         } catch (ParseErrorException | 
MethodInvocationException | ResourceNotFoundException e) {
                             throw new MojoExecutionException("Error rendering 
velocity resource: " + source, e);
                         }
-                        fileWriteIfDiffers(os);
                     }
                 } else if (resource.isFiltering()) {
 
@@ -642,7 +640,7 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
                         throw new MojoExecutionException("Error filtering 
resource: " + source, e);
                     }
                 } else {
-                    FileUtils.copyFile(source, file);
+                    FilteringUtils.copyFile(source, file, null, null);
                 }
 
                 // exclude the original (so eclipse doesn't complain about 
duplicate resources)
@@ -670,47 +668,6 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
         }
     }
 
-    /**
-     * If the transformation result fits in memory and the destination file 
already exists
-     * then both are compared.
-     * <p>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.
-     * <p>Note: the method should be called after {@link 
DeferredFileOutputStream#close}
-     *
-     * @param outStream Deferred stream
-     * @throws IOException On IO error.
-     */
-    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();
         req.setFrom(source);
@@ -953,10 +910,7 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
 
                 if (!copyResourceIfExists(f, projectResource, context)) {
                     if (doVelocity) {
-                        try (DeferredFileOutputStream os = 
DeferredFileOutputStream.builder()
-                                .setThreshold(velocityFilterInMemoryThreshold)
-                                .setOutputFile(f)
-                                .get()) {
+                        try (CachingOutputStream os = new 
CachingOutputStream(f)) {
                             try (Writer writer = bundle.getSourceEncoding() == 
null
                                     ? new OutputStreamWriter(os)
                                     : new OutputStreamWriter(os, 
bundle.getSourceEncoding())) {
@@ -968,7 +922,6 @@ public abstract class AbstractProcessRemoteResourcesMojo 
extends AbstractMojo {
                                     velocity.mergeTemplate(bundleResource, 
bundle.getSourceEncoding(), context, writer);
                                 }
                             }
-                            fileWriteIfDiffers(os);
                         }
                     } else {
                         URL resUrl = classLoader.getResource(bundleResource);

Reply via email to