
com.github.wu191287278.maven.swagger.SwaggerMojo Maven / Gradle / Ivy
package com.github.wu191287278.maven.swagger;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.wu191287278.maven.swagger.doc.SwaggerDocs;
import com.github.wu191287278.maven.swagger.doc.visitor.ResolveSwaggerType;
import com.google.common.collect.ImmutableMap;
import io.swagger.models.*;
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
@Mojo(name = "generate", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Execute(phase = LifecyclePhase.COMPILE)
public class SwaggerMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(name = "title", defaultValue = "Api Documentation")
private String title;
@Parameter(name = "version", defaultValue = "1.0.0")
private String version;
@Parameter(name = "description", defaultValue = "")
private String description;
@Parameter(name = "schema", defaultValue = "http")
private String schema;
@Parameter(name = "host", defaultValue = "localhost")
private String host;
@Parameter(name = "basePath", defaultValue = "/")
private String basePath;
@Parameter(name = "camel", defaultValue = "true")
private Boolean camel;
@Parameter(name = "timeFormat", defaultValue = "13:11:43")
public String timeFormat;
@Parameter(name = "dateFormat", defaultValue = "2018-09-10")
public String dateFormat;
@Parameter(name = "datetimeFormat", defaultValue = "2018-09-10T13:11:43Z")
public String datetimeFormat;
@Parameter(name = "recursionAncestor", defaultValue = "false")
public Boolean recursionAncestor;
@Parameter(name = "outputDirectory", defaultValue = "${project.build.outputDirectory}/static")
private File outputDirectory;
@Parameter(name = "includeArtifactIds", defaultValue = "")
private String includeArtifactIds;
@Parameter(name = "excludeBasePackage", defaultValue = "")
private String excludeBasePackage;
@Parameter(name = "basePackage", defaultValue = "")
private String basePackage;
@Parameter(name = "skip", defaultValue = "false")
private String skip;
@Override
public void execute() {
if (isSkip()) {
return;
}
Set includeArtifactIdsSet = toSet(getIncludeArtifactIds());
if (!includeArtifactIdsSet.isEmpty() && !includeArtifactIdsSet.contains(project.getArtifactId().toLowerCase())) {
return;
}
String packaging = project.getPackaging();
if ("pom".equals(packaging)) {
return;
}
List libs = new ArrayList<>();
try {
for (String compileClasspathElement : project.getCompileClasspathElements()) {
if (compileClasspathElement == null || new File(compileClasspathElement).isDirectory()) {
continue;
}
libs.add(compileClasspathElement);
}
} catch (DependencyResolutionRequiredException e) {
getLog().error(e);
return;
}
MavenProject parent = project;
while (parent.hasParent()) {
if (parent.isExecutionRoot()) {
break;
}
parent = parent.getParent();
}
SwaggerDocs swaggerDocs = new SwaggerDocs(getTitle(), getDescription(), getVersion(), getBasePath(), getHost());
swaggerDocs.setCamel(getCamel());
ResolveSwaggerType.DATE_FORMAT = getDateFormat();
ResolveSwaggerType.TIME_FORMAT = getTimeFormat();
ResolveSwaggerType.DATETIME_FORMAT = getDatetimeFormat();
ResolveSwaggerType.RECURSION_ANCESTOR = getRecursionAncestor();
Map m = swaggerDocs.parse(parent.getBasedir().getAbsolutePath(), getBasePackage(),getExcludeBasePackage(), libs, c -> {
getLog().info("Parsing " + c);
});
File output = getOutputDirectory();
if (!output.exists()) output.mkdirs();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy