Skip to content

Commit

Permalink
Merge pull request #51 from cbuschka/added_output_format_json_additio…
Browse files Browse the repository at this point in the history
…nal_to_properties

Added support for json output format additionally to properties.
  • Loading branch information
olamy authored Nov 28, 2016
2 parents 4225fd4 + cc5f45e commit 7b2231f
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

<build>
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/org/codehaus/mojo/build/CreateMetadataMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public class CreateMetadataMojo
@Parameter
private Map<String, String> properties = new HashMap<String, String>();

/**
* Enable output format detection. (Disabled per default for compatibility.)
*
* @since 3.0
*/
@Parameter( defaultValue = "false" )
private boolean autoDetectOutputFormat;

/**
* Maven ProjectHelper.
*/
Expand Down Expand Up @@ -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 )
Expand All @@ -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
{
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/codehaus/mojo/build/JsonOutputFormat.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/codehaus/mojo/build/OutputFormat.java
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/main/java/org/codehaus/mojo/build/PropertiesOutputFormat.java
Original file line number Diff line number Diff line change
@@ -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" );
}
}
57 changes: 57 additions & 0 deletions src/test/java/org/codehaus/mojo/build/JsonOutputFormatTest.java
Original file line number Diff line number Diff line change
@@ -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<String,Object> 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");
}
}
32 changes: 32 additions & 0 deletions src/test/java/org/codehaus/mojo/build/OutputFormatTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

}
}
46 changes: 46 additions & 0 deletions src/test/projects/create-metadata-json-it/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.it</groupId>
<artifactId>build-metadata-it</artifactId>
<version>1.0-SNAPSHOT</version>

<scm>
<developerConnection>scm:svn:http://svn.codehaus.org/mojo/trunk/mojo/buildnumber-maven-plugin/src/test/projects/bogus</developerConnection>
</scm>

<build>

<defaultGoal>package</defaultGoal>

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<goals>
<goal>create-metadata</goal>
</goals>
<configuration>
<detectedOutputFormat>true</detectedOutputFormat>
<revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
<!-- see outputDirectory + outputName for the default outputFile -->
<outputFiles>
<outputFile>${project.build.directory}/file1.json</outputFile>
<outputFile>${project.build.directory}/xxx/file1.json</outputFile>
</outputFiles>
<addOutputDirectoryToResources>true</addOutputDirectoryToResources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>

0 comments on commit 7b2231f

Please sign in to comment.