Skip to content

Commit b32343b

Browse files
authored
Merge pull request #80 from IBM/1.0.7-dev
Fix issue #79
2 parents 7b7a451 + 9946546 commit b32343b

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version=1.0.6
1+
version=1.0.7-dev
2+

src/main/java/com/ibm/cldk/CodeAnalyzer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class CodeAnalyzer implements Runnable {
6969
@Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.")
7070
private static boolean noBuild = false;
7171

72-
@Option(names = {"-f", "--project-root-path"}, description = "Path to the root pom.xml file of the project.")
72+
@Option(names = {"-f", "--project-root-path"}, description = "Path to the root pom.xml/build.gradle file of the project.")
7373
public static String projectRootPom;
7474

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

111111
JsonObject combinedJsonObject = new JsonObject();
112112
Map<String, JavaCompilationUnit> symbolTable;
113+
projectRootPom = projectRootPom == null ? input : projectRootPom;
113114
// First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
114115
if (sourceAnalysis != null) {
115116
// Construct symbol table for source code

src/main/java/com/ibm/cldk/utils/BuildProject.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public class BuildProject {
2121
public static Path libDownloadPath;
2222
private static final String LIB_DEPS_DOWNLOAD_DIR = "_library_dependencies";
2323
private static final String MAVEN_CMD = BuildProject.getMavenCommand();
24+
private static final String GRADLE_CMD = BuildProject.getGradleCmd();
25+
26+
/**
27+
* Gets the maven command to be used for building the project.
28+
*
29+
* @return the maven command
30+
*/
2431
private static String getMavenCommand() {
2532
Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
2633
String mvnCommand;
@@ -32,7 +39,30 @@ private static String getMavenCommand() {
3239
return mvnCommand;
3340
}
3441

35-
private static final String GRADLE_CMD = System.getProperty("os.name").toLowerCase().contains("windows") ? "gradlew.bat" : "gradlew";
42+
/**
43+
* Gets the gradle command to be used for building the project.
44+
*
45+
* @return the gradle command
46+
*/
47+
private static String getGradleCmd() {
48+
String GRADLE_CMD;
49+
String osName = System.getProperty("os.name").toLowerCase();
50+
boolean isWindows = osName.contains("windows");
51+
String gradleWrapper = isWindows ? "gradlew.bat" : "gradlew";
52+
String gradle = isWindows ? "gradle.bat" : "gradle";
53+
54+
String gradleWrapperExists = new File(projectRootPom, gradleWrapper).exists() ? "true" : "false";
55+
56+
if (new File(projectRootPom, gradleWrapper).exists()) {
57+
GRADLE_CMD = gradleWrapper;
58+
} else if (commandExists(gradle)) {
59+
GRADLE_CMD = gradle;
60+
} else {
61+
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.");
62+
}
63+
return GRADLE_CMD;
64+
}
65+
3666
public static Path tempInitScript;
3767
static {
3868
try {
@@ -60,6 +90,16 @@ private static String getMavenCommand() {
6090
" }\n" +
6191
"}";
6292

93+
private static boolean commandExists(String command) {
94+
try {
95+
Process process = new ProcessBuilder(command, "--version").start();
96+
int exitCode = process.waitFor();
97+
return exitCode == 0;
98+
} catch (IOException | InterruptedException exceptions) {
99+
return false;
100+
}
101+
}
102+
63103
private static boolean buildWithTool(String[] buildCommand) {
64104
Log.info("Building the project using " + buildCommand[0] + ".");
65105
ProcessBuilder processBuilder = new ProcessBuilder(buildCommand);
@@ -126,8 +166,13 @@ private static boolean mavenBuild(String projectPath) {
126166

127167
public static boolean gradleBuild(String projectPath) {
128168
// Adjust Gradle command as needed
129-
String gradleWrapper = projectPath + File.separator + GRADLE_CMD;
130-
String[] gradleCommand = {gradleWrapper, "clean", "compileJava", "-p", projectPath};
169+
String[] gradleCommand;
170+
if (GRADLE_CMD.equals("gradlew") || GRADLE_CMD.equals("gradlew.bat")) {
171+
gradleCommand = new String[]{projectPath + File.separator + GRADLE_CMD, "clean", "compileJava", "-p", projectPath};
172+
}
173+
else {
174+
gradleCommand = new String[]{GRADLE_CMD, "clean", "compileJava", "-p", projectPath};
175+
}
131176
return buildWithTool(gradleCommand);
132177
}
133178

@@ -194,7 +239,14 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
194239
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
195240
Log.info("Found build.gradle[.kts] in the project directory. Using Gradle to download dependencies.");
196241
tempInitScript = Files.writeString(tempInitScript, GRADLE_DEPENDENCIES_TASK);
197-
String[] gradleCommand = {projectRoot + File.separator + GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir="+libDownloadPath.toString()};
242+
String[] gradleCommand;
243+
if (GRADLE_CMD.equals("gradlew") || GRADLE_CMD.equals("gradlew.bat")) {
244+
gradleCommand = new String[]{projectRoot + File.separator + GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir=" + libDownloadPath.toString()};
245+
}
246+
else {
247+
gradleCommand = new String[]{GRADLE_CMD, "--init-script", tempInitScript.toFile().getAbsolutePath(), "downloadDependencies", "-PoutputDir=" + libDownloadPath.toString()};
248+
}
249+
198250
System.out.println(Arrays.toString(gradleCommand));
199251
return buildWithTool(gradleCommand);
200252
}

0 commit comments

Comments
 (0)