From cc5f45edb08050da205870893d24e2abca8a10aa Mon Sep 17 00:00:00 2001 From: conni Date: Thu, 10 Nov 2016 11:31:27 +0100 Subject: [PATCH] Added support for json output format additional to properties. --- pom.xml | 5 ++ .../mojo/build/CreateMetadataMojo.java | 47 +++++++++++---- .../codehaus/mojo/build/JsonOutputFormat.java | 27 +++++++++ .../org/codehaus/mojo/build/OutputFormat.java | 29 ++++++++++ .../mojo/build/PropertiesOutputFormat.java | 19 +++++++ .../mojo/build/JsonOutputFormatTest.java | 57 +++++++++++++++++++ .../codehaus/mojo/build/OutputFormatTest.java | 32 +++++++++++ .../build/PropertiesOutputFormatTest.java | 57 +++++++++++++++++++ .../mojo/build/it/CreateMetadataMojoTest.java | 19 +++++++ .../projects/create-metadata-json-it/pom.xml | 46 +++++++++++++++ 10 files changed, 328 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/codehaus/mojo/build/JsonOutputFormat.java create mode 100644 src/main/java/org/codehaus/mojo/build/OutputFormat.java create mode 100644 src/main/java/org/codehaus/mojo/build/PropertiesOutputFormat.java create mode 100644 src/test/java/org/codehaus/mojo/build/JsonOutputFormatTest.java create mode 100644 src/test/java/org/codehaus/mojo/build/OutputFormatTest.java create mode 100644 src/test/java/org/codehaus/mojo/build/PropertiesOutputFormatTest.java create mode 100644 src/test/projects/create-metadata-json-it/pom.xml diff --git a/pom.xml b/pom.xml index 2b24d1f..8aa8c82 100644 --- a/pom.xml +++ b/pom.xml @@ -212,6 +212,11 @@ test + + com.google.code.gson + gson + 2.8.0 + diff --git a/src/main/java/org/codehaus/mojo/build/CreateMetadataMojo.java b/src/main/java/org/codehaus/mojo/build/CreateMetadataMojo.java index c180d96..a1a0b08 100644 --- a/src/main/java/org/codehaus/mojo/build/CreateMetadataMojo.java +++ b/src/main/java/org/codehaus/mojo/build/CreateMetadataMojo.java @@ -164,6 +164,14 @@ public class CreateMetadataMojo @Parameter private Map properties = new HashMap(); + /** + * Enable output format detection. (Disabled per default for compatibility.) + * + * @since 3.0 + */ + @Parameter( defaultValue = "false" ) + private boolean autoDetectOutputFormat; + /** * Maven ProjectHelper. */ @@ -196,16 +204,7 @@ public void execute() for ( File file : outputFiles ) { file.getParentFile().mkdirs(); - OutputStream os = null; - try - { - os = new FileOutputStream( file ); - props.store( os, "Created by build system. Do not modify" ); - } - catch ( IOException e ) - { - throw new MojoFailureException( "Unable to store output to " + file, e ); - } + writeToFile(props, file); } if ( attach ) @@ -222,6 +221,34 @@ public void execute() } } + private void writeToFile(Properties props, File file) throws MojoFailureException { + try + { + if( this.autoDetectOutputFormat ) { + OutputFormat outputFormat = OutputFormat.getOutputFormatFor(file.getName()); + writeToFile(props, file, outputFormat); + } + else + { + writeToFile(props, file, OutputFormat.DEFAULT_FORMAT); + } + } + catch ( IOException e ) + { + throw new MojoFailureException( "Unable to store output to " + file, e ); + } + } + + private void writeToFile(Properties props, File file, OutputFormat outputFormat) throws IOException { + OutputStream out = new FileOutputStream(file); + try { + outputFormat.write(props, out); + } + finally { + out.close(); + } + } + public String getRevision() throws MojoExecutionException { diff --git a/src/main/java/org/codehaus/mojo/build/JsonOutputFormat.java b/src/main/java/org/codehaus/mojo/build/JsonOutputFormat.java new file mode 100644 index 0000000..d4d980e --- /dev/null +++ b/src/main/java/org/codehaus/mojo/build/JsonOutputFormat.java @@ -0,0 +1,27 @@ +package org.codehaus.mojo.build; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; + +import java.io.*; +import java.util.Properties; + +public class JsonOutputFormat extends OutputFormat { + @Override + public boolean handles(String fileName) { + return fileName.endsWith(".json"); + } + + @Override + public void write(Properties props, OutputStream out) throws IOException { + Gson gson = new Gson(); + JsonWriter jsonWriter = gson.newJsonWriter(new OutputStreamWriter( out, "UTF-8" )); + jsonWriter.beginObject(); + for(Object key: props.keySet()) { + jsonWriter.name((String)key); + jsonWriter.value(props.getProperty((String)key)); + } + jsonWriter.endObject(); + jsonWriter.flush(); + } +} diff --git a/src/main/java/org/codehaus/mojo/build/OutputFormat.java b/src/main/java/org/codehaus/mojo/build/OutputFormat.java new file mode 100644 index 0000000..510949d --- /dev/null +++ b/src/main/java/org/codehaus/mojo/build/OutputFormat.java @@ -0,0 +1,29 @@ +package org.codehaus.mojo.build; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; + +public abstract class OutputFormat { + static final OutputFormat DEFAULT_FORMAT = new PropertiesOutputFormat(); + + private static final OutputFormat[] FORMATS = new OutputFormat[] { + new JsonOutputFormat(), + OutputFormat.DEFAULT_FORMAT + }; + + public static OutputFormat getOutputFormatFor(String fileName) { + for(OutputFormat outputFormat : OutputFormat.FORMATS) { + if( outputFormat.handles(fileName) ) { + return outputFormat; + } + } + + return OutputFormat.DEFAULT_FORMAT; + } + + public abstract boolean handles(String fileName); + + public abstract void write(Properties props, OutputStream out) throws IOException; +} diff --git a/src/main/java/org/codehaus/mojo/build/PropertiesOutputFormat.java b/src/main/java/org/codehaus/mojo/build/PropertiesOutputFormat.java new file mode 100644 index 0000000..f0962b1 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/build/PropertiesOutputFormat.java @@ -0,0 +1,19 @@ +package org.codehaus.mojo.build; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; + +public class PropertiesOutputFormat extends OutputFormat { + @Override + public boolean handles(String fileName) { + return fileName.endsWith(".properties"); + } + + @Override + public void write(Properties props, OutputStream out) throws IOException { + props.store( out, "Created by build system. Do not modify" ); + } +} diff --git a/src/test/java/org/codehaus/mojo/build/JsonOutputFormatTest.java b/src/test/java/org/codehaus/mojo/build/JsonOutputFormatTest.java new file mode 100644 index 0000000..6faf87a --- /dev/null +++ b/src/test/java/org/codehaus/mojo/build/JsonOutputFormatTest.java @@ -0,0 +1,57 @@ +package org.codehaus.mojo.build; + +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +/** + * Created by conni on 11/10/16. + */ +public class JsonOutputFormatTest { + + private OutputFormat outputFormat = new JsonOutputFormat(); + + private Properties properties = new Properties(); + + private Gson gson = new Gson(); + + @Before + public void before() { + properties.put("key0", "value0"); + properties.put("key1", "value1"); + } + + @Test + public void handlesDotJson() { + assertTrue(outputFormat.handles("file.json")); + } + + @Test + public void doesNotHandleNonJson() { + assertFalse(outputFormat.handles("file.other")); + } + + @Test + public void writesJson() throws IOException { + String s = writePropertiesToString(); + + Map map = gson.fromJson(s, Map.class); + assertThat(map.size(), is(2)); + assertThat(map.get("key0"), is((Object)"value0")); + assertThat(map.get("key1"), is((Object)"value1")); + } + + private String writePropertiesToString() throws IOException { + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + outputFormat.write(properties, bytesOut); + return new String(bytesOut.toByteArray(), "UTF-8"); + } +} \ No newline at end of file diff --git a/src/test/java/org/codehaus/mojo/build/OutputFormatTest.java b/src/test/java/org/codehaus/mojo/build/OutputFormatTest.java new file mode 100644 index 0000000..c01ed83 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/build/OutputFormatTest.java @@ -0,0 +1,32 @@ +package org.codehaus.mojo.build; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by conni on 11/10/16. + */ +public class OutputFormatTest { + + @Test + public void defaultIsPropertiesFormat() { + OutputFormat outputFormat = OutputFormat.getOutputFormatFor("illegal"); + + assertTrue(outputFormat instanceof PropertiesOutputFormat); + } + + @Test + public void jsonForForDotJson() { + OutputFormat outputFormat = OutputFormat.getOutputFormatFor("file.json"); + + assertTrue(outputFormat instanceof JsonOutputFormat); + } + + @Test + public void propertiesForForDotProperties() { + OutputFormat outputFormat = OutputFormat.getOutputFormatFor("file.properties"); + + assertTrue(outputFormat instanceof PropertiesOutputFormat); + } +} \ No newline at end of file diff --git a/src/test/java/org/codehaus/mojo/build/PropertiesOutputFormatTest.java b/src/test/java/org/codehaus/mojo/build/PropertiesOutputFormatTest.java new file mode 100644 index 0000000..5519acf --- /dev/null +++ b/src/test/java/org/codehaus/mojo/build/PropertiesOutputFormatTest.java @@ -0,0 +1,57 @@ +package org.codehaus.mojo.build; + +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +/** + * Created by conni on 11/10/16. + */ +public class PropertiesOutputFormatTest { + + private OutputFormat outputFormat = new PropertiesOutputFormat(); + + private Properties properties = new Properties(); + + @Before + public void before() { + properties.put("key0", "value0"); + properties.put("key1", "value1"); + } + + @Test + public void handlesDotProperties() { + assertTrue(outputFormat.handles("file.properties")); + } + + @Test + public void doesNotHandleNonProperties() { + assertFalse(outputFormat.handles("file.other")); + } + + @Test + public void writesProperties() throws IOException { + byte[] serialized = writeProperties(); + + Properties deserializedProperties = new Properties(); + deserializedProperties.load(new ByteArrayInputStream(serialized)); + + assertThat(deserializedProperties, is(properties)); + } + + private byte[] writeProperties() throws IOException { + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + outputFormat.write(properties, bytesOut); + return bytesOut.toByteArray(); + } +} \ No newline at end of file diff --git a/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java b/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java index 1f874f6..2855a6b 100644 --- a/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java +++ b/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java @@ -47,4 +47,23 @@ public void testBasicConfiguration() Assert.assertTrue( new File( testDir, "target/classes/build.properties" ).exists() ); } + + + @Test + public void testBasicJsonConfiguration() + throws Exception + { + File projDir = resources.getBasedir( "create-metadata-json-it" ); + + MavenExecution mavenExec = maven.forProject( projDir ); + MavenExecutionResult result = mavenExec.execute( "clean", "test" ); + result.assertErrorFreeLog(); + + File testDir = result.getBasedir(); + Assert.assertTrue( new File( testDir, "target/file1.json" ).exists() ); + Assert.assertTrue( new File( testDir, "target/xxx/file1.json" ).exists() ); + Assert.assertTrue( new File( testDir, "target/generated/build-metadata/build.properties" ).exists() ); + Assert.assertTrue( new File( testDir, "target/classes/build.properties" ).exists() ); + + } } diff --git a/src/test/projects/create-metadata-json-it/pom.xml b/src/test/projects/create-metadata-json-it/pom.xml new file mode 100644 index 0000000..61af28c --- /dev/null +++ b/src/test/projects/create-metadata-json-it/pom.xml @@ -0,0 +1,46 @@ + + + + 4.0.0 + + org.codehaus.mojo.it + build-metadata-it + 1.0-SNAPSHOT + + + scm:svn:http://svn.codehaus.org/mojo/trunk/mojo/buildnumber-maven-plugin/src/test/projects/bogus + + + + + package + + + + org.codehaus.mojo + buildnumber-maven-plugin + ${it-plugin.version} + + + useLastCommittedRevision + + create-metadata + + + true + UNKNOWN + + + ${project.build.directory}/file1.json + ${project.build.directory}/xxx/file1.json + + true + + + + + + + + + \ No newline at end of file