Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure files are not overwritten with the same content #29

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ under the License.
<project.build.outputTimestamp>2022-07-17T16:33:17Z</project.build.outputTimestamp>

<!-- Used by site documentation as well, do not remove -->
<mavenFilteringVersion>3.2.0</mavenFilteringVersion>
<mavenFilteringVersion>3.3.2-SNAPSHOT</mavenFilteringVersion>
</properties>

<dependencies>
Expand Down Expand Up @@ -149,13 +149,13 @@ under the License.
</exclusion>
</exclusions>
</dependency>

<!-- other -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.5.1</version>
</dependency>

<!-- other -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* under the License.
*/

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 ) )
{
Expand All @@ -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() )
Expand All @@ -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)
Expand Down Expand Up @@ -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.
* <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 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();
Expand Down Expand Up @@ -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() ) )
Expand All @@ -1256,7 +1205,6 @@ protected void processResourceBundles( ClassLoader classLoader, VelocityContext
writer );
}
}
fileWriteIfDiffers( os );
}
}
else
Expand Down