Yeah, DEB_BUILD_DATE makes me less uneasy, too. I've updated the patch to replace the line. This assumes that it is present; would be great if this kind of thing was specified. It's hard to verify the assumption as the date comes out in locale format.
>From 4462124973f36a06d2e8695f789c4bc5beb8eaff Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" <g...@goeswhere.com> Date: Sat, 10 Jan 2015 16:48:22 +0000 Subject: [PATCH] honour DEB_BUILD_DATE when generating pom.properties (lines) --- .../apache/maven/archiver/PomPropertiesUtil.java | 70 +++++++++++++++++++++- .../maven/archiver/PomPropertiesUtilTest.java | 24 ++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/maven/archiver/PomPropertiesUtilTest.java diff --git a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java index 5e0a41b..e294419 100644 --- a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java @@ -19,13 +19,21 @@ package org.apache.maven.archiver; * under the License. */ +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; import java.util.Properties; +import java.util.regex.Pattern; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.Archiver; @@ -85,7 +93,7 @@ public class PomPropertiesUtil OutputStream os = new FileOutputStream( outputFile ); try { - properties.store( os, GENERATED_BY_MAVEN ); + storeWithCustomTimestamp( properties, os, GENERATED_BY_MAVEN ); os.close(); // stream is flushed but not closed by Properties.store() os = null; } @@ -95,6 +103,66 @@ public class PomPropertiesUtil } } + private static Date findBuildDate() { + final String envName = "DEB_BUILD_DATE"; + final String envVariable = System.getenv(envName); + if (null == envVariable) { + return null; + } + + try { + return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").parse(envVariable); + } catch (ParseException e) { + throw new IllegalStateException("maven-archiver: " + envName + " not in recognised format", e); + } + } + + /** + * Replace the date in the file with the specified date, and sort the properties + * + * @param lines line 1: comment, line 2: date, line 3 onwards: properties + */ + static void mangle(String[] lines, Date buildDate) { + lines[1] = "#" + buildDate.toString(); + Arrays.sort(lines, 2, lines.length); + } + + private void storeWithCustomTimestamp(Properties properties, OutputStream os, String comment) throws IOException { + final String[] lines = propertyFileLines(properties, comment); + + final Date buildDate = findBuildDate(); + if (null != buildDate) { + mangle(lines, buildDate); + } + + writeLines(os, lines); + } + + private static String[] propertyFileLines(Properties properties, String comment) throws IOException { + final StringWriter stringWriter = new StringWriter(); + try { + properties.store(stringWriter, comment); + } finally { + stringWriter.close(); + } + + return stringWriter.toString() + .split(Pattern.quote(System.getProperty("line.separator"))); + } + + private static void writeLines(OutputStream os, String[] lines) throws IOException { + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "ISO-8859-1")); + try { + for (String line : lines) { + writer.write(line); + writer.newLine(); + } + } finally { + writer.flush(); + writer.close(); + } + } + /** * Creates the pom.properties file. */ diff --git a/src/test/java/org/apache/maven/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/archiver/PomPropertiesUtilTest.java new file mode 100644 index 0000000..b051b2d --- /dev/null +++ b/src/test/java/org/apache/maven/archiver/PomPropertiesUtilTest.java @@ -0,0 +1,24 @@ +package org.apache.maven.archiver; + +import junit.framework.TestCase; + +import java.util.Arrays; +import java.util.Date; + +import static org.apache.maven.archiver.PomPropertiesUtil.mangle; + +public class PomPropertiesUtilTest extends TestCase { + + public void testMangle() { + final Date date = new Date(1); + final String[] input = {"#foo bar", "#" + new Date(), "groupId=foo", "artifactId=bar"}; + mangle(input, date); + + assertTrue(Arrays.equals(new String[] { "#foo bar", "#" + date, "artifactId=bar", "groupId=foo" }, input)); + final String[] strings = {"z", "y", "x", "v", "u"}; + Arrays.sort(strings, 2, strings.length); + assertTrue(Arrays.equals(new String[] { "z", "y", "u", "v", "x" }, strings)); + + } + +} \ No newline at end of file -- 2.1.0