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_r307728176
 
 

 ##########
 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:
   Not sure, if you put it back in context we are merging multiple files before 
bundling them, it is not a file which is supposed to be visible and properties 
comments are not reparsed anyway, this is why I think we are safe - it 
shouldn't if it would be in assembly plugin where it can be an user exposed 
file.

----------------------------------------------------------------
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