rmannibucau commented on a change in pull request #23: [MSHADE-322] adding 
properties transformer
URL: https://github.com/apache/maven-shade-plugin/pull/23#discussion_r307723218
 
 

 ##########
 File path: 
src/main/java/org/apache/maven/plugins/shade/resource/properties/PropertiesTransformer.java
 ##########
 @@ -0,0 +1,195 @@
+package org.apache.maven.plugins.shade.resource.properties;
+
+/*
+ * 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.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.maven.plugins.shade.relocation.Relocator;
+import org.apache.maven.plugins.shade.resource.ResourceTransformer;
+
+/**
+ * Enables to merge a set of properties respecting priority between them.
+ *
+ * @since 3.2.2
+ */
+public class PropertiesTransformer implements ResourceTransformer
+{
+    private String resource;
+    private String alreadyMergedKey;
+    private String ordinalKey;
+    private int defaultOrdinal;
+    private boolean reverseOrder;
+
+    private final List<Properties> properties = new ArrayList<>();
+
+    public PropertiesTransformer()
+    {
+        // no-op
+    }
+
+    protected PropertiesTransformer( final String resource, final String 
ordinalKey,
+                                     final int defaultOrdinal, final boolean 
reversed )
+    {
+        this.resource = resource;
+        this.ordinalKey = ordinalKey;
+        this.defaultOrdinal = defaultOrdinal;
+        this.reverseOrder = reversed;
+    }
+
+    @Override
+    public boolean canTransformResource( final String resource )
+    {
+        return Objects.equals( resource, this.resource );
+    }
+
+    @Override
+    public void processResource( final String resource, final InputStream is, 
final List<Relocator> relocators )
+            throws IOException
+    {
+        final Properties p = new Properties();
+        p.load( is );
+        properties.add( p );
+    }
+
+    @Override
+    public boolean hasTransformedResource()
+    {
+        return !properties.isEmpty();
+    }
+
+    @Override
+    public void modifyOutputStream( final JarOutputStream os ) throws 
IOException
+    {
+        if ( properties.isEmpty() )
+        {
+            return;
+        }
+
+        final Properties out = mergeProperties( sortProperties() );
+        if ( ordinalKey != null )
+        {
+            out.remove( ordinalKey );
+        }
+        if ( alreadyMergedKey != null )
+        {
+            out.remove( alreadyMergedKey );
+        }
+        os.putNextEntry( new JarEntry( resource ) );
+        out.store( os, "# Merged by maven-shade-plugin (" + 
getClass().getName() + ")" );
 
 Review comment:
   Comments: we don't care, we are merging a bunch of properties into one to 
build a unified model so comments consistency can be lost anyway and keeping 
them is not important here so better to drop them IMHO. Side note being: 
assuming we would like to keep them then we wouldn't be able to sort by 
something else then the original gav of the properties which would likely be 
more misleading. Let's start by dropping them and then if users request to keep 
them we could make a complex impl but not sure it would be asked.
   
   About the order: there are multiple methods but only keys is used (either y 
others but more importantly by the store() method) so this should be 
sufficient. I didn't test on a lot of JDK but worse case the order would be 
lost which is not breaking anything for the end user, only the reproducibility 
and hashing of the file would change which is very very low in term of issue 
IMHO.
   
   So at the end I think it is a good compromise complexity/maintainance and 
provided feature.
   
   Does it make sense?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to