You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/09/05 10:14:03 UTC
[maven-shade-plugin] branch master updated: [MSHADE-425] Relocate services name before add to serviceEntries
This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new ad2f6f8 [MSHADE-425] Relocate services name before add to serviceEntries
ad2f6f8 is described below
commit ad2f6f8e7855860b69b950d14ca8ec627b099d6b
Author: tison <wa...@gmail.com>
AuthorDate: Sun Sep 4 00:16:40 2022 +0800
[MSHADE-425] Relocate services name before add to serviceEntries
Signed-off-by: tison <wa...@gmail.com>
---
.../resource/ServicesResourceTransformer.java | 45 +++++++---------------
.../resource/ServiceResourceTransformerTest.java | 44 +++++++++++++++++++++
2 files changed, 58 insertions(+), 31 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
index 1edc153..7eaeaf5 100644
--- a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
@@ -41,15 +41,12 @@ import org.apache.maven.plugins.shade.relocation.Relocator;
* shading process.
*/
public class ServicesResourceTransformer
- extends AbstractCompatibilityTransformer
+ extends AbstractCompatibilityTransformer
{
-
private static final String SERVICES_PATH = "META-INF/services";
private final Map<String, ArrayList<String>> serviceEntries = new HashMap<>();
- private List<Relocator> relocators;
-
private long time = Long.MIN_VALUE;
public boolean canTransformResource( String resource )
@@ -58,14 +55,20 @@ public class ServicesResourceTransformer
}
public void processResource( String resource, InputStream is, final List<Relocator> relocators, long time )
- throws IOException
+ throws IOException
{
- ArrayList<String> out = serviceEntries.get( resource );
- if ( out == null )
+ resource = resource.substring( SERVICES_PATH.length() + 1 );
+ for ( Relocator relocator : relocators )
{
- out = new ArrayList<>();
- serviceEntries.put( resource, out );
+ if ( relocator.canRelocateClass( resource ) )
+ {
+ resource = relocator.relocateClass( resource );
+ break;
+ }
}
+ resource = SERVICES_PATH + '/' + resource;
+
+ ArrayList<String> out = serviceEntries.computeIfAbsent( resource, k -> new ArrayList<>() );
Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
while ( scanner.hasNextLine() )
@@ -81,14 +84,9 @@ public class ServicesResourceTransformer
out.add( relContent );
}
- if ( this.relocators == null )
- {
- this.relocators = relocators;
- }
-
if ( time > this.time )
{
- this.time = time;
+ this.time = time;
}
}
@@ -98,28 +96,13 @@ public class ServicesResourceTransformer
}
public void modifyOutputStream( JarOutputStream jos )
- throws IOException
+ throws IOException
{
for ( Map.Entry<String, ArrayList<String>> entry : serviceEntries.entrySet() )
{
String key = entry.getKey();
ArrayList<String> data = entry.getValue();
- if ( relocators != null )
- {
- key = key.substring( SERVICES_PATH.length() + 1 );
- for ( Relocator relocator : relocators )
- {
- if ( relocator.canRelocateClass( key ) )
- {
- key = relocator.relocateClass( key );
- break;
- }
- }
-
- key = SERVICES_PATH + '/' + key;
- }
-
JarEntry jarEntry = new JarEntry( key );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );
diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
index 8c3dd85..f58c1a0 100644
--- a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
@@ -30,6 +30,7 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -85,6 +86,49 @@ public class ServiceResourceTransformerTest {
tempJar.delete();
}
}
+
+ @Test
+ public void mergeRelocatedFiles() throws Exception {
+ SimpleRelocator relocator =
+ new SimpleRelocator( "org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*"));
+ relocators.add( relocator );
+
+ String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
+ String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
+ byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
+ String contentResource = "META-INF/services/org.foo.something.another";
+ String contentResourceShaded = "META-INF/services/borg.foo.something.another";
+
+ ServicesResourceTransformer xformer = new ServicesResourceTransformer();
+
+ try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
+ xformer.processResource(contentResource, contentStream, relocators, 0);
+ }
+
+ try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
+ xformer.processResource(contentResourceShaded, contentStream, relocators, 0);
+ }
+
+ File tempJar = File.createTempFile("shade.", ".jar");
+ tempJar.deleteOnExit();
+ FileOutputStream fos = new FileOutputStream( tempJar );
+ try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
+ xformer.modifyOutputStream( jos );
+ jos.close();
+
+ JarFile jarFile = new JarFile( tempJar );
+ JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
+ assertNotNull( jarEntry );
+ try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
+ String xformedContent = IOUtils.toString( entryStream, StandardCharsets.UTF_8);
+ assertEquals( contentShaded + contentShaded, xformedContent );
+ } finally {
+ jarFile.close();
+ }
+ } finally {
+ tempJar.delete();
+ }
+ }
@Test
public void concatanationAppliedMultipleTimes() throws Exception {