Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.stapler file generation is nondeterministic #526

Closed
basil opened this issue Apr 10, 2024 · 0 comments · Fixed by #527
Closed

.stapler file generation is nondeterministic #526

basil opened this issue Apr 10, 2024 · 0 comments · Fixed by #527

Comments

@basil
Copy link
Member

basil commented Apr 10, 2024

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);
+            }
+        }
+    }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant