live.document.mavenplugin.rootmethod.FindRootMethodsOfEntityAndTableMojo Maven / Gradle / Ivy
package live.document.mavenplugin.rootmethod;
import live.document.generator.utils.FileUtils;
import live.document.mavenplugin.common.AnalyzeAssistant;
import live.document.mavenplugin.common.ProjectAnalyzeResult;
import live.document.scanner.CallGraph;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 分析多个指定方法的调用链中,涉及实体Entity类操作的部分。导出CSV
*/
@Mojo(name = "find-all-root-methods-of-entities-and-tables")
public class FindRootMethodsOfEntityAndTableMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}", required = true)
private File projectRoot;
/**
* 排除某些文件夹不扫描class文件
*/
@Parameter
private String[] excludeFolders;
@Parameter(required = true)
private String gitRepositoryUri;
@Parameter
private String[] plsqlPaths;
@Parameter
private String[] sqlExecuteFullMethodNames;
@Parameter
private String[] entitiesToFindRootMethods;
@Parameter
private String[] tablesToFindRootMethods;
@Parameter
private String[] rootMethodPatterns;
@Parameter
private String[] rootMethodAnnotations;
@Parameter
private String[] rootMethodScopePatterns;
@Parameter
private String[] rootMethodExcludePatterns;
/**
* 最小化入口方法集合。
* 当两个入口方法都调用了同一个Table的写方法时,只显示其中一个入口方法,这样评估改造量更加合理
*/
@Parameter(required = true, defaultValue = "true")
private Boolean minimizeRootMethod;
@Parameter(defaultValue = "", required = true)
private String analysisResultOutputDir;
@Override
public void execute() throws MojoExecutionException {
try {
getLog().info("scan: " + projectRoot.getAbsolutePath());
RootMethodChecker rootMethodChecker = new RootMethodChecker();
rootMethodChecker.setRootMethodPatterns(rootMethodPatterns);
rootMethodChecker.setRootMethodAnnotations(rootMethodAnnotations);
rootMethodChecker.setRootMethodExcludePatterns(rootMethodExcludePatterns);
rootMethodChecker.setRootMethodScopePatterns(rootMethodScopePatterns);
if (entitiesToFindRootMethods == null) {
entitiesToFindRootMethods = new String[]{};
}
if (tablesToFindRootMethods == null) {
tablesToFindRootMethods = new String[]{};
}
FileUtils fileUtils = new FileUtils();
ProjectAnalyzeResult projectResult = AnalyzeAssistant.analyze(projectRoot,
sqlExecuteFullMethodNames,
plsqlPaths,
analysisResultOutputDir,
excludeFolders);
RootMethodFinder finder = new RootMethodFinder(projectResult.getCallGraph(), projectResult.getPlSqlExplained());
finder.setIsRootMethod(rootMethodChecker::isRootMethod);
finder.setMinimizeRootMethod(minimizeRootMethod);
finder.setLogger(getLog());
List results = new ArrayList<>();
results.add("Module,RootMethodName,Object,RW");
for (String entity : entitiesToFindRootMethods) {
getLog().info("find root methods for entity: " + entity);
List collect = finder.findByEntity(entity).stream()
.map(e -> String.format("%s,%s,%s,%s",
getJarFileNameOrModuleName(projectResult.getCallGraph(), e),
e.getName(),
entity,
e.getOperator()))
.collect(Collectors.toList());
results.addAll(collect);
}
for (String table : tablesToFindRootMethods) {
getLog().info("find root methods for table: " + table);
List collect = finder.findByTable(table).stream()
.map(e -> String.format("%s,%s,%s,%s",
getJarFileNameOrModuleName(projectResult.getCallGraph(), e),
e.getName(),
table,
e.getOperator()))
.collect(Collectors.toList());
results.addAll(collect);
}
Path fileName = Paths.get(analysisResultOutputDir, "root-methods.csv");
fileUtils.writeFile(fileName, results.stream().collect(Collectors.joining(System.lineSeparator())));
getLog().info("File created (root methods of entities and tables): file://" + fileName);
} catch (Exception e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage());
}
}
private String getJarFileNameOrModuleName(CallGraph callGraph, RootMethodOperator e) {
return callGraph.getClass(e.getClassName())
.getModule();
}
}