diff --git a/src/main/java/com/ibm/northstar/CodeAnalyzer.java b/src/main/java/com/ibm/northstar/CodeAnalyzer.java index a14ccf35..008726b3 100644 --- a/src/main/java/com/ibm/northstar/CodeAnalyzer.java +++ b/src/main/java/com/ibm/northstar/CodeAnalyzer.java @@ -46,6 +46,12 @@ public class CodeAnalyzer implements Runnable { @Option(names = {"-i", "--input"}, required = true, description = "Path to the project root directory.") private static String input; + @Option(names = {"-b", "--build-cmd"}, description = "Custom build command. Defaults to auto build.") + private static String build; + + @Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.") + private static boolean noBuild = false; + @Option(names = {"-a", "--analysis-level"}, description = "[Optional] Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for full analysis including the system depenedency graph). Default: 1") private static int analysisLevel = 1; @@ -114,7 +120,11 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG combinedJsonObject.add("symbol_table", symbolTableJSON); if (analysisLevel > 1) { // Save SDG, IPCFG, and Call graph as JSON - String sdgAsJSONString = SystemDependencyGraph.construct(input, dependencies, analyzeSource); + // If noBuild is not true, and build is also not provided, we will use "auto" as the build command + build = build == null ? "auto" : build; + // Is noBuild is true, we will not build the project + build = noBuild ? null : build; + String sdgAsJSONString = SystemDependencyGraph.construct(input, dependencies, build); JsonElement sdgAsJSONElement = gson.fromJson(sdgAsJSONString, JsonElement.class); JsonObject sdgAsJSONObject = sdgAsJSONElement.getAsJsonObject(); diff --git a/src/main/java/com/ibm/northstar/SystemDependencyGraph.java b/src/main/java/com/ibm/northstar/SystemDependencyGraph.java index 29f1e690..8c319823 100644 --- a/src/main/java/com/ibm/northstar/SystemDependencyGraph.java +++ b/src/main/java/com/ibm/northstar/SystemDependencyGraph.java @@ -195,7 +195,7 @@ private static org.jgrapht.Graph, AbstractGraphEdge> buil * * @param input the input * @param dependencies the dependencies - * @param experimental the experimental + * @param build The build options * @return A List of triples containing the source, destination, and edge type * @throws IOException the io exception * @throws ClassHierarchyException the class hierarchy exception @@ -204,16 +204,11 @@ private static org.jgrapht.Graph, AbstractGraphEdge> buil * exception */ public static String construct( - String input, String dependencies, boolean experimental) + String input, String dependencies, String build) throws IOException, ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException { // Initialize scope - AnalysisScope scope; - if (dependencies == null) { - scope = ScopeUtils.createScope(input, experimental); - } else { - scope = ScopeUtils.createScope(input, dependencies, experimental); - } + AnalysisScope scope = ScopeUtils.createScope(input, dependencies, build); IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope, new ECJClassLoaderFactory(scope.getExclusions())); Log.done("There were a total of " + cha.getNumberOfClasses() + " classes of which " diff --git a/src/main/java/com/ibm/northstar/utils/BuildProject.java b/src/main/java/com/ibm/northstar/utils/BuildProject.java index 1942c83f..b58d0a8d 100644 --- a/src/main/java/com/ibm/northstar/utils/BuildProject.java +++ b/src/main/java/com/ibm/northstar/utils/BuildProject.java @@ -85,14 +85,25 @@ public static boolean gradleBuild(String projectPath) { return buildWithTool(gradleCommand); } - private static boolean buildProject(String projectPath) { + private static boolean buildProject(String projectPath, String build) { File pomFile = new File(projectPath, "pom.xml"); - if (pomFile.exists()) { - Log.info("Found pom.xml in the project directory. Using Maven to build the project."); - return mavenBuild(projectPath); // Use Maven if pom.xml exists - } else { - Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project."); - return gradleBuild(projectPath); // Otherwise, use Gradle + if (build ==null) { + return true; + } else if (build.equals("auto")) { + if (pomFile.exists()) { + Log.info("Found pom.xml in the project directory. Using Maven to build the project."); + return mavenBuild(projectPath); // Use Maven if pom.xml exists + } else { + Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project."); + return gradleBuild(projectPath); // Otherwise, use Gradle + } + } + else { + // Update command with a project path + build = build.replace("mvn", "mvn -f " + projectPath); + Log.info("Using custom build command: " + build); + String[] customBuildCommand = build.split(" "); + return buildWithTool(customBuildCommand); } } @@ -102,8 +113,8 @@ private static boolean buildProject(String projectPath) { * @param projectPath is the path to the project to be streamed. * @return true if the streaming was successful, false otherwise. */ - public static List buildProjectAndStreamClassFiles(String projectPath) throws IOException { - return buildProject(projectPath) ? classFilesStream(projectPath) : null; + public static List buildProjectAndStreamClassFiles(String projectPath, String build) throws IOException { + return buildProject(projectPath, build) ? classFilesStream(projectPath) : null; } /** diff --git a/src/main/java/com/ibm/northstar/utils/ScopeUtils.java b/src/main/java/com/ibm/northstar/utils/ScopeUtils.java index ac96a23a..242e55e0 100644 --- a/src/main/java/com/ibm/northstar/utils/ScopeUtils.java +++ b/src/main/java/com/ibm/northstar/utils/ScopeUtils.java @@ -52,11 +52,6 @@ public class ScopeUtils { * @return scope The created analysis scope * @throws IOException the io exception */ - public static AnalysisScope createScope(String projectPath, boolean experimental) - throws IOException { - return createScope(projectPath, null, experimental); - } - /** * Create an analysis scope base on the input * @@ -65,7 +60,7 @@ public static AnalysisScope createScope(String projectPath, boolean experimental * @return scope The created analysis scope * @throws IOException the io exception */ - public static AnalysisScope createScope(String projectPath, String applicationDeps, boolean experimental) + public static AnalysisScope createScope(String projectPath, String applicationDeps, String build) throws IOException { Log.info("Create analysis scope."); AnalysisScope scope = new JavaSourceAnalysisScope(); @@ -106,7 +101,7 @@ public static AnalysisScope createScope(String projectPath, String applicationDe Path workDir = Paths.get(tmpDirString); FileUtils.cleanDirectory(workDir.toFile()); - List applicationClassFiles = BuildProject.buildProjectAndStreamClassFiles(projectPath); + List applicationClassFiles = BuildProject.buildProjectAndStreamClassFiles(projectPath, build); Log.debug("Application class files: " + String.valueOf(applicationClassFiles.size())); if (applicationClassFiles == null) { Log.error("No application classes found.");