package com.github.kongchen.swagger.docgen.mavenplugin;

import java.io.File;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import com.github.kongchen.swagger.docgen.AbstractDocumentSource;
import com.github.kongchen.swagger.docgen.GenerateException;

/**
 * Created with IntelliJ IDEA.
 * User: kongchen
 * Date: 3/7/13
 */

/**
 * @goal generate
 * @phase compile
 * @configurator include-project-dependencies
 * @requiresDependencyResolution runtime
 */
public class ApiDocumentMojo extends AbstractMojo {

    /**
     * @parameter
     */
    private List<ApiSource> apiSources;

    public List<ApiSource> getApiSources() {
        return apiSources;
    }

    public void setApiSources(List<ApiSource> apiSources) {
        this.apiSources = apiSources;
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        try {
            getLog().debug(apiSources.toString());
            for (ApiSource apiSource : apiSources) {
                File outputDirectory = new File(apiSource.getOutputPath()).getParentFile();
                if (outputDirectory != null && !outputDirectory.exists()) {
                    if (!outputDirectory.mkdirs()) {
                        throw new MojoExecutionException("Create directory[" +
                                apiSource.getOutputPath() + "] for output failed.");
                    }
                }
                AbstractDocumentSource documentSource = new MavenDocumentSource(apiSource, getLog());
                documentSource.loadDocuments();
                documentSource.toDocuments();
                documentSource.toSwaggerDocuments();
            }

        } catch (Exception e) {
            throw new MojoFailureException(e.getMessage(), e);
        } catch (GenerateException e) {
            throw new MojoFailureException(e.getMessage(), e);
        }
    }
}

@@ -988,16 +988,159 @@
 ption %7B%0A
+        if (apiSources == null) %7B%0A            throw new MojoFailureException(%22You must configure at least one apiSources element%22);%0A        %7D%0A%0A
         
@@ -1955,16 +1955,24 @@
  catch (
+Generate
 Exceptio
@@ -2054,32 +2054,24 @@
    %7D catch (
-Generate
 Exception e)
@@ -2091,39 +2091,41 @@
   throw new Mojo
-Failure
+Execution
 Exception(e.getM

Improve error handling for misconfiguration