Skip to content

Commit

Permalink
[MRRESOURCES-145] Avoid overwriting the generated file with the same …
Browse files Browse the repository at this point in the history
…content (#50)
  • Loading branch information
gnodet committed Mar 2, 2024
1 parent 190ec34 commit 9f5b057
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 60 deletions.
7 changes: 1 addition & 6 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +50,6 @@
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;
Expand Down Expand Up @@ -80,6 +78,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;
Expand All @@ -98,6 +97,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;
import org.eclipse.aether.RepositorySystem;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -620,17 +622,13 @@ protected boolean copyResourceIfExists(File file, String relFileName, VelocityCo

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()) {

Expand All @@ -642,7 +640,7 @@ protected boolean copyResourceIfExists(File file, String relFileName, VelocityCo
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)
Expand Down Expand Up @@ -670,47 +668,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.
* <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);
Expand Down Expand Up @@ -953,10 +910,7 @@ protected void processResourceBundles(ClassLoader classLoader, VelocityContext c

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())) {
Expand All @@ -968,7 +922,6 @@ protected void processResourceBundles(ClassLoader classLoader, VelocityContext c
velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer);
}
}
fileWriteIfDiffers(os);
}
} else {
URL resUrl = classLoader.getResource(bundleResource);
Expand Down

0 comments on commit 9f5b057

Please sign in to comment.