Skip to content

Commit

Permalink
[MSHADE-363] add ReproducibleResourceTransformer to keep compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed May 14, 2020
1 parent 51bbf72 commit 229f4d7
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 36 deletions.
28 changes: 18 additions & 10 deletions src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.maven.plugins.shade.filter.Filter;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer;
import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.AbstractLogEnabled;
Expand Down Expand Up @@ -76,15 +77,15 @@ public void shade( ShadeRequest shadeRequest )
{
Set<String> resources = new HashSet<>();

ResourceTransformer manifestTransformer = null;
ManifestResourceTransformer manifestTransformer = null;
List<ResourceTransformer> transformers =
new ArrayList<>( shadeRequest.getResourceTransformers() );
for ( Iterator<ResourceTransformer> it = transformers.iterator(); it.hasNext(); )
{
ResourceTransformer transformer = it.next();
if ( transformer instanceof ManifestResourceTransformer )
{
manifestTransformer = transformer;
manifestTransformer = (ManifestResourceTransformer) transformer;
it.remove();
}
}
Expand Down Expand Up @@ -265,11 +266,11 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
}

private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeRequest, Set<String> resources,
ResourceTransformer resourceTransformer,
ManifestResourceTransformer manifestTransformer,
JarOutputStream jos )
throws IOException
{
if ( resourceTransformer != null )
if ( manifestTransformer != null )
{
for ( File jar : shadeRequest.getJars() )
{
Expand All @@ -279,22 +280,22 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq
{
JarEntry entry = en.nextElement();
String resource = entry.getName();
if ( resourceTransformer.canTransformResource( resource ) )
if ( manifestTransformer.canTransformResource( resource ) )
{
resources.add( resource );
try ( InputStream inputStream = jarFile.getInputStream( entry ) )
{
resourceTransformer.processResource( resource, inputStream,
shadeRequest.getRelocators(), entry.getTime() );
manifestTransformer.processResource( resource, inputStream,
shadeRequest.getRelocators(), entry.getTime() );
}
break;
}
}
}
}
if ( resourceTransformer.hasTransformedResource() )
if ( manifestTransformer.hasTransformedResource() )
{
resourceTransformer.modifyOutputStream( jos );
manifestTransformer.modifyOutputStream( jos );
}
}
}
Expand Down Expand Up @@ -544,7 +545,14 @@ private boolean resourceTransformed( List<ResourceTransformer> resourceTransform
{
getLogger().debug( "Transforming " + name + " using " + transformer.getClass().getName() );

transformer.processResource( name, is, relocators, time );
if ( transformer instanceof ReproducibleResourceTransformer )
{
( (ReproducibleResourceTransformer) transformer ).processResource( name, is, relocators, time );
}
else
{
transformer.processResource( name, is, relocators );
}

resourceTransformed = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 org.apache.maven.plugins.shade.relocation.Relocator;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
* An abstract class to implement once the old non-reproducible ResourceTransformer API.
*/
abstract class AbstractCompatibilityTransformer
implements ReproducibleResourceTransformer
{
public final void processResource( String resource, InputStream is, List<Relocator> relocators )
throws IOException
{
processResource( resource, is, relocators, 0 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Prevents duplicate copies of the license
*/
public class ApacheLicenseResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
private static final String LICENSE_PATH = "META-INF/LICENSE";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* Merges <code>META-INF/NOTICE.TXT</code> files.
*/
public class ApacheNoticeResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
Set<String> entries = new LinkedHashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* A resource processor that appends content for a resource, separated by a newline.
*/
public class AppendingTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
String resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* A resource processor that aggregates plexus <code>components.xml</code> files.
*/
public class ComponentsXmlResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
private Map<String, Xpp3Dom> components = new LinkedHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* resource into the shaded JAR.
*/
public class DontIncludeResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
String resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Aggregate Apache Groovy extension modules descriptors
*/
public class GroovyResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{

static final String EXT_MODULE_NAME_LEGACY = "META-INF/services/org.codehaus.groovy.runtime.ExtensionModule";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* content into the shaded JAR.
*/
public class IncludeResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
File file;

Expand Down
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 AbstractCompatibilityTransformer
{
private final List<String> defaultAttributes = Arrays.asList( "Export-Package",
"Import-Package",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @since 3.0
*/
public class PluginXmlResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
private List<Xpp3Dom> mojos = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 org.apache.maven.plugins.shade.relocation.Relocator;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
* Transform resource ensuring reproducible output: that requires to get the timestamp of
* the initial resources to define in a reproducible way the timestamp of the transformed
* resource.
*
* @author Hervé Boutemy
* @since 3.2.4
*/
public interface ReproducibleResourceTransformer
extends ResourceTransformer
{
/**
* Transform an individual resource
* @param resource The resource name
* @param is An input stream for the resource, the implementation should *not* close this stream
* @param relocators A list of relocators
* @param time the time of the resource to process
* @throws IOException When the IO blows up
*/
void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @since 3.0.0
*/
public class ResourceBundleAppendingTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
private Map<String, ByteArrayOutputStream> dataMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public interface ResourceTransformer
* @param resource The resource name
* @param is An input stream for the resource, the implementation should *not* close this stream
* @param relocators A list of relocators
* @param time the time of the resource to process
* @throws IOException When the IO blows up
* @deprecated prefer ReproducibleResourceTransformer
*/
void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
void processResource( String resource, InputStream is, List<Relocator> relocators )
throws IOException;

boolean hasTransformedResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* shading process.
*/
public class ServicesResourceTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{

private static final String SERVICES_PATH = "META-INF/services";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* Appends multiple occurrences of some XML file.
*/
public class XmlAppendingTransformer
implements ResourceTransformer
extends AbstractCompatibilityTransformer
{
public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.jar.JarOutputStream;

import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer;
import org.apache.maven.plugins.shade.resource.properties.io.NoCloseOutputStream;
import org.apache.maven.plugins.shade.resource.properties.io.SkipPropertiesDateLineWriter;

Expand All @@ -42,7 +42,7 @@
* @since 3.2.2
*/
public class PropertiesTransformer
implements ResourceTransformer
implements ReproducibleResourceTransformer
{
private String resource;
private String alreadyMergedKey;
Expand Down Expand Up @@ -73,6 +73,13 @@ public boolean canTransformResource( final String resource )
return Objects.equals( resource, this.resource );
}

@Override
public final void processResource( String resource, InputStream is, List<Relocator> relocators )
throws IOException
{
processResource( resource, is, relocators, 0 );
}

@Override
public void processResource( final String resource, final InputStream is, final List<Relocator> relocators,
long time )
Expand Down
Loading

0 comments on commit 229f4d7

Please sign in to comment.