18
18
19
19
import static com .ibm .cldk .utils .ProjectDirectoryScanner .classFilesStream ;
20
20
import static com .ibm .cldk .CodeAnalyzer .projectRootPom ;
21
+ import static com .ibm .cldk .CodeAnalyzer .noCleanDependencies ;
21
22
22
23
public class BuildProject {
23
24
public static Path libDownloadPath ;
@@ -188,6 +189,17 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
188
189
return buildProject (projectPath , build ) ? classFilesStream (projectPath ) : new ArrayList <>();
189
190
}
190
191
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
+ }
191
203
/**
192
204
* Downloads library dependency jars of the given project so that the jars can be used
193
205
* for type resolution during symbol table creation.
@@ -198,17 +210,15 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
198
210
public static boolean downloadLibraryDependencies (String projectPath , String projectRootPom ) throws IOException {
199
211
// created download dir if it does not exist
200
212
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
+
210
214
File pomFile = new File (projectRoot , "pom.xml" );
211
215
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
+
212
222
if (MAVEN_CMD == null || !commandExists (new File (MAVEN_CMD )).getKey ()) {
213
223
String msg = MAVEN_CMD == null ?
214
224
"Could not find Maven or a valid Maven Wrapper" :
@@ -225,6 +235,12 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
225
235
return buildWithTool (mavenCommand );
226
236
} else if (new File (projectRoot , "build.gradle" ).exists () || new File (projectRoot , "build.gradle.kts" ).exists ()) {
227
237
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
+
228
244
String msg = GRADLE_CMD == null ?
229
245
"Could not find Gradle or valid Gradle Wrapper" :
230
246
MessageFormat .format ("Could not verify that {0} exists" , GRADLE_CMD );
@@ -249,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
249
265
}
250
266
251
267
public static void cleanLibraryDependencies () {
268
+ if (noCleanDependencies ) {
269
+ return ;
270
+ }
252
271
if (libDownloadPath != null ) {
253
272
Log .info ("Cleaning up library dependency directory: " + libDownloadPath );
254
273
try {
255
274
Files .walk (libDownloadPath ).filter (Files ::isRegularFile ).map (Path ::toFile ).forEach (File ::delete );
256
275
Files .delete (libDownloadPath );
257
276
} 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 ());
259
278
}
260
279
}
261
280
if (tempInitScript != null ) {
262
281
try {
263
282
Files .delete (tempInitScript );
264
283
} 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 ());
266
285
}
267
286
}
268
287
}
0 commit comments