Skip to content

Commit ad2f6f8

Browse files
tisonkunslawekjaranowski
authored andcommitted
[MSHADE-425] Relocate services name before add to serviceEntries
Signed-off-by: tison <[email protected]>
1 parent 26b5873 commit ad2f6f8

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java

+14-31
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@
4141
* shading process.
4242
*/
4343
public class ServicesResourceTransformer
44-
extends AbstractCompatibilityTransformer
44+
extends AbstractCompatibilityTransformer
4545
{
46-
4746
private static final String SERVICES_PATH = "META-INF/services";
4847

4948
private final Map<String, ArrayList<String>> serviceEntries = new HashMap<>();
5049

51-
private List<Relocator> relocators;
52-
5350
private long time = Long.MIN_VALUE;
5451

5552
public boolean canTransformResource( String resource )
@@ -58,14 +55,20 @@ public boolean canTransformResource( String resource )
5855
}
5956

6057
public void processResource( String resource, InputStream is, final List<Relocator> relocators, long time )
61-
throws IOException
58+
throws IOException
6259
{
63-
ArrayList<String> out = serviceEntries.get( resource );
64-
if ( out == null )
60+
resource = resource.substring( SERVICES_PATH.length() + 1 );
61+
for ( Relocator relocator : relocators )
6562
{
66-
out = new ArrayList<>();
67-
serviceEntries.put( resource, out );
63+
if ( relocator.canRelocateClass( resource ) )
64+
{
65+
resource = relocator.relocateClass( resource );
66+
break;
67+
}
6868
}
69+
resource = SERVICES_PATH + '/' + resource;
70+
71+
ArrayList<String> out = serviceEntries.computeIfAbsent( resource, k -> new ArrayList<>() );
6972

7073
Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
7174
while ( scanner.hasNextLine() )
@@ -81,14 +84,9 @@ public void processResource( String resource, InputStream is, final List<Relocat
8184
out.add( relContent );
8285
}
8386

84-
if ( this.relocators == null )
85-
{
86-
this.relocators = relocators;
87-
}
88-
8987
if ( time > this.time )
9088
{
91-
this.time = time;
89+
this.time = time;
9290
}
9391
}
9492

@@ -98,28 +96,13 @@ public boolean hasTransformedResource()
9896
}
9997

10098
public void modifyOutputStream( JarOutputStream jos )
101-
throws IOException
99+
throws IOException
102100
{
103101
for ( Map.Entry<String, ArrayList<String>> entry : serviceEntries.entrySet() )
104102
{
105103
String key = entry.getKey();
106104
ArrayList<String> data = entry.getValue();
107105

108-
if ( relocators != null )
109-
{
110-
key = key.substring( SERVICES_PATH.length() + 1 );
111-
for ( Relocator relocator : relocators )
112-
{
113-
if ( relocator.canRelocateClass( key ) )
114-
{
115-
key = relocator.relocateClass( key );
116-
break;
117-
}
118-
}
119-
120-
key = SERVICES_PATH + '/' + key;
121-
}
122-
123106
JarEntry jarEntry = new JarEntry( key );
124107
jarEntry.setTime( time );
125108
jos.putNextEntry( jarEntry );

src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.charset.StandardCharsets;
3131
import java.util.ArrayList;
3232
import java.util.Arrays;
33+
import java.util.Collections;
3334
import java.util.List;
3435
import java.util.jar.JarEntry;
3536
import java.util.jar.JarFile;
@@ -85,6 +86,49 @@ public void relocatedClasses() throws Exception {
8586
tempJar.delete();
8687
}
8788
}
89+
90+
@Test
91+
public void mergeRelocatedFiles() throws Exception {
92+
SimpleRelocator relocator =
93+
new SimpleRelocator( "org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*"));
94+
relocators.add( relocator );
95+
96+
String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
97+
String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
98+
byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
99+
String contentResource = "META-INF/services/org.foo.something.another";
100+
String contentResourceShaded = "META-INF/services/borg.foo.something.another";
101+
102+
ServicesResourceTransformer xformer = new ServicesResourceTransformer();
103+
104+
try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
105+
xformer.processResource(contentResource, contentStream, relocators, 0);
106+
}
107+
108+
try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
109+
xformer.processResource(contentResourceShaded, contentStream, relocators, 0);
110+
}
111+
112+
File tempJar = File.createTempFile("shade.", ".jar");
113+
tempJar.deleteOnExit();
114+
FileOutputStream fos = new FileOutputStream( tempJar );
115+
try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
116+
xformer.modifyOutputStream( jos );
117+
jos.close();
118+
119+
JarFile jarFile = new JarFile( tempJar );
120+
JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
121+
assertNotNull( jarEntry );
122+
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
123+
String xformedContent = IOUtils.toString( entryStream, StandardCharsets.UTF_8);
124+
assertEquals( contentShaded + contentShaded, xformedContent );
125+
} finally {
126+
jarFile.close();
127+
}
128+
} finally {
129+
tempJar.delete();
130+
}
131+
}
88132

89133
@Test
90134
public void concatanationAppliedMultipleTimes() throws Exception {

0 commit comments

Comments
 (0)