Skip to content

Commit

Permalink
Mirror fix for issue 74 and PR 99 as version 2.0.2
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Krishna <[email protected]>
  • Loading branch information
rahlk committed Feb 3, 2025
1 parent edc2606 commit eb040d7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.0.1
version=2.0.2
3 changes: 3 additions & 0 deletions src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ 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 = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies")
public static boolean noCleanDependencies = false;

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

Expand Down
81 changes: 52 additions & 29 deletions src/main/java/com/ibm/cldk/utils/BuildProject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ibm.cldk.utils;

import com.ibm.cldk.CodeAnalyzer;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -13,11 +15,12 @@
import java.util.Arrays;
import java.util.List;

import static com.ibm.cldk.CodeAnalyzer.projectRootPom;

import static com.ibm.cldk.utils.ProjectDirectoryScanner.classFilesStream;
import static com.ibm.cldk.CodeAnalyzer.projectRootPom;
import static com.ibm.cldk.CodeAnalyzer.noCleanDependencies;

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();
Expand Down Expand Up @@ -186,50 +189,67 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
return buildProject(projectPath, build) ? classFilesStream(projectPath) : new ArrayList<>();
}

private static boolean mkLibDepDirs(String projectPath) {
if (!Files.exists(libDownloadPath)) {
try {
Files.createDirectories(libDownloadPath);
} catch (IOException e) {
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
return false;
}
}
return true;
}
/**
* Downloads library dependency jars of the given project so that the jars
* can be used for type resolution during symbol table creation.
* Downloads library dependency jars of the given project so that the jars can be used
* for type resolution during symbol table creation.
*
* @param projectPath Path to the project under analysis
* @return true if dependency download succeeds; false otherwise
*/
public static boolean downloadLibraryDependencies(String projectPath, String projectRootPom) throws IOException {
// created download dir if it does not exist
String projectRoot = projectRootPom != null ? projectRootPom : projectPath;
libDownloadPath = Paths.get(projectPath, LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (!Files.exists(libDownloadPath)) {
try {
Files.createDirectory(libDownloadPath);
} catch (IOException e) {
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
return false;
}
}

File pomFile = new File(projectRoot, "pom.xml");
if (pomFile.exists()) {
libDownloadPath = Paths.get(projectPath, "target", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (mkLibDepDirs(projectPath))
Log.debug("Dependencies found/created in " + libDownloadPath);
else
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);

if (MAVEN_CMD == null || !commandExists(new File(MAVEN_CMD)).getKey()) {
String msg = MAVEN_CMD == null
? "Could not find Maven or a valid Maven Wrapper"
: MessageFormat.format("Could not verify that {0} exists", MAVEN_CMD);
String msg = MAVEN_CMD == null ?
"Could not find Maven or a valid Maven Wrapper" :
MessageFormat.format("Could not verify that {0} exists", MAVEN_CMD);
Log.error(msg);
throw new IllegalStateException("Unable to execute Maven command. "
+ (MAVEN_CMD == null
? "Could not find Maven or a valid Maven Wrapper"
: "Attempt failed with message\n" + commandExists(new File(MAVEN_CMD)).getValue()));
throw new IllegalStateException("Unable to execute Maven command. " +
(MAVEN_CMD == null ?
"Could not find Maven or a valid Maven Wrapper" :
"Attempt failed with message\n" + commandExists(new File(MAVEN_CMD)).getValue()
));
}
Log.info("Found pom.xml in the project directory. Using Maven to download dependencies.");
String[] mavenCommand = {MAVEN_CMD, "--no-transfer-progress", "-f", Paths.get(projectRoot, "pom.xml").toString(), "dependency:copy-dependencies", "-DoutputDirectory=" + libDownloadPath.toString()};
return buildWithTool(mavenCommand);
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
if (GRADLE_CMD == null || !commandExists(new File(GRADLE_CMD)).getKey()) {
String msg = GRADLE_CMD == null
? "Could not find Gradle or valid Gradle Wrapper"
: MessageFormat.format("Could not verify that {0} exists", GRADLE_CMD);
libDownloadPath = Paths.get(projectPath, "build", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (mkLibDepDirs(projectPath))
Log.debug("Dependencies found/created in " + libDownloadPath);
else
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);

String msg = GRADLE_CMD == null ?
"Could not find Gradle or valid Gradle Wrapper" :
MessageFormat.format("Could not verify that {0} exists", GRADLE_CMD);
Log.error(msg);
throw new IllegalStateException("Unable to execute Maven command. "
+ (GRADLE_CMD == null
? "Could not find Gradle or valid Gradle Wrapper"
: "Attempt failed with message\n" + commandExists(new File(GRADLE_CMD)).getValue()));
throw new IllegalStateException("Unable to execute Maven command. " +
(GRADLE_CMD == null ?
"Could not find Gradle or valid Gradle Wrapper" :
"Attempt failed with message\n" + commandExists(new File(GRADLE_CMD)).getValue()
));
}
Log.info("Found build.gradle or build.gradle.kts in the project directory. Using Gradle to download dependencies.");
tempInitScript = Files.writeString(tempInitScript, GRADLE_DEPENDENCIES_TASK);
Expand All @@ -245,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
}

public static void cleanLibraryDependencies() {
if (noCleanDependencies) {
return;
}
if (libDownloadPath != null) {
Log.info("Cleaning up library dependency directory: " + libDownloadPath);
try {
Files.walk(libDownloadPath).filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
Files.delete(libDownloadPath);
} catch (IOException e) {
Log.error("Error deleting library dependency directory: " + e.getMessage());
Log.warn("Unable to fully delete library dependency directory: " + e.getMessage());
}
}
if (tempInitScript != null) {
try {
Files.delete(tempInitScript);
} catch (IOException e) {
Log.error("Error deleting temporary Gradle init script: " + e.getMessage());
Log.warn("Error deleting temporary Gradle init script: " + e.getMessage());
}
}
}
Expand Down

0 comments on commit eb040d7

Please sign in to comment.