From 0a1aaada8a71334e1256587f91792b9807ae1b94 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Sun, 26 Apr 2020 18:34:19 +0200 Subject: [PATCH] [MASSEMBLY-927] Support for properties mapping on executions of maven-assembly-plugin --- .../AssemblerConfigurationSource.java | 15 +++++++--- .../assembly/format/ReaderFormatter.java | 8 +++-- .../assembly/mojos/AbstractAssemblyMojo.java | 13 +++++++++ .../assembly/format/ReaderFormatterTest.java | 29 +++++++++++++++++++ .../assembly/testutils/PojoConfigSource.java | 23 ++++++++++++--- 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java b/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java index da038d697..ace04d20e 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java +++ b/src/main/java/org/apache/maven/plugins/assembly/AssemblerConfigurationSource.java @@ -19,6 +19,12 @@ * 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; @@ -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; - /** * */ @@ -114,6 +116,11 @@ public interface AssemblerConfigurationSource * @return The filters. */ List getFilters(); + + /** + * @return the additional properties + */ + Properties getAdditionalProperties(); /** * @return include the project build filters or not. diff --git a/src/main/java/org/apache/maven/plugins/assembly/format/ReaderFormatter.java b/src/main/java/org/apache/maven/plugins/assembly/format/ReaderFormatter.java index 4b26f35f9..f47a6fafc 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/format/ReaderFormatter.java +++ b/src/main/java/org/apache/maven/plugins/assembly/format/ReaderFormatter.java @@ -37,6 +37,7 @@ import java.io.Reader; import java.util.LinkedHashSet; import java.util.List; +import java.util.Properties; import java.util.Set; /** @@ -45,7 +46,8 @@ public class ReaderFormatter { private static Reader createReaderFilter( @Nonnull Reader source, String escapeString, List delimiters, - AssemblerConfigurationSource configSource, boolean isPropertiesFile ) + AssemblerConfigurationSource configSource, boolean isPropertiesFile, + Properties additionalProperties ) throws IOException { try @@ -53,7 +55,7 @@ private static Reader createReaderFilter( @Nonnull Reader source, String escapeS MavenReaderFilterRequest filterRequest = new MavenReaderFilterRequest( source, true, configSource.getProject(), configSource.getFilters(), - isPropertiesFile, configSource.getMavenSession(), null ); + isPropertiesFile, configSource.getMavenSession(), additionalProperties ); filterRequest.setEscapeString( escapeString ); @@ -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 ); diff --git a/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java b/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java index 2766a33a5..2999c8d22 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java +++ b/src/main/java/org/apache/maven/plugins/assembly/mojos/AbstractAssemblyMojo.java @@ -260,6 +260,14 @@ public abstract class AbstractAssemblyMojo extends AbstractMojo implements Assem */ @Parameter private List 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 @@ -783,6 +791,11 @@ public void setFilters( final List filters ) { this.filters = filters; } + + public Properties getAdditionalProperties() + { + return additionalProperties; + } @Override public boolean isIncludeProjectBuildFilters() diff --git a/src/test/java/org/apache/maven/plugins/assembly/format/ReaderFormatterTest.java b/src/test/java/org/apache/maven/plugins/assembly/format/ReaderFormatterTest.java index 51d0b9390..d0279a74d 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/format/ReaderFormatterTest.java +++ b/src/test/java/org/apache/maven/plugins/assembly/format/ReaderFormatterTest.java @@ -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; @@ -33,6 +34,7 @@ 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; @@ -40,11 +42,14 @@ 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 @@ -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.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 filteringRequest = + ArgumentCaptor.forClass(MavenReaderFilterRequest.class); + verify( mavenReaderFilter ).filter( filteringRequest.capture() ); + assertThat( filteringRequest.getValue().getAdditionalProperties(), sameInstance( additionalProperties ) ); + } + private MavenProject createBasicMavenProject() { final Model model = new Model(); diff --git a/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java b/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java index 75f501f40..93259558d 100644 --- a/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java +++ b/src/test/java/org/apache/maven/plugins/assembly/testutils/PojoConfigSource.java @@ -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; @@ -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 { @@ -88,6 +90,8 @@ public class PojoConfigSource private File archiveBaseDirectory; private List filters; + + private Properties additionalProperties; private boolean isIncludeProjectBuildFilter; @@ -291,6 +295,17 @@ public void setFilters( List filters ) this.filters = filters; } + @Override + public Properties getAdditionalProperties() + { + return additionalProperties; + } + + public void setAdditionalProperties( Properties additionalProperties ) + { + this.additionalProperties = additionalProperties; + } + public boolean isIncludeProjectBuildFilters() { return isIncludeProjectBuildFilter;