Skip to content

Commit

Permalink
Implement json schema generator plugin, fix #305
Browse files Browse the repository at this point in the history
  • Loading branch information
gashcrumb committed May 7, 2013
1 parent 929e01f commit 8171f99
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 1 deletion.
@@ -0,0 +1,79 @@
<?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">
<parent>
<artifactId>hawtio-json-schema-generator</artifactId>
<groupId>io.hawt</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hawtio-json-schema-generator-plugin</artifactId>
<name>${project.parent.artifactId}/${project.artifactId}</name>

<packaging>maven-plugin</packaging>

<dependencies>

<dependency>
<groupId>io.hawt</groupId>
<artifactId>hawtio-json-schema-mbean</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.4</version>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>

<!-- generated help mojo has a dependency to plexus-utils -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.4</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<!-- see http://jira.codehaus.org/browse/MNG-5346 -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<!-- if you want to generate help goal -->
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
@@ -0,0 +1,56 @@
package io.hawt.jsonschema.maven.plugin;

import io.hawt.jsonschema.SchemaLookup;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

/**
* Uses hawtio-json-schema-mbean to generate Json Schema for Java classes
*
*/
@Mojo( name="generate-json-schema", defaultPhase = LifecyclePhase.GENERATE_SOURCES )
public class JsonSchemaGeneratorMojo extends AbstractMojo
{

/**
* Java classes to be converted into schema
*/
@Parameter(required=true)
private Map<String, String> classes;

public void execute() throws MojoExecutionException
{
SchemaLookup lookup = new SchemaLookup();
lookup.init();

for (String clazz : this.classes.keySet()) {
try {
getLog().info("Looking up schema for class " + clazz);

String targetFileName = this.classes.get(clazz);
String fileContents = "var " + clazz + " = " + lookup.getSchemaForClass(clazz);

File outputFile = new File(targetFileName);

if (outputFile.getParentFile().mkdirs()) {
getLog().info("Created path " + outputFile.getParentFile());
}

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
out.write(fileContents.getBytes());
out.flush();
out.close();
} catch (Exception e) {
throw new MojoExecutionException("Failed to generate schema for " + clazz, e);
}
}
}
}
126 changes: 126 additions & 0 deletions hawtio-json-schema-generator/hawtio-json-schema-generator-test/pom.xml
@@ -0,0 +1,126 @@
<?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">
<parent>
<artifactId>hawtio-json-schema-generator</artifactId>
<groupId>io.hawt</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hawtio-json-schema-generator-test</artifactId>
<name>${project.parent.artifactId}/${project.artifactId}</name>

<properties>
<!-- stupid maven test harness is stupid -->
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>

<!-- these are all totally ignored at the moment -->
<dependency>
<groupId>io.hawt</groupId>
<artifactId>hawtio-json-schema-generator-plugin</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.5.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.sonatype.aether</groupId>
<artifactId>aether-api</artifactId>
<version>1.13.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.sonatype.aether</groupId>
<artifactId>aether-util</artifactId>
<version>1.13.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>io.hawt</groupId>
<artifactId>hawtio-json-schema-generator-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>generate-json-schema</goal>
</goals>
</execution>
</executions>
<configuration>
<classes>
<org.fusesource.fabric.api.CreateContainerChildOptions>
${project.build.outputDirectory}/webapp/fabric/js/createContainerChildOptions.js
</org.fusesource.fabric.api.CreateContainerChildOptions>
<org.fusesource.fabric.api.CreateSshContainerOptions>
${project.build.outputDirectory}/webapp/fabric/js/createSshContainerOptions.js
</org.fusesource.fabric.api.CreateSshContainerOptions>
<org.fusesource.fabric.api.CreateJCloudsContainerOptions>
${project.build.outputDirectory}/webapp/fabric/js/createJcloudsContainerOptions.js
</org.fusesource.fabric.api.CreateJCloudsContainerOptions>
</classes>
</configuration>
<dependencies>
<dependency>
<groupId>org.fusesource.fabric</groupId>
<artifactId>fabric-core</artifactId>
<version>${fabric-version}</version>
</dependency>
</dependencies>
</plugin>

</plugins>

</build>

</project>
@@ -0,0 +1,27 @@
package io.hawt;

import io.hawt.jsonschema.maven.plugin.JsonSchemaGeneratorMojo;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;

import java.io.File;

/**
* @author Stan Lewis
*/
public class JsonSchemaGeneratorMojoTest extends AbstractMojoTestCase {

/*
@Override
protected void setUp() throws Exception {
super.setUp();
}
*/

public void testMojoGoal() throws Exception {
File testPom = new File( getBasedir(), "/target/test-classes/test-pom.xml");
JsonSchemaGeneratorMojo mojo = new JsonSchemaGeneratorMojo();
configureMojo(mojo, "hawtio-json-schema-generator-plugin", testPom);
mojo.execute();
}

}
@@ -0,0 +1,56 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>project</artifactId>
<groupId>io.hawt</groupId>
<version>1.2-SNAPSHOT</version>
</parent>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>

<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>

<dependency>
<groupId>org.fusesource.fabric</groupId>
<artifactId>fabric-core</artifactId>
<version>${fabric-version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.hawt</groupId>
<artifactId>hawtio-json-schema-generator-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<classes>
<org.fusesource.fabric.api.CreateSshContainerOptions>
createSshContainerOptions.json
</org.fusesource.fabric.api.CreateSshContainerOptions>
</classes>
</configuration>
<dependencies>
</dependencies>
</plugin>

</plugins>
</build>
</project>
23 changes: 23 additions & 0 deletions hawtio-json-schema-generator/pom.xml
@@ -0,0 +1,23 @@
<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/maven-v4_0_0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>io.hawt</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hawtio-json-schema-generator</artifactId>

<packaging>pom</packaging>

<name>${project.parent.artifactId}/${project.artifactId}</name>
<description>${project.parent.artifactId} :: ${project.artifactId}</description>

<modules>
<module>hawtio-json-schema-generator-plugin</module>
<module>hawtio-json-schema-generator-test</module>
</modules>


</project>
2 changes: 1 addition & 1 deletion hawtio-json-schema-mbean/pom.xml
Expand Up @@ -92,7 +92,7 @@
<groupId>org.fusesource.fabric</groupId>
<artifactId>fabric-core</artifactId>
<version>${fabric-version}</version>
<scope>provided</scope>
<scope>test</scope>
</dependency>


Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -246,6 +246,7 @@
<module>hawtio-git</module>
<module>hawtio-maven-indexer</module>
<module>hawtio-json-schema-mbean</module>
<module>hawtio-json-schema-generator</module>
<module>hawtio-local-jvm-mbean</module>
<module>hawtio-plugin-mbean</module>
<module>example-services</module>
Expand Down

0 comments on commit 8171f99

Please sign in to comment.