You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2020/05/04 09:41:33 UTC

[felix-dev] branch master updated: FELIX-6203 skip adding comments in pom.properties

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new a3ea095  FELIX-6203 skip adding comments in pom.properties
     new 03a2ccf  Merge pull request #17 from doggy-dev/FELIX-6203
a3ea095 is described below

commit a3ea095d43f0cf9230bd7bc5dc54c41c26b37321
Author: Veselin Markov <ve...@adesso.de>
AuthorDate: Sun Apr 12 14:54:56 2020 +0200

    FELIX-6203 skip adding comments in pom.properties
---
 .../apache/felix/bundleplugin/BundlePlugin.java    | 42 ++++++++++++++++++++--
 .../felix/bundleplugin/BundlePluginTest.java       | 23 +++++++++++-
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 8d0e570..09f9b2a 100644
--- a/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -19,15 +19,19 @@
 package org.apache.felix.bundleplugin;
 
 
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.lang.reflect.Array;
 import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -1586,9 +1590,41 @@ public class BundlePlugin extends AbstractMojo
         p.put( "version", currentProject.getVersion() );
         p.put( "groupId", currentProject.getGroupId() );
         p.put( "artifactId", currentProject.getArtifactId() );
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        p.store( out, "Generated by org.apache.felix.bundleplugin" );
-        jar.putResource( path + "/pom.properties", new EmbeddedResource( out.toByteArray(), System.currentTimeMillis() ) );
+        jar.putResource( path + "/pom.properties", new EmbeddedResource( toFileContentAsBytes( p ), System.currentTimeMillis() ) );
+    }
+
+
+    private byte[] toFileContentAsBytes( Properties properties )
+    {
+        byte[] bytes;
+        try
+        {
+            StringWriter sw = new StringWriter();
+            properties.store( sw, null );
+    
+            BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
+            StringWriter stringWriter = new StringWriter();
+    
+            String line;
+            while ( ( line = r.readLine() ) != null )
+            {
+                if ( !line.startsWith( "#" ) )
+                {
+                    stringWriter.append( line ).append( NL );
+                }
+            }
+            bytes = stringWriter.getBuffer().toString().getBytes( StandardCharsets.ISO_8859_1 );
+           
+            r.close();
+            sw.close();
+            stringWriter.close();
+        }
+        catch ( IOException e )
+        {
+            getLog().error( "Error while converting properties to file content. Returning empty array.", e );
+            bytes = new byte[] {};
+        }
+        return bytes;
     }
 
 
diff --git a/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
index f18a767..6eaa2ef 100644
--- a/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
+++ b/tools/maven-bundle-plugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
@@ -1,6 +1,10 @@
 package org.apache.felix.bundleplugin;
 
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -22,6 +26,7 @@ package org.apache.felix.bundleplugin;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
@@ -35,12 +40,12 @@ import org.apache.felix.bundleplugin.BundlePlugin.ClassPathItem;
 import org.apache.maven.model.Organization;
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.project.MavenProject;
-import org.apache.maven.shared.dependency.graph.DependencyNode;
 import org.osgi.framework.Constants;
 
 import aQute.bnd.osgi.Analyzer;
 import aQute.bnd.osgi.Builder;
 import aQute.bnd.osgi.Jar;
+import aQute.bnd.osgi.Resource;
 
 
 /**
@@ -374,6 +379,22 @@ public class BundlePluginTest extends AbstractBundlePluginTest
         assertEquals( eas1, eas2 );
     }
 
+    /**
+     * Test that no comment with a time stamp is added to the pom.properties file so ensure a reproducible build. Easiest way is
+     * to remove all comments.
+     */
+    public void testIgnoreTimestampInPomProperties() throws Exception
+    {
+        final MavenProject project = this.getMavenProjectStub();
+        final Map<String, String> instructions = new HashMap<>();
+
+        final Builder builder = this.plugin.buildOSGiBundle( project, instructions, this.plugin.getClasspath( project) );
+
+        final String path = "META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.properties";
+        final Resource resource = builder.getJar().getResource(path);
+        final String fileContent = new String(resource.buffer().array(), StandardCharsets.ISO_8859_1);
+        assertThat(fileContent, not(containsString("#")));
+    }
 
     public void testPropertySanitizing() throws Exception
     {