Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
version=1.0.6
version=1.0.7-dev

3 changes: 2 additions & 1 deletion src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CodeAnalyzer implements Runnable {
@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 = {"-f", "--project-root-path"}, description = "Path to the root pom.xml file of the project.")
@Option(names = {"-f", "--project-root-path"}, description = "Path to the root pom.xml/build.gradle file of the project.")
public static String projectRootPom;

@Option(names = {"-a", "--analysis-level"}, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for call graph). Default: 1")
Expand Down Expand Up @@ -110,6 +110,7 @@ private static void analyze() throws Exception {

JsonObject combinedJsonObject = new JsonObject();
Map<String, JavaCompilationUnit> symbolTable;
projectRootPom = projectRootPom == null ? input : projectRootPom;
// First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
if (sourceAnalysis != null) {
// Construct symbol table for source code
Expand Down
60 changes: 56 additions & 4 deletions src/main/java/com/ibm/cldk/utils/BuildProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class BuildProject {
public static Path libDownloadPath;
private static final String LIB_DEPS_DOWNLOAD_DIR = "_library_dependencies";
private static final String MAVEN_CMD = BuildProject.getMavenCommand();
private static final String GRADLE_CMD = BuildProject.getGradleCmd();

/**
* Gets the maven command to be used for building the project.
*
* @return the maven command
*/
private static String getMavenCommand() {
Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
String mvnCommand;
Expand All @@ -32,7 +39,30 @@ private static String getMavenCommand() {
return mvnCommand;
}

private static final String GRADLE_CMD = System.getProperty("os.name").toLowerCase().contains("windows") ? "gradlew.bat" : "gradlew";
/**
* Gets the gradle command to be used for building the project.
*
* @return the gradle command
*/
private static String getGradleCmd() {
String GRADLE_CMD;
String osName = System.getProperty("os.name").toLowerCase();
boolean isWindows = osName.contains("windows");
String gradleWrapper = isWindows ? "gradlew.bat" : "gradlew";
String gradle = isWindows ? "gradle.bat" : "gradle";

String gradleWrapperExists = new File(projectRootPom, gradleWrapper).exists() ? "true" : "false";

if (new File(projectRootPom, gradleWrapper).exists()) {
GRADLE_CMD = gradleWrapper;
} else if (commandExists(gradle)) {
GRADLE_CMD = gradle;
} else {
throw new IllegalStateException("Could not file a valid gradle command. I did not find " + gradleWrapper + " or " + gradle + " in the project directory or in the system PATH.");
}
return GRADLE_CMD;
}

public static Path tempInitScript;
static {
try {
Expand Down Expand Up @@ -60,6 +90,16 @@ private static String getMavenCommand() {
" }\n" +
"}";

private static boolean commandExists(String command) {
try {
Process process = new ProcessBuilder(command, "--version").start();
int exitCode = process.waitFor();
return exitCode == 0;
} catch (IOException | InterruptedException exceptions) {
return false;
}
}

private static boolean buildWithTool(String[] buildCommand) {
Log.info("Building the project using " + buildCommand[0] + ".");
ProcessBuilder processBuilder = new ProcessBuilder(buildCommand);
Expand Down Expand Up @@ -126,8 +166,13 @@ private static boolean mavenBuild(String projectPath) {

public static boolean gradleBuild(String projectPath) {
// Adjust Gradle command as needed
String gradleWrapper = projectPath + File.separator + GRADLE_CMD;
String[] gradleCommand = {gradleWrapper, "clean", "compileJava", "-p", projectPath};
String[] gradleCommand;
if (GRADLE_CMD.equals("gradlew") || GRADLE_CMD.equals("gradlew.bat")) {
gradleCommand = new String[]{projectPath + File.separator + GRADLE_CMD, "clean", "compileJava", "-p", projectPath};
}
else {
gradleCommand = new String[]{GRADLE_CMD, "clean", "compileJava", "-p", projectPath};
}
return buildWithTool(gradleCommand);
}

Expand Down Expand Up @@ -194,7 +239,14 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
Log.info("Found build.gradle[.kts] in the project directory. Using Gradle to download dependencies.");
tempInitScript = Files.writeString(tempInitScript, GRADLE_DEPENDENCIES_TASK);
String[] gradleCommand = {projectRoot + File.separator + GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir="+libDownloadPath.toString()};
String[] gradleCommand;
if (GRADLE_CMD.equals("gradlew") || GRADLE_CMD.equals("gradlew.bat")) {
gradleCommand = new String[]{projectRoot + File.separator + GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir=" + libDownloadPath.toString()};
}
else {
gradleCommand = new String[]{GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir=" + libDownloadPath.toString()};
}

System.out.println(Arrays.toString(gradleCommand));
return buildWithTool(gradleCommand);
}
Expand Down