Java tutorial
/** * Copyright (C) 2013 Steve Owens (DoItNext.com) http://www.doitnext.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.doitnext.mojo.pojo.jsonschemagen; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.reflections.Reflections; import com.doitnext.jsonschema.annotations.JsonSchemaClass; import com.doitnext.jsonschema.generator.SchemaGen; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; /** * Goal which generates schema files for annotated POJOs * * @goal exploded * * @phase prepare-package * @configurator include-project-dependencies * @requiresDependencyResolution compile+runtime */ public class Pojo2JsonSchemaMojo extends AbstractMojo { private ObjectMapper objectMapper = new ObjectMapper(); private String uriPathPrefix = ""; /** * List of packages to scan for JsonSchemaClass annotated classes * @parameter alias="scanPackages" */ private String[] scanPackages = null; /** * Output directory for schemas. * * @parameter expression="${project.basedir}/target/site/schemas" * @required */ private File schemaOutputDirectory; public void setUriPathPrefix(String uriPathPrefix) { this.uriPathPrefix = uriPathPrefix; } public void setSchemaOutputDirectory(File schemaOutputDirectory) { this.schemaOutputDirectory = schemaOutputDirectory; } public void setScanPackages(String[] scanPackages) { this.scanPackages = scanPackages; getLog().info(String.format("Scanning packages %s", Arrays.asList(this.scanPackages))); // TODO: Complete this method } private void prepareTargetDirectories() throws MojoExecutionException { if (schemaOutputDirectory.exists()) { getLog().info(String.format("Output Directory '%s' exists on plugin startup cleaning directory.", schemaOutputDirectory.getPath())); try { File files[] = schemaOutputDirectory.listFiles(); for (File file : files) { if (file.isDirectory()) FileUtils.deleteDirectory(file); else file.delete(); } } catch (IOException e) { throw new MojoExecutionException( String.format("Failed to clean directory %s", schemaOutputDirectory.getPath()), e); } } else { if (schemaOutputDirectory.mkdirs()) { getLog().info(String.format("Created Output Directory '%s'.", schemaOutputDirectory.getPath())); } else { throw new MojoExecutionException( String.format("Unable to create Output Directory '%s'.", schemaOutputDirectory.getPath())); } } } public void execute() throws MojoExecutionException { objectMapper.enable(SerializationFeature.INDENT_OUTPUT); prepareTargetDirectories(); for (String basePackage : scanPackages) { Reflections reflections = new Reflections(basePackage); Set<Class<?>> classesToScan = reflections.getTypesAnnotatedWith(JsonSchemaClass.class); for (Class<?> classz : classesToScan) { scanClass(classz); } } } private void scanClass(Class<?> classz) throws MojoExecutionException { String json = SchemaGen.toSchema(classz, uriPathPrefix); try { JsonNode node = objectMapper.readTree(json); JsonNode idNode = node.get("id"); String id = idNode.textValue(); File f = new File(schemaOutputDirectory, id + ".schema.json"); objectMapper.writeValue(f, node); } catch (Exception e) { System.err.println(json); throw new MojoExecutionException("Failed to handle class " + classz.getName(), e); } } }