jenkinsci / stapler

Stapler web framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`.stapler` file generation is nondeterministic

basil opened this issue · comments

One issue blocking progress on jenkinsci/plugin-pom#919 is the use of Properties#store, which adds timestamps to every generated resource file — though perhaps this could be worked around by specifying -Djava.properties.date on recent Java versions or using a custom Writer that chomps comment lines along the lines of

diff --git a/core/src/main/java/org/kohsuke/stapler/jsr269/AbstractProcessorImpl.java b/core/src/main/java/org/kohsuke/stapler/jsr269/AbstractProcessorImpl.java
index 017b9023a..d04b6ef48 100644
--- a/core/src/main/java/org/kohsuke/stapler/jsr269/AbstractProcessorImpl.java
+++ b/core/src/main/java/org/kohsuke/stapler/jsr269/AbstractProcessorImpl.java
@@ -26,10 +26,12 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import javax.annotation.processing.AbstractProcessor;
 import javax.lang.model.element.Element;
 import javax.tools.FileObject;
+import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.Properties;
 
 import static javax.tools.Diagnostic.Kind.*;
@@ -66,8 +68,8 @@ abstract class AbstractProcessorImpl extends AbstractProcessor {
 
     protected void writePropertyFile(Properties p, String name) throws IOException {
         FileObject f = createResource(name);
-        try (OutputStream os = f.openOutputStream()) {
-            p.store(os,null);
+        try (Writer w = f.openWriter(); BufferedWriter bw = new CommentStrippingBufferedWriter(w)) {
+            p.store(bw,null);
         }
     }
 
@@ -78,4 +80,17 @@ abstract class AbstractProcessorImpl extends AbstractProcessor {
     protected FileObject createResource(String name) throws IOException {
         return processingEnv.getFiler().createResource(CLASS_OUTPUT, "", name);
     }
+
+    private static class CommentStrippingBufferedWriter extends BufferedWriter {
+        public CommentStrippingBufferedWriter(Writer out) {
+            super(out);
+        }
+
+        @Override
+        public void write(String str) throws IOException {
+            if (!str.startsWith("#")) {
+                super.write(str);
+            }
+        }
+    }
 }