11package com .ibm .cldk .utils ;
22
3+ import com .ibm .cldk .CodeAnalyzer ;
4+
35import java .io .BufferedReader ;
46import java .io .File ;
57import java .io .IOException ;
1315import java .util .Arrays ;
1416import java .util .List ;
1517
16- import static com . ibm . cldk . CodeAnalyzer . projectRootPom ;
18+
1719import static com .ibm .cldk .utils .ProjectDirectoryScanner .classFilesStream ;
20+ import static com .ibm .cldk .CodeAnalyzer .projectRootPom ;
21+ import static com .ibm .cldk .CodeAnalyzer .noCleanDependencies ;
1822
1923public class BuildProject {
20-
2124 public static Path libDownloadPath ;
2225 private static final String LIB_DEPS_DOWNLOAD_DIR = "_library_dependencies" ;
2326 private static final String MAVEN_CMD = BuildProject .getMavenCommand ();
@@ -186,50 +189,67 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
186189 return buildProject (projectPath , build ) ? classFilesStream (projectPath ) : new ArrayList <>();
187190 }
188191
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+ }
189203 /**
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.
192206 *
193207 * @param projectPath Path to the project under analysis
194208 * @return true if dependency download succeeds; false otherwise
195209 */
196210 public static boolean downloadLibraryDependencies (String projectPath , String projectRootPom ) throws IOException {
197211 // created download dir if it does not exist
198212 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+
208214 File pomFile = new File (projectRoot , "pom.xml" );
209215 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+
210222 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 );
214226 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+ ));
219232 }
220233 Log .info ("Found pom.xml in the project directory. Using Maven to download dependencies." );
221234 String [] mavenCommand = {MAVEN_CMD , "--no-transfer-progress" , "-f" , Paths .get (projectRoot , "pom.xml" ).toString (), "dependency:copy-dependencies" , "-DoutputDirectory=" + libDownloadPath .toString ()};
222235 return buildWithTool (mavenCommand );
223236 } else if (new File (projectRoot , "build.gradle" ).exists () || new File (projectRoot , "build.gradle.kts" ).exists ()) {
224237 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 );
228247 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+ ));
233253 }
234254 Log .info ("Found build.gradle or build.gradle.kts in the project directory. Using Gradle to download dependencies." );
235255 tempInitScript = Files .writeString (tempInitScript , GRADLE_DEPENDENCIES_TASK );
@@ -245,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
245265 }
246266
247267 public static void cleanLibraryDependencies () {
268+ if (noCleanDependencies ) {
269+ return ;
270+ }
248271 if (libDownloadPath != null ) {
249272 Log .info ("Cleaning up library dependency directory: " + libDownloadPath );
250273 try {
251274 Files .walk (libDownloadPath ).filter (Files ::isRegularFile ).map (Path ::toFile ).forEach (File ::delete );
252275 Files .delete (libDownloadPath );
253276 } 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 ());
255278 }
256279 }
257280 if (tempInitScript != null ) {
258281 try {
259282 Files .delete (tempInitScript );
260283 } 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 ());
262285 }
263286 }
264287 }
0 commit comments