Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHADE-353] adding a generic relocation friendly transformer #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.apache.maven.plugins.shade.resource;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.util.List;

import org.apache.maven.plugins.shade.relocation.Relocator;

/**
* Abstract class providing the needed logic to relocate any content.
*/
public abstract class BaseRelocatingTransformer implements ResourceTransformer
{
protected String relocate( String originalValue, List<Relocator> relocators )
{
String newValue = originalValue;
for ( Relocator relocator : relocators )
{
String value;
do
{
value = newValue;
newValue = relocator.relocateClass( value );
}
while ( !value.equals( newValue ) );
}
return newValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @since 1.2
*/
public class ManifestResourceTransformer
implements ResourceTransformer
extends BaseRelocatingTransformer
{
private final List<String> defaultAttributes = Arrays.asList( "Export-Package",
"Import-Package",
Expand Down Expand Up @@ -163,20 +163,4 @@ public void modifyOutputStream( JarOutputStream jos )
jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
manifest.write( jos );
}

private String relocate( String originalValue, List<Relocator> relocators )
{
String newValue = originalValue;
for ( Relocator relocator : relocators )
{
String value;
do
{
value = newValue;
newValue = relocator.relocateClass( value );
}
while ( !value.equals( newValue ) );
}
return newValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.apache.maven.plugins.shade.resource;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.jar.JarOutputStream;

import org.apache.maven.plugins.shade.relocation.Relocator;
import org.codehaus.plexus.util.IOUtil;

/**
* Trivial transformer applying relocators on resources content.
*/
public class RelocationTransformer extends BaseRelocatingTransformer
{
private Collection<ResourceTransformer> delegates;
private boolean transformed;

@Override
public boolean canTransformResource( String resource )
{
if ( delegates == null )
{
return false;
}
for ( ResourceTransformer transformer : delegates )
{
if ( transformer.canTransformResource( resource ) )
{
return true;
}
}
return false;
}

@Override
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException
{
byte[] relocated = null;
for ( ResourceTransformer transformer : delegates )
{
if ( transformer.canTransformResource( resource ) )
{
transformed = true;
if ( relocated == null )
{
relocated = relocate( IOUtil.toString( is ), relocators )
.getBytes( StandardCharsets.UTF_8 );
}
transformer.processResource(
resource,
new ByteArrayInputStream( relocated ),
relocators );
}
}
}

@Override
public boolean hasTransformedResource()
{
return transformed;
}

@Override
public void modifyOutputStream( JarOutputStream os ) throws IOException
{
if ( !transformed )
{
return;
}
for ( ResourceTransformer transformer : delegates )
{
if ( transformer.hasTransformedResource() )
{
transformer.modifyOutputStream( os );
}
}
}

public void setDelegates( Collection<ResourceTransformer> delegates )
{
this.delegates = delegates;
}
}
43 changes: 42 additions & 1 deletion src/site/apt/examples/resource-transformers.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ Resource Transformers
*-----------------------------------------+------------------------------------------+
| {{PropertiesTransformer}} | Merges properties files owning an ordinal to solve conflicts |
*-----------------------------------------+------------------------------------------+
| {{ResourceBundleAppendingTransformer}} | Merges ResourceBundles |
| {{RelocationTransformer}} | Delegates the processing to other transformers but relocating the resource contents |
*-----------------------------------------+------------------------------------------+
| {{ResourceBundleAppendingTransformer}} | Merges ResourceBundles |
*-----------------------------------------+------------------------------------------+
| {{ServicesResourceTransformer}} | Relocated class names in <<<META-INF/services>>> resources and merges them. |
*-----------------------------------------+------------------------------------------+
Expand Down Expand Up @@ -601,6 +603,45 @@ Transformers in <<<org.apache.maven.plugins.shade.resource>>>
</project>
+-----

* Relocating file content with {RelocationTransformer}

The <<<RelocationTransformer>>> allows to apply relocators before delegating the processing to other transformers.

+-----
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.RelocationTransformer">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing an important detail, but in this example is no relocation. So why do it like this?
And I would have expected that defining both transformers after each other would work, but that requires defining the resource twice. That's probably the thing you're trying to solve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is about composing transformers which is not something obvious for most people.
Defining each one after the other does not work because the relocation transformer is a delagate pattern which only works if there is another transformer saying it what it should handle. Goal is not to not define resource twice but to be able to decorate any transformer with this feature, including the ones with dynamic resource handling (pattern in canTransform for ex).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested transformers looks more like a XML representation of the Java solution, and it might also expose a design flaw in the plugin.
It probably makes more sense to start with the resources, and define its transformers:

<resources>
  <resource>
     <includes/>
     <excludes/>
     <transformers/>
  </resource>
</resources>

I understand that this is quite a different approach, but this looks way more clear to what's happening for the end user. Maybe the code can stay almost the same, but I would map this configuration to the actual transformer calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @rfscholte , I don't get this proposal at all. You assume the resource configuration is an input of a transformer but a transformer can actually compute its resources and even create them on the fly without taking any input from the project sources so I don't see how it can work :s.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your test the input is foo/bar.txt, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yes and no. It is foo/bar.txt cause it is an AppendingTransformer but the RelocatingTransformer is not aware of that detail. Think to a GraalVM transformer, it will create resources depending what is in the build so the files will not be configured statically. The RelocatingTransformer still works.

<delegates>
<transformer implementation="org.apache.maven.plugins.shade.resource.properties.PropertiesTransformer">
<resource>configuration/application.properties</resource>
<ordinalKey>ordinal</ordinalKey>
</transformer>
</delegates>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
+-----

* Merging Apache OpenWebBeans configuration with {OpenWebBeansPropertiesTransformer}

<<<OpenWebBeansPropertiesTransformer>>> preconfigure a <<<PropertiesTransformer>>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.apache.maven.plugins.shade.resource;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;

import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Test;

/**
* Ensure the relation transformer uses relocators.
*/
public class RelocationTransformerTest
{
@Test
public void relocate() throws IOException
{
AppendingTransformer delegate = new AppendingTransformer();
delegate.resource = "foo/bar.txt";

RelocationTransformer resourceTransformer = new RelocationTransformer();
resourceTransformer.setDelegates( Collections.<ResourceTransformer>singletonList( delegate ) );

assertTrue( resourceTransformer.canTransformResource( "foo/bar.txt" ) );
resourceTransformer.processResource(
"foo/bar.txt",
new ByteArrayInputStream("a=javax.foo.bar".getBytes( StandardCharsets.UTF_8 )),
Collections.<Relocator>singletonList( new SimpleRelocator(
"javax", "jakarta", null, null ) ));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try ( JarOutputStream jarOutputStream = new JarOutputStream(out) )
{
resourceTransformer.modifyOutputStream(jarOutputStream);
}
try ( JarInputStream jarInputStream = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) ))
{
final JarEntry entry = jarInputStream.getNextJarEntry();
assertNotNull( entry );
assertEquals( "foo/bar.txt", entry.getName() );
assertEquals( "a=jakarta.foo.bar", IOUtil.toString( jarInputStream ).trim() );
assertNull( jarInputStream.getNextJarEntry() );
}
}
}