You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2020/02/08 10:30:36 UTC
[maven-shade-plugin] branch MSHADE-350 updated: [MSHADE_350] Add
additionalAttributes
This is an automated email from the ASF dual-hosted git repository.
rfscholte pushed a commit to branch MSHADE-350
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
The following commit(s) were added to refs/heads/MSHADE-350 by this push:
new 9f929ed [MSHADE_350] Add additionalAttributes
9f929ed is described below
commit 9f929edcd20652d3c0bd22d6531a1b5866ce69ea
Author: rfscholte <rf...@apache.org>
AuthorDate: Sat Feb 8 11:30:26 2020 +0100
[MSHADE_350] Add additionalAttributes
---
.../resource/ManifestResourceTransformer.java | 85 +++++++++++++--------
.../resource/ManifestResourceTransformerTest.java | 86 ++++++++++++++++------
2 files changed, 117 insertions(+), 54 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java
index 420d285..688078c 100644
--- a/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java
@@ -21,6 +21,7 @@ package org.apache.maven.plugins.shade.resource;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
@@ -42,16 +43,39 @@ import org.apache.maven.plugins.shade.relocation.Relocator;
public class ManifestResourceTransformer
implements ResourceTransformer
{
+ private final List<String> defaultAttributes = Arrays.asList( "Export-Package",
+ "Import-Package",
+ "Provide-Capability",
+ "Require-Capability" );
+
// Configuration
private String mainClass;
private Map<String, Object> manifestEntries;
+ private List<String> additionalAttributes;
+
// Fields
private boolean manifestDiscovered;
private Manifest manifest;
+
+ public void setMainClass( String mainClass )
+ {
+ this.mainClass = mainClass;
+ }
+
+ public void setManifestEntries( Map<String, Object> manifestEntries )
+ {
+ this.manifestEntries = manifestEntries;
+ }
+
+ public void setAdditionalAttributes( List<String> additionalAttributes )
+ {
+ this.additionalAttributes = additionalAttributes;
+ }
+ @Override
public boolean canTransformResource( String resource )
{
if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
@@ -62,6 +86,7 @@ public class ManifestResourceTransformer
return false;
}
+ @Override
public void processResource( String resource, InputStream is, List<Relocator> relocators )
throws IOException
{
@@ -71,21 +96,46 @@ public class ManifestResourceTransformer
if ( !manifestDiscovered )
{
manifest = new Manifest( is );
-
+
if ( relocators != null && !relocators.isEmpty() )
{
- modifyOSGiAttributes( manifest, relocators );
+ final Attributes attributes = manifest.getMainAttributes();
+
+ for ( final String attribute : defaultAttributes )
+ {
+ final String attributeValue = attributes.getValue( attribute );
+ if ( attributeValue != null )
+ {
+ String newValue = relocate( attributeValue, relocators );
+ attributes.putValue( attribute, newValue );
+ }
+ }
+
+ if ( additionalAttributes != null )
+ {
+ for ( final String attribute : additionalAttributes )
+ {
+ final String attributeValue = attributes.getValue( attribute );
+ if ( attributeValue != null )
+ {
+ String newValue = relocate( attributeValue, relocators );
+ attributes.putValue( attribute, newValue );
+ }
+ }
+ }
}
-
+
manifestDiscovered = true;
}
}
+ @Override
public boolean hasTransformedResource()
{
return true;
}
+ @Override
public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
@@ -114,35 +164,6 @@ public class ManifestResourceTransformer
manifest.write( jos );
}
- private void modifyOSGiAttributes( Manifest manifest, List<Relocator> relocators )
- {
- final Attributes attributes = manifest.getMainAttributes();
- final String exportPackageValue = attributes.getValue( "Export-Package" );
- if ( exportPackageValue != null )
- {
- String newValue = relocate( exportPackageValue, relocators );
- attributes.putValue( "Export-Package", newValue );
- }
- final String importPackageValue = attributes.getValue( "Import-Package" );
- if ( importPackageValue != null )
- {
- String newValue = relocate( importPackageValue, relocators );
- attributes.putValue( "Import-Package", newValue );
- }
- final String provideCapabilityValue = attributes.getValue( "Provide-Capability" );
- if ( provideCapabilityValue != null )
- {
- String newValue = relocate( provideCapabilityValue, relocators );
- attributes.putValue( "Provide-Capability", newValue );
- }
- final String requireCapabilityValue = attributes.getValue( "Require-Capability" );
- if ( requireCapabilityValue != null )
- {
- String newValue = relocate( requireCapabilityValue, relocators );
- attributes.putValue( "Require-Capability", newValue );
- }
- }
-
private String relocate( String originalValue, List<Relocator> relocators )
{
String newValue = originalValue;
diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformerTest.java b/src/test/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformerTest.java
index e309fae..c1c7ff2 100644
--- a/src/test/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformerTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformerTest.java
@@ -23,9 +23,11 @@ import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.OutputStream;
-import java.lang.reflect.Field;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
@@ -34,15 +36,22 @@ import java.util.jar.Manifest;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
+import org.junit.Before;
import org.junit.Test;
public class ManifestResourceTransformerTest
{
- @Test
- public void rewriteManifestEntries() throws Exception
+ private ManifestResourceTransformer transformer;
+
+ @Before
+ public void setUp()
{
- final ManifestResourceTransformer transformer = new ManifestResourceTransformer();
+ transformer = new ManifestResourceTransformer();
+ }
+ @Test
+ public void rewriteDefaultAttributes() throws Exception
+ {
final Manifest manifest = new Manifest();
final Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
@@ -72,25 +81,15 @@ public class ManifestResourceTransformerTest
"osgi.contract;osgi.contract=JavaInject;" +
"filter:=\"(&(osgi.contract=JavaInject)(version=1.0.0))\"," +
"osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.8))\"");
- final ByteArrayOutputStream mboas = new ByteArrayOutputStream();
- try (final OutputStream mos = mboas)
- {
- manifest.write(mos);
- }
- transformer.processResource(
- JarFile.MANIFEST_NAME,
- new ByteArrayInputStream(mboas.toByteArray()),
- Collections.<Relocator>singletonList(new SimpleRelocator(
- "javax", "jakarta",
- Collections.<String>emptyList(), Collections.<String>emptyList())));
-
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try (final JarOutputStream jarOutputStream = new JarOutputStream(out))
- {
- transformer.modifyOutputStream(jarOutputStream);
- }
+
+ List<Relocator> relocators =
+ Collections.<Relocator>singletonList( new SimpleRelocator( "javax", "jakarta",
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList() ) );
+
+ final ByteArrayOutputStream out = transform( manifest, relocators );
- try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray())))
+ try ( final JarInputStream jis = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) ) )
{
final Attributes attrs = jis.getManifest().getMainAttributes();
assertEquals(
@@ -125,4 +124,47 @@ public class ManifestResourceTransformerTest
attrs.getValue("Require-Capability"));
}
}
+
+ @Test
+ public void rewriteAdditionalAttributes() throws Exception
+ {
+ final Manifest manifest = new Manifest();
+ final Attributes attributes = manifest.getMainAttributes();
+ attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
+ attributes.putValue("description-custom",
+ "This jar uses javax packages");
+
+ List<Relocator> relocators =
+ Collections.<Relocator>singletonList( new SimpleRelocator( "javax", "jakarta",
+ Collections.<String>emptyList(),
+ Collections.<String>emptyList() ) );
+
+ transformer.setAdditionalAttributes( Arrays.asList("description-custom", "attribute-unknown") );
+ final ByteArrayOutputStream out = transform( manifest, relocators );
+
+ try ( final JarInputStream jis = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) ) )
+ {
+ final Attributes attrs = jis.getManifest().getMainAttributes();
+ assertEquals( "This jar uses jakarta packages", attrs.getValue( "description-custom" ) );
+ }
+ }
+
+ private ByteArrayOutputStream transform( final Manifest manifest, List<Relocator> relocators )
+ throws IOException
+ {
+ final ByteArrayOutputStream mboas = new ByteArrayOutputStream();
+ try ( final OutputStream mos = mboas )
+ {
+ manifest.write( mos );
+ }
+ transformer.processResource( JarFile.MANIFEST_NAME, new ByteArrayInputStream( mboas.toByteArray() ),
+ relocators );
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try ( final JarOutputStream jarOutputStream = new JarOutputStream( out ) )
+ {
+ transformer.modifyOutputStream( jarOutputStream );
+ }
+ return out;
+ }
}
\ No newline at end of file