Skip to content

Commit

Permalink
[MASSEMBLY-927] Support for properties mapping on executions of maven…
Browse files Browse the repository at this point in the history
…-assembly-plugin
  • Loading branch information
rfscholte committed Apr 26, 2020
1 parent 8054f50 commit 0a1aaad
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
* under the License.
*/

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.annotation.Nonnull;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenReaderFilter;
import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;

import javax.annotation.Nonnull;
import java.io.File;
import java.util.List;

/**
*
*/
Expand Down Expand Up @@ -114,6 +116,11 @@ public interface AssemblerConfigurationSource
* @return The filters.
*/
List<String> getFilters();

/**
* @return the additional properties
*/
Properties getAdditionalProperties();

/**
* @return include the project build filters or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.Reader;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
Expand All @@ -45,15 +46,16 @@
public class ReaderFormatter
{
private static Reader createReaderFilter( @Nonnull Reader source, String escapeString, List<String> delimiters,
AssemblerConfigurationSource configSource, boolean isPropertiesFile )
AssemblerConfigurationSource configSource, boolean isPropertiesFile,
Properties additionalProperties )
throws IOException
{
try
{

MavenReaderFilterRequest filterRequest =
new MavenReaderFilterRequest( source, true, configSource.getProject(), configSource.getFilters(),
isPropertiesFile, configSource.getMavenSession(), null );
isPropertiesFile, configSource.getMavenSession(), additionalProperties );

filterRequest.setEscapeString( escapeString );

Expand Down Expand Up @@ -154,7 +156,7 @@ public InputStream transform( @Nonnull PlexusIoResource plexusIoResource,
: new InputStreamReader( inputStream ); // wtf platform encoding ? TODO: Fix this
Reader filtered =
createReaderFilter( source, configSource.getEscapeString(), configSource.getDelimiters(),
configSource, isPropertyFile );
configSource, isPropertyFile, configSource.getAdditionalProperties() );
result = encoding != null
? new ReaderInputStream( filtered, encoding )
: new ReaderInputStream( filtered );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ public abstract class AbstractAssemblyMojo extends AbstractMojo implements Assem
*/
@Parameter
private List<String> filters;

/**
* A set of additional properties to use for filtering
*
* @since 3.3.0
*/
@Parameter
private Properties additionalProperties;

/**
* If True (default) then the ${project.build.filters} are also used in addition to any further filters defined for
Expand Down Expand Up @@ -783,6 +791,11 @@ public void setFilters( final List<String> filters )
{
this.filters = filters;
}

public Properties getAdditionalProperties()
{
return additionalProperties;
}

@Override
public boolean isIncludeProjectBuildFilters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -33,18 +34,22 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Model;
import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.DefaultMavenReaderFilter;
import org.apache.maven.shared.filtering.MavenReaderFilter;
import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
import org.codehaus.plexus.archiver.resources.PlexusIoVirtualFileResource;
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.junit.Test;
import org.mockito.ArgumentCaptor;


public class ReaderFormatterTest
Expand Down Expand Up @@ -99,6 +104,30 @@ public void nonFilteredFileExtensions() throws Exception
assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
}

@Test
public void additionalProperties() throws Exception
{
final MavenReaderFilter mavenReaderFilter = mock( MavenReaderFilter.class );

final PojoConfigSource cfg = getPojoConfigSource();
cfg.setMavenReaderFilter( mavenReaderFilter );
Properties additionalProperties = new Properties();
cfg.setAdditionalProperties( additionalProperties );

InputStreamTransformer transformer = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );

final InputStream inputStream = new ByteArrayInputStream( new byte[0] );
PlexusIoResource resource = mock( PlexusIoResource.class );
when( resource.getName() ).thenReturn( "file.txt" );

transformer.transform( resource, inputStream );

ArgumentCaptor<MavenReaderFilterRequest> filteringRequest =
ArgumentCaptor.forClass(MavenReaderFilterRequest.class);
verify( mavenReaderFilter ).filter( filteringRequest.capture() );
assertThat( filteringRequest.getValue().getAdditionalProperties(), sameInstance( additionalProperties ) );
}

private MavenProject createBasicMavenProject()
{
final Model model = new Model();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
package org.apache.maven.plugins.assembly.testutils;

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.annotation.Nonnull;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
Expand All @@ -26,10 +32,6 @@
import org.apache.maven.shared.filtering.MavenReaderFilter;
import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;

import javax.annotation.Nonnull;
import java.io.File;
import java.util.List;

public class PojoConfigSource
implements AssemblerConfigurationSource
{
Expand Down Expand Up @@ -88,6 +90,8 @@ public class PojoConfigSource
private File archiveBaseDirectory;

private List<String> filters;

private Properties additionalProperties;

private boolean isIncludeProjectBuildFilter;

Expand Down Expand Up @@ -291,6 +295,17 @@ public void setFilters( List<String> filters )
this.filters = filters;
}

@Override
public Properties getAdditionalProperties()
{
return additionalProperties;
}

public void setAdditionalProperties( Properties additionalProperties )
{
this.additionalProperties = additionalProperties;
}

public boolean isIncludeProjectBuildFilters()
{
return isIncludeProjectBuildFilter;
Expand Down

0 comments on commit 0a1aaad

Please sign in to comment.