Skip to content

Commit 412d3d4

Browse files
authored
Merge pull request #99 from IBM/fix-issue-74
Fix issue 74
2 parents 5cf8398 + 86c5bca commit 412d3d4

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=1.1.0
1+
version=1.1.1
22

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public class CodeAnalyzer implements Runnable {
7676
@Option(names = {"-v", "--verbose"}, description = "Print logs to console.")
7777
private static boolean verbose = false;
7878

79+
@Option(names = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies")
80+
public static boolean noCleanDependencies = false;
81+
7982
private static final String outputFileName = "analysis.json";
8083

8184
public static Gson gson = new GsonBuilder()

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.ibm.cldk.utils.ProjectDirectoryScanner.classFilesStream;
2020
import static com.ibm.cldk.CodeAnalyzer.projectRootPom;
21+
import static com.ibm.cldk.CodeAnalyzer.noCleanDependencies;
2122

2223
public class BuildProject {
2324
public static Path libDownloadPath;
@@ -188,6 +189,17 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
188189
return buildProject(projectPath, build) ? classFilesStream(projectPath) : new ArrayList<>();
189190
}
190191

192+
private static boolean mkLibDepDirs(String projectPath) {
193+
if (!Files.exists(libDownloadPath)) {
194+
try {
195+
Files.createDirectories(libDownloadPath);
196+
} catch (IOException e) {
197+
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
198+
return false;
199+
}
200+
}
201+
return true;
202+
}
191203
/**
192204
* Downloads library dependency jars of the given project so that the jars can be used
193205
* for type resolution during symbol table creation.
@@ -198,17 +210,15 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
198210
public static boolean downloadLibraryDependencies(String projectPath, String projectRootPom) throws IOException {
199211
// created download dir if it does not exist
200212
String projectRoot = projectRootPom != null ? projectRootPom : projectPath;
201-
libDownloadPath = Paths.get(projectPath, LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
202-
if (!Files.exists(libDownloadPath)) {
203-
try {
204-
Files.createDirectory(libDownloadPath);
205-
} catch (IOException e) {
206-
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
207-
return false;
208-
}
209-
}
213+
210214
File pomFile = new File(projectRoot, "pom.xml");
211215
if (pomFile.exists()) {
216+
libDownloadPath = Paths.get(projectPath, "target", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
217+
if (mkLibDepDirs(projectPath))
218+
Log.debug("Dependencies found/created in " + libDownloadPath);
219+
else
220+
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);
221+
212222
if (MAVEN_CMD == null || !commandExists(new File(MAVEN_CMD)).getKey()) {
213223
String msg = MAVEN_CMD == null ?
214224
"Could not find Maven or a valid Maven Wrapper" :
@@ -225,6 +235,12 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
225235
return buildWithTool(mavenCommand);
226236
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
227237
if (GRADLE_CMD == null || !commandExists(new File(GRADLE_CMD)).getKey()) {
238+
libDownloadPath = Paths.get(projectPath, "build", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
239+
if (mkLibDepDirs(projectPath))
240+
Log.debug("Dependencies found/created in " + libDownloadPath);
241+
else
242+
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);
243+
228244
String msg = GRADLE_CMD == null ?
229245
"Could not find Gradle or valid Gradle Wrapper" :
230246
MessageFormat.format("Could not verify that {0} exists", GRADLE_CMD);
@@ -249,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
249265
}
250266

251267
public static void cleanLibraryDependencies() {
268+
if (noCleanDependencies) {
269+
return;
270+
}
252271
if (libDownloadPath != null) {
253272
Log.info("Cleaning up library dependency directory: " + libDownloadPath);
254273
try {
255274
Files.walk(libDownloadPath).filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
256275
Files.delete(libDownloadPath);
257276
} catch (IOException e) {
258-
Log.error("Error deleting library dependency directory: " + e.getMessage());
277+
Log.warn("Unable to fully delete library dependency directory: " + e.getMessage());
259278
}
260279
}
261280
if (tempInitScript != null) {
262281
try {
263282
Files.delete(tempInitScript);
264283
} catch (IOException e) {
265-
Log.error("Error deleting temporary Gradle init script: " + e.getMessage());
284+
Log.warn("Error deleting temporary Gradle init script: " + e.getMessage());
266285
}
267286
}
268287
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.List;
88
import java.util.stream.Collectors;
99
import java.util.stream.Stream;
10+
import java.util.zip.ZipEntry;
11+
import java.util.zip.ZipFile;
1012

1113
public class ProjectDirectoryScanner {
1214
public static List<Path> classFilesStream(String projectPath) throws IOException {
@@ -37,6 +39,30 @@ public static List<Path> jarFilesStream(String projectPath) throws IOException {
3739
return null;
3840
}
3941

42+
/**
43+
* Returns a stream of class files inside the jars and class files in the project.
44+
* @param projectPath
45+
* @return
46+
* @throws IOException
47+
*/
48+
public static Stream<String> classesFromJarFileStream(String projectPath) throws IOException {
49+
List<Path> jarPaths = jarFilesStream(projectPath);
50+
51+
if (jarPaths == null) {
52+
return Stream.empty();
53+
}
54+
55+
return jarPaths.stream().flatMap(jarPath -> {
56+
try (ZipFile zip = new ZipFile(jarPath.toFile())) {
57+
return zip.stream()
58+
.filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class"))
59+
.map(ZipEntry::getName);
60+
} catch (IOException e) {
61+
return Stream.empty();
62+
}
63+
});
64+
}
65+
4066
public static List<Path> sourceFilesStream(String projectPath) throws IOException {
4167
Path projectDir = Paths.get(projectPath);
4268
Log.info("Finding *.java files in " + projectDir);

0 commit comments

Comments
 (0)