1818
1919import static com .ibm .cldk .utils .ProjectDirectoryScanner .classFilesStream ;
2020import static com .ibm .cldk .CodeAnalyzer .projectRootPom ;
21+ import static com .ibm .cldk .CodeAnalyzer .noCleanDependencies ;
2122
2223public 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 }
0 commit comments