private boolean appendTaskToBomFile ( File buildGradleTmp ) { FileReader fileReader ; BufferedReader bufferedReader = null ; InputStream inputStream = null ; boolean hasDependencies = false ; try { // appending the task only if the build.gradle file has 'dependencies {' node (only at the beginning of the line) // otherwise, later when the task is ran it'll fail fileReader = new FileReader ( buildGradleTmp ) ; bufferedReader = new BufferedReader ( fileReader ) ; String currLine ; while ( ( currLine = bufferedReader . readLine ( ) ) != null ) { if ( currLine . indexOf ( DEPENDENCIES + Constants . WHITESPACE + CURLY_BRACKETS_OPEN ) == 0 || currLine . indexOf ( DEPENDENCIES + CURLY_BRACKETS_OPEN ) == 0 ) { hasDependencies = true ; break ; } } if ( hasDependencies ) { byte [ ] bytes ; List < String > lines = getDependenciesTree ( buildGradleTmp . getParent ( ) , buildGradleTmp . getParentFile ( ) . getName ( ) ) ; if ( lines != null ) { List < String > scopes = getScopes ( lines ) ; String copyDependenciesTask = Constants . NEW_LINE + TASK_COPY_DEPENDENCIES_HEADER + Constants . NEW_LINE ; for ( String scope : scopes ) { copyDependenciesTask = copyDependenciesTask . concat ( " from configurations." + scope + Constants . NEW_LINE ) ; } copyDependenciesTask = copyDependenciesTask . concat ( TASK_COPY_DEPENDENCIES_FOOTER + Constants . NEW_LINE + CURLY_BRACKTES_CLOSE ) ; bytes = copyDependenciesTask . getBytes ( ) ; } else { ClassLoader classLoader = Main . class . getClassLoader ( ) ; inputStream = classLoader . getResourceAsStream ( COPY_DEPENDENCIES_TASK_TXT ) ; bytes = IOUtils . toByteArray ( inputStream ) ; } if ( bytes . length > 0 ) { Files . write ( Paths . get ( buildGradleTmp . getPath ( ) ) , bytes , StandardOpenOption . APPEND ) ; } else if ( lines == null ) { logger . warn ( "Could not read {}" , COPY_DEPENDENCIES_TASK_TXT ) ; } else { logger . warn ( "Could not read dependencies' tree" ) ; } } } catch ( IOException e ) { logger . error ( "Could not write into the file {}, the cause {}" , buildGradleTmp . getPath ( ) , e . getMessage ( ) ) ; hasDependencies = false ; } try { if ( inputStream != null ) { inputStream . close ( ) ; } if ( bufferedReader != null ) { bufferedReader . close ( ) ; } } catch ( IOException e ) { logger . error ( "Could close the file, cause" , e . getMessage ( ) ) ; } return hasDependencies ; }
append new task to bom file