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

[MASSEMBLY-939] - Aggregation Handlers support <updateOnly> #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
import org.codehaus.plexus.components.io.resources.ResourceFactory;
import org.codehaus.plexus.logging.Logger;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -784,7 +785,15 @@ private boolean acceptFile( final File inputFile )
{
if ( selectors != null )
{
final FileInfo fileInfo = new DefaultFileInfo( inputFile );
final FileInfo fileInfo;
try
{
fileInfo = ResourceFactory.createResource( inputFile );
}
catch ( final IOException e )
{
throw new ArchiverException( "Error processing file: " + inputFile, e );
}

for ( final FileSelector selector : selectors )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.codehaus.plexus.archiver.ResourceIterator;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
Expand All @@ -38,11 +39,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import static java.lang.Math.max;

abstract class AbstractLineAggregatingHandler
implements ContainerDescriptorHandler
{

private Map<String, AtomicLong> catalogLastModified = new HashMap<>();

private Map<String, List<String>> catalog = new HashMap<>();

private boolean excludeOverride = false;
Expand Down Expand Up @@ -93,6 +99,12 @@ void addToArchive( final Archiver archiver )
writer.println( line );
}
}

final AtomicLong lastModified = catalogLastModified.get( name );
if ( lastModified != null && lastModified.get() > 0 )
{
f.setLastModified( lastModified.get() );
}
}
catch ( final IOException e )
{
Expand Down Expand Up @@ -133,6 +145,22 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )
{
name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();

if ( fileInfo instanceof PlexusIoResource )
{
final PlexusIoResource resource = (PlexusIoResource) fileInfo;
final long lastModified = resource.getLastModified();

if ( catalogLastModified.containsKey( name ) )
{
final AtomicLong latestLastModified = catalogLastModified.get( name );
latestLastModified.set( max( latestLastModified.get(), lastModified ) );
}
else
{
catalogLastModified.put( name, new AtomicLong( lastModified ) );
}
}

List<String> lines = catalog.get( name );
if ( lines == null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3DomWriter;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import javax.annotation.Nonnull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -45,6 +45,8 @@
import java.util.List;
import java.util.Map;

import static java.lang.Math.max;

/**
* Components XML file filter.
*
Expand All @@ -64,6 +66,8 @@ public class ComponentsXmlArchiverFileFilter

private boolean excludeOverride = false;

private long lastModified = 0L;

void addComponentsXml( final Reader componentsReader )
throws XmlPullParserException, IOException
{
Expand Down Expand Up @@ -121,6 +125,11 @@ private void addToArchive( final Archiver archiver )
Xpp3DomWriter.write( fileWriter, dom );
}

if ( lastModified > 0 )
{
f.setLastModified( lastModified );
}

excludeOverride = true;

archiver.addFile( f, COMPONENTS_XML_PATH );
Expand Down Expand Up @@ -183,6 +192,12 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )

if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) )
{
if ( fileInfo instanceof PlexusIoResource )
{
final PlexusIoResource resource = (PlexusIoResource) fileInfo;
lastModified = max( lastModified, resource.getLastModified() );
}

try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( fileInfo.getContents() ) ) )
{
addComponentsXml( reader );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
Expand All @@ -46,6 +47,8 @@
import java.util.Date;
import java.util.List;

import static java.lang.Math.max;

/**
*
*/
Expand All @@ -65,6 +68,8 @@ public class SimpleAggregatingDescriptorHandler

// calculated, temporary values.

private long lastModified = 0L;

private String filePattern;

private String outputPath;
Expand Down Expand Up @@ -94,6 +99,11 @@ public void finalizeArchiveCreation( final Archiver archiver )

final File temp = writePropertiesFile();

if ( lastModified > 0 )
{
temp.setLastModified( lastModified );
}

overrideFilterAction = true;

archiver.addFile( temp, outputPath );
Expand Down Expand Up @@ -172,6 +182,12 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )
readProperties( fileInfo );
filenames.add( name );

if ( fileInfo instanceof PlexusIoResource )
{
final PlexusIoResource resource = (PlexusIoResource) fileInfo;
lastModified = max( lastModified, resource.getLastModified() );
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.apache.maven.plugins.assembly.filter;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ResourceIterator;
import org.codehaus.plexus.archiver.dir.DirectoryArchiver;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class AbstractLineAggregatingHandlerTest
{
/** A time today, rounded down to the previous minute */
static long MODIFIED_TODAY = (System.currentTimeMillis() / TimeUnit.MINUTES.toMillis( 1 )) * TimeUnit.MINUTES.toMillis( 1 );

/** A time yesterday, rounded down to the previous minute */
static long MODIFIED_YESTERDAY = MODIFIED_TODAY - TimeUnit.DAYS.toMillis( 1 );

/** A time last week, rounded down to the previous minute */
static long MODIFIED_LAST_WEEK = MODIFIED_TODAY - TimeUnit.DAYS.toMillis( 7 );

private final AbstractLineAggregatingHandler handler = new AbstractLineAggregatingHandler()
{
@Override
protected String getOutputPathPrefix( final FileInfo fileInfo ) {
return "";
}

@Override
protected boolean fileMatches( final FileInfo fileInfo ) {
return true;
}
};

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testHandlerMergesMatchingFiles()
throws Exception
{
// Arrange
final Archiver archiver = new DirectoryArchiver();
final FileInfo resource1 = resource( "merged.txt", "text1", MODIFIED_YESTERDAY );
final FileInfo resource2 = resource( "merged.txt", "text2", MODIFIED_LAST_WEEK );

// Act
handler.isSelected( resource1 );
handler.isSelected( resource2 );
handler.finalizeArchiveCreation( archiver );

// Assert

final ResourceIterator resources = archiver.getResources();
assertTrue( "Expected at least one resource", resources.hasNext() );

final ArchiveEntry resource = resources.next();
assertFalse( "Expected at most one resource", resources.hasNext() );

try(final BufferedReader in =
new BufferedReader( new InputStreamReader( resource.getInputStream() ) ) )
{
assertEquals( "text1", in.readLine());
assertEquals( "text2", in.readLine());
assertNull( in.readLine() );
}

assertTrue(
"Merging old resources should result in old merge",
resource.getResource().getLastModified() < MODIFIED_TODAY
);
}

private PlexusIoResource resource( final String name, final String text, final long modified ) throws IOException {
final File file = temporaryFolder.newFile();
FileUtils.fileWrite( file, text );
file.setLastModified( modified );
return createResource( file, name );
}
}
Loading