You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2019/07/25 09:58:38 UTC

[GitHub] [maven-shade-plugin] rmannibucau commented on a change in pull request #23: [MSHADE-322] adding properties transformer

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_r307214471
 
 

 ##########
 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:
   updated the PR, since we merge properties i can't just browse them and write them manually so I kept the properties.store call but I skip the date line and sort the keys to ensure it is written deterministicly.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services