live.document.mavenplugin.networkgraph.NetWorkGraphMojo Maven / Gradle / Ivy
package live.document.mavenplugin.networkgraph;
import live.document.generator.utils.FileUtils;
import live.document.mavenplugin.common.AnalyzeAssistant;
import live.document.mavenplugin.common.CallNodeVisibleFunc;
import live.document.mavenplugin.common.ProjectAnalyzeResult;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Mojo(name = "generate-network-graph-for-methods")
public class NetWorkGraphMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}", required = true)
private File projectRoot;
@Parameter(required = true)
private String gitRepositoryUri;
@Parameter(defaultValue = "true", required = true)
private String displayClassAndMethodName;
@Parameter
private String[] resourceLayerPattern;
@Parameter
private String[] controllerPattern;
@Parameter
private String[] applicationLayerPattern;
@Parameter
private String[] applicationServicePattern;
@Parameter
private String[] domainLayerPattern;
@Parameter
private String[] domainServicePattern;
@Parameter
private String[] entityPattern;
@Parameter
private String[] infrastructureLayerPattern;
@Parameter
private String[] repositoryPattern;
@Parameter
private String[] aclLayerPattern;
@Parameter
private String[] aclPattern;
@Parameter
private String[] transactionPattern;
@Parameter
private String methodMindMapClassName;
@Parameter
private String methodMindMapMethodName;
@Parameter
private String[] tables;
@Parameter
private String[] entities;
@Parameter
private String[] highlightTablesForNetworkGraph;
@Parameter
private String[] highlightEntitiesForNetworkGraph;
/**
* 对类名+方法名 匹配模式,如果匹配就不显示
*/
@Parameter
private String[] plsqlPaths;
@Parameter
private String[] sqlExecuteFullMethodNames;
@Parameter
private String[] methodsForBatchGenerateMindMap;
@Parameter(defaultValue = "", required = true)
private String analysisResultOutputDir;
@Parameter(defaultValue = "200", required = true)
private Integer xStep;
@Parameter(defaultValue = "60", required = true)
private Integer yStep;
/**
* 忽略的module。生成network graph时,会比较module,如果edge跨module,以不同颜色表示。将procedure这样的module ignore,可以避免过多的跨module线
*/
@Parameter
private String[] ignoreModuleMatchForNetworkGraph;
@Override
public void execute() throws MojoExecutionException {
try {
Set methods = new HashSet<>();
if (!isParameterEmpty(methodMindMapClassName) && !isParameterEmpty(methodMindMapMethodName)) {
methods.add(methodMindMapClassName + "." + methodMindMapMethodName);
}
if (!isParameterEmpty(methodsForBatchGenerateMindMap)) {
methods.addAll(Arrays.asList(methodsForBatchGenerateMindMap));
}
if (methods.size() == 0) {
throw new MojoFailureException("Please specify 1. methodMindMapClassName and methodMindMapMethodName or 2. methodsForBatchGenerateMindMap");
}
FileUtils fileUtils = new FileUtils();
ProjectAnalyzeResult projectResult = AnalyzeAssistant.analyze(projectRoot,
sqlExecuteFullMethodNames,
plsqlPaths,
analysisResultOutputDir,
null);
CallNodeVisibleFunc visibleFunc = new CallNodeVisibleFunc(entities, tables, null, null);
getLog().info("create network graph...");
CallTree2BriefNetworkGraph callTree2BriefNetworkGraph = new CallTree2BriefNetworkGraph(
projectResult.getCallGraph(), projectResult.getPlSqlExplained(), methods, visibleFunc, getLog());
callTree2BriefNetworkGraph.setLogger(getLog());
callTree2BriefNetworkGraph.setxStep(xStep);
callTree2BriefNetworkGraph.setyStep(yStep);
callTree2BriefNetworkGraph.setIgnoreModules(ignoreModuleMatchForNetworkGraph);
callTree2BriefNetworkGraph.addSpecialEntitiesAndTables(highlightEntitiesForNetworkGraph, highlightTablesForNetworkGraph);
getLog().info("generating network graph json file for Java method");
String json = "var data=" + callTree2BriefNetworkGraph.generateJson(true, false) + ";";
Path fileName = Paths.get(analysisResultOutputDir, "network-graph-java.json");
fileUtils.writeFile(fileName, json);
getLog().info("Network graph file of Java method created: file://" + fileName);
getLog().info("generating network graph json file for DB Procedure/Function method");
json = "var data=" + callTree2BriefNetworkGraph.generateJson(false, true) + ";";
fileName = Paths.get(analysisResultOutputDir, "network-graph-db.json");
fileUtils.writeFile(fileName, json);
getLog().info("Network graph file of DB procedure/function method created: file://" + fileName);
} catch (Exception e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage());
}
}
private boolean isParameterEmpty(String parameter) {
return (parameter == null || parameter.trim().length() == 0);
}
private boolean isParameterEmpty(String[] parameters) {
return (parameters == null || parameters.length == 0);
}
}