1
1
package com .ibm .cldk .utils ;
2
2
3
+ import com .ibm .cldk .CodeAnalyzer ;
4
+
3
5
import java .io .BufferedReader ;
4
6
import java .io .File ;
5
7
import java .io .IOException ;
13
15
import java .util .Arrays ;
14
16
import java .util .List ;
15
17
16
- import static com . ibm . cldk . CodeAnalyzer . projectRootPom ;
18
+
17
19
import static com .ibm .cldk .utils .ProjectDirectoryScanner .classFilesStream ;
20
+ import static com .ibm .cldk .CodeAnalyzer .projectRootPom ;
21
+ import static com .ibm .cldk .CodeAnalyzer .noCleanDependencies ;
18
22
19
23
public class BuildProject {
20
-
21
24
public static Path libDownloadPath ;
22
25
private static final String LIB_DEPS_DOWNLOAD_DIR = "_library_dependencies" ;
23
26
private static final String MAVEN_CMD = BuildProject .getMavenCommand ();
@@ -186,50 +189,67 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
186
189
return buildProject (projectPath , build ) ? classFilesStream (projectPath ) : new ArrayList <>();
187
190
}
188
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
+ }
189
203
/**
190
- * Downloads library dependency jars of the given project so that the jars
191
- * can be used for type resolution during symbol table creation.
204
+ * Downloads library dependency jars of the given project so that the jars can be used
205
+ * for type resolution during symbol table creation.
192
206
*
193
207
* @param projectPath Path to the project under analysis
194
208
* @return true if dependency download succeeds; false otherwise
195
209
*/
196
210
public static boolean downloadLibraryDependencies (String projectPath , String projectRootPom ) throws IOException {
197
211
// created download dir if it does not exist
198
212
String projectRoot = projectRootPom != null ? projectRootPom : projectPath ;
199
- libDownloadPath = Paths .get (projectPath , LIB_DEPS_DOWNLOAD_DIR ).toAbsolutePath ();
200
- if (!Files .exists (libDownloadPath )) {
201
- try {
202
- Files .createDirectory (libDownloadPath );
203
- } catch (IOException e ) {
204
- Log .error ("Error creating library dependency directory for " + projectPath + ": " + e .getMessage ());
205
- return false ;
206
- }
207
- }
213
+
208
214
File pomFile = new File (projectRoot , "pom.xml" );
209
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
+
210
222
if (MAVEN_CMD == null || !commandExists (new File (MAVEN_CMD )).getKey ()) {
211
- String msg = MAVEN_CMD == null
212
- ? "Could not find Maven or a valid Maven Wrapper"
213
- : MessageFormat .format ("Could not verify that {0} exists" , MAVEN_CMD );
223
+ String msg = MAVEN_CMD == null ?
224
+ "Could not find Maven or a valid Maven Wrapper" :
225
+ MessageFormat .format ("Could not verify that {0} exists" , MAVEN_CMD );
214
226
Log .error (msg );
215
- throw new IllegalStateException ("Unable to execute Maven command. "
216
- + (MAVEN_CMD == null
217
- ? "Could not find Maven or a valid Maven Wrapper"
218
- : "Attempt failed with message\n " + commandExists (new File (MAVEN_CMD )).getValue ()));
227
+ throw new IllegalStateException ("Unable to execute Maven command. " +
228
+ (MAVEN_CMD == null ?
229
+ "Could not find Maven or a valid Maven Wrapper" :
230
+ "Attempt failed with message\n " + commandExists (new File (MAVEN_CMD )).getValue ()
231
+ ));
219
232
}
220
233
Log .info ("Found pom.xml in the project directory. Using Maven to download dependencies." );
221
234
String [] mavenCommand = {MAVEN_CMD , "--no-transfer-progress" , "-f" , Paths .get (projectRoot , "pom.xml" ).toString (), "dependency:copy-dependencies" , "-DoutputDirectory=" + libDownloadPath .toString ()};
222
235
return buildWithTool (mavenCommand );
223
236
} else if (new File (projectRoot , "build.gradle" ).exists () || new File (projectRoot , "build.gradle.kts" ).exists ()) {
224
237
if (GRADLE_CMD == null || !commandExists (new File (GRADLE_CMD )).getKey ()) {
225
- String msg = GRADLE_CMD == null
226
- ? "Could not find Gradle or valid Gradle Wrapper"
227
- : MessageFormat .format ("Could not verify that {0} exists" , GRADLE_CMD );
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
+
244
+ String msg = GRADLE_CMD == null ?
245
+ "Could not find Gradle or valid Gradle Wrapper" :
246
+ MessageFormat .format ("Could not verify that {0} exists" , GRADLE_CMD );
228
247
Log .error (msg );
229
- throw new IllegalStateException ("Unable to execute Maven command. "
230
- + (GRADLE_CMD == null
231
- ? "Could not find Gradle or valid Gradle Wrapper"
232
- : "Attempt failed with message\n " + commandExists (new File (GRADLE_CMD )).getValue ()));
248
+ throw new IllegalStateException ("Unable to execute Maven command. " +
249
+ (GRADLE_CMD == null ?
250
+ "Could not find Gradle or valid Gradle Wrapper" :
251
+ "Attempt failed with message\n " + commandExists (new File (GRADLE_CMD )).getValue ()
252
+ ));
233
253
}
234
254
Log .info ("Found build.gradle or build.gradle.kts in the project directory. Using Gradle to download dependencies." );
235
255
tempInitScript = Files .writeString (tempInitScript , GRADLE_DEPENDENCIES_TASK );
@@ -245,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
245
265
}
246
266
247
267
public static void cleanLibraryDependencies () {
268
+ if (noCleanDependencies ) {
269
+ return ;
270
+ }
248
271
if (libDownloadPath != null ) {
249
272
Log .info ("Cleaning up library dependency directory: " + libDownloadPath );
250
273
try {
251
274
Files .walk (libDownloadPath ).filter (Files ::isRegularFile ).map (Path ::toFile ).forEach (File ::delete );
252
275
Files .delete (libDownloadPath );
253
276
} catch (IOException e ) {
254
- Log .error ( "Error deleting library dependency directory: " + e .getMessage ());
277
+ Log .warn ( "Unable to fully delete library dependency directory: " + e .getMessage ());
255
278
}
256
279
}
257
280
if (tempInitScript != null ) {
258
281
try {
259
282
Files .delete (tempInitScript );
260
283
} catch (IOException e ) {
261
- Log .error ("Error deleting temporary Gradle init script: " + e .getMessage ());
284
+ Log .warn ("Error deleting temporary Gradle init script: " + e .getMessage ());
262
285
}
263
286
}
264
287
}
0 commit comments