diff --git a/gradle.properties b/gradle.properties index 619a0679..ed246e6c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ -version=1.1.0 +version=1.1.1 diff --git a/src/main/java/com/ibm/cldk/CodeAnalyzer.java b/src/main/java/com/ibm/cldk/CodeAnalyzer.java index d1ea7c3f..40d2ff57 100644 --- a/src/main/java/com/ibm/cldk/CodeAnalyzer.java +++ b/src/main/java/com/ibm/cldk/CodeAnalyzer.java @@ -76,6 +76,9 @@ public class CodeAnalyzer implements Runnable { @Option(names = {"-v", "--verbose"}, description = "Print logs to console.") private static boolean verbose = false; + @Option(names = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies") + public static boolean noCleanDependencies = false; + private static final String outputFileName = "analysis.json"; public static Gson gson = new GsonBuilder() diff --git a/src/main/java/com/ibm/cldk/utils/BuildProject.java b/src/main/java/com/ibm/cldk/utils/BuildProject.java index 9a426b4f..33c2a057 100644 --- a/src/main/java/com/ibm/cldk/utils/BuildProject.java +++ b/src/main/java/com/ibm/cldk/utils/BuildProject.java @@ -18,6 +18,7 @@ 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; @@ -188,6 +189,17 @@ public static List 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. @@ -198,17 +210,15 @@ public static List buildProjectAndStreamClassFiles(String projectPath, Str 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" : @@ -225,6 +235,12 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro 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()) { + 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); @@ -249,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()); } } } diff --git a/src/main/java/com/ibm/cldk/utils/ProjectDirectoryScanner.java b/src/main/java/com/ibm/cldk/utils/ProjectDirectoryScanner.java index d6178a15..fb138dca 100644 --- a/src/main/java/com/ibm/cldk/utils/ProjectDirectoryScanner.java +++ b/src/main/java/com/ibm/cldk/utils/ProjectDirectoryScanner.java @@ -7,6 +7,8 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; public class ProjectDirectoryScanner { public static List classFilesStream(String projectPath) throws IOException { @@ -37,6 +39,30 @@ public static List jarFilesStream(String projectPath) throws IOException { return null; } + /** + * Returns a stream of class files inside the jars and class files in the project. + * @param projectPath + * @return + * @throws IOException + */ + public static Stream classesFromJarFileStream(String projectPath) throws IOException { + List jarPaths = jarFilesStream(projectPath); + + if (jarPaths == null) { + return Stream.empty(); + } + + return jarPaths.stream().flatMap(jarPath -> { + try (ZipFile zip = new ZipFile(jarPath.toFile())) { + return zip.stream() + .filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class")) + .map(ZipEntry::getName); + } catch (IOException e) { + return Stream.empty(); + } + }); + } + public static List sourceFilesStream(String projectPath) throws IOException { Path projectDir = Paths.get(projectPath); Log.info("Finding *.java files in " + projectDir);