@Override
    public Collection<AgentProjectInfo> collectDependencies(String rootDirectory) {
        Collection<AgentProjectInfo> projects = new ArrayList<>();
        if (!this.isMavenExist(rootDirectory)) {
            logger.warn("Please install maven");
        } else {
            if (runPreStep) {
                try {
                    CommandLineProcess mvnCleanInstall = new CommandLineProcess(rootDirectory, getCleanInstallCommandParams());
                    mvnCleanInstall.executeProcess();
                    if (mvnCleanInstall.isErrorInProcess()) {
                        logger.warn("Failed to execute the command {}", getCleanInstallCommandParams());
                    }

                } catch (Exception e) {
                    logger.warn("Error while execute dependencies after running {} on {}, {}", getCleanInstallCommandParams(), rootDirectory, e.getMessage());
                    logger.debug("Error: {}", e.getStackTrace());
                }
            }

            if (StringUtils.isBlank(M2Path)) {
                this.M2Path = getMavenM2Path(Constants.DOT);
            }

            try {
                CommandLineProcess mvnDependencies = new CommandLineProcess(rootDirectory, getLsCommandParamsBatchMode());
                List<String> lines = mvnDependencies.executeProcess();

                if (mvnDependencies.isErrorInProcess()) {
                    logger.debug("Failed to execute the command {}", getLsCommandParamsBatchMode());
                    mvnDependencies = new CommandLineProcess(rootDirectory, getLsCommandParams());
                    lines = mvnDependencies.executeProcess();
                }
                // set flag of errors, in case we do not have errors  we do not want to parse direct dependencies from pom later on.
                if (mvnDependencies.isErrorInProcess()) {
                    this.errorsRunningDependencyTree = true;
                }
                if (!mvnDependencies.isErrorInProcess() || mavenIgnoreDependencyTreeErrors) {
                    List<Node> nodes = mavenLinesParser.parseLines(lines);

                    logger.info("End parsing pom files , found : " + String.join(Constants.COMMA,
                            nodes.stream().map(node -> node.getArtifactId()).collect(Collectors.toList())));

                    projects = nodes.stream()
                            .filter(node -> !this.ignorePomModules || (ignorePomModules && !node.getPackaging().equals(Constants.POM)))
                            .map(tree -> {
                                Map<String, List<DependencyInfo>> pathToDependenciesMap = new HashMap<>();
                                List<DependencyInfo> dependencies = new LinkedList<>();
                                Stream<Node> nodeStream = tree.getChildNodes().stream().filter(node -> !mavenIgnoredScopes.contains(node.getScope()));
                                dependencies.addAll(nodeStream.map(node -> getDependencyFromNode(node, pathToDependenciesMap)).collect(Collectors.toList()));
                                Map<String, String> pathToSha1Map = pathToDependenciesMap.keySet().stream().distinct().parallel().collect(Collectors.toMap(file -> file, file -> getSha1(file)));
                                pathToSha1Map.entrySet().forEach(pathSha1Pair -> pathToDependenciesMap.get(pathSha1Pair.getKey()).stream().forEach(dependency -> {
                                    dependency.setSha1(pathSha1Pair.getValue());
                                    dependency.setSystemPath(pathSha1Pair.getKey());
                                }));
                                AgentProjectInfo projectInfo = new AgentProjectInfo();
                                projectInfo.setCoordinates(new Coordinates(tree.getGroupId(), tree.getArtifactId(), tree.getVersion()));
                                logger.debug("Project/Module coordinates: {}", projectInfo.getCoordinates().toString());
                                logger.debug("Total project direct dependencies found : {}", dependencies.size());
                                dependencies.stream().filter(dependency -> StringUtils.isNotEmpty(dependency.getSha1()) ||
                                        (StringUtils.isNotEmpty(dependency.getGroupId()) && StringUtils.isNotEmpty(dependency.getArtifactId())
                                                && StringUtils.isNotEmpty(dependency.getVersion()))).forEach(dependency ->
                                        projectInfo.getDependencies().add(dependency));
                                logger.debug("ProjectInfo direct dependency added : {}", projectInfo.getDependencies().size());
                                return projectInfo;
                            }).collect(Collectors.toList());
                } else {
                    logger.warn("Failed to scan and send {}", getLsCommandParams()); //either dead code? supposed to be up there..
                }
            } catch (IOException e) {
                logger.warn("Error getting dependencies after running {} on {}, {}", getLsCommandParams(), rootDirectory, e.getMessage());
                logger.debug("Error: {}", e.getStackTrace());
            }

            if (projects != null && projects.isEmpty()) {
                if (!showMavenTreeError) {
                    logger.warn("Failed to getting dependencies after running '{}'", getLsCommandParams());
                    showMavenTreeError = true;
                }
            }
        }
        return projects;
    }
@ Override public Collection < AgentProjectInfo > collectDependencies ( String rootDirectory ) { Collection < AgentProjectInfo > projects = new ArrayList < > ( ) ; if ( ! this . isMavenExist ( rootDirectory ) ) { logger . warn ( "Please install maven" ) ; } else { if ( runPreStep ) { try { CommandLineProcess mvnCleanInstall = new CommandLineProcess ( rootDirectory , getCleanInstallCommandParams ( ) ) ; mvnCleanInstall . executeProcess ( ) ; if ( mvnCleanInstall . isErrorInProcess ( ) ) { logger . warn ( "Failed to execute the command {}" , getCleanInstallCommandParams ( ) ) ; } } catch ( Exception e ) { logger . warn ( "Error while execute dependencies after running {} on {}, {}" , getCleanInstallCommandParams ( ) , rootDirectory , e . getMessage ( ) ) ; logger . debug ( "Error: {}" , e . getStackTrace ( ) ) ; } } if ( StringUtils . isBlank ( M2Path ) ) { this . M2Path = getMavenM2Path ( Constants . DOT ) ; } try { CommandLineProcess mvnDependencies = new CommandLineProcess ( rootDirectory , getLsCommandParamsBatchMode ( ) ) ; List < String > lines = mvnDependencies . executeProcess ( ) ; if ( mvnDependencies . isErrorInProcess ( ) ) { logger . debug ( "Failed to execute the command {}" , getLsCommandParamsBatchMode ( ) ) ; mvnDependencies = new CommandLineProcess ( rootDirectory , getLsCommandParams ( ) ) ; lines = mvnDependencies . executeProcess ( ) ; } if ( mvnDependencies . isErrorInProcess ( ) ) { this . errorsRunningDependencyTree = true ; } if ( ! mvnDependencies . isErrorInProcess ( ) || mavenIgnoreDependencyTreeErrors ) { List < Node > nodes = mavenLinesParser . parseLines ( lines ) ; logger . info ( "End parsing pom files , found : " + String . join ( Constants . COMMA , nodes . stream ( ) . map ( node -> node . getArtifactId ( ) ) . collect ( Collectors . toList ( ) ) ) ) ; projects = nodes . stream ( ) . filter ( node -> ! this . ignorePomModules || ( ignorePomModules && ! node . getPackaging ( ) . equals ( Constants . POM ) ) ) . map ( tree -> { Map < String , List < DependencyInfo > > pathToDependenciesMap = new HashMap < > ( ) ; List < DependencyInfo > dependencies = new LinkedList < > ( ) ; Stream < Node > nodeStream = tree . getChildNodes ( ) . stream ( ) . filter ( node -> ! mavenIgnoredScopes . contains ( node . getScope ( ) ) ) ; dependencies . addAll ( nodeStream . map ( node -> getDependencyFromNode ( node , pathToDependenciesMap ) ) . collect ( Collectors . toList ( ) ) ) ; Map < String , String > pathToSha1Map = pathToDependenciesMap . keySet ( ) . stream ( ) . distinct ( ) . parallel ( ) . collect ( Collectors . toMap ( file -> file , file -> getSha1 ( file ) ) ) ; pathToSha1Map . entrySet ( ) . forEach ( pathSha1Pair -> pathToDependenciesMap . get ( pathSha1Pair . getKey ( ) ) . stream ( ) . forEach ( dependency -> { dependency . setSha1 ( pathSha1Pair . getValue ( ) ) ; dependency . setSystemPath ( pathSha1Pair . getKey ( ) ) ; } ) ) ; AgentProjectInfo projectInfo = new AgentProjectInfo ( ) ; projectInfo . setCoordinates ( new Coordinates ( tree . getGroupId ( ) , tree . getArtifactId ( ) , tree . getVersion ( ) ) ) ; logger . debug ( "Project/Module coordinates: {}" , projectInfo . getCoordinates ( ) . toString ( ) ) ; logger . debug ( "Total project direct dependencies found : {}" , dependencies . size ( ) ) ; dependencies . stream ( ) . filter ( dependency -> StringUtils . isNotEmpty ( dependency . getSha1 ( ) ) || ( StringUtils . isNotEmpty ( dependency . getGroupId ( ) ) && StringUtils . isNotEmpty ( dependency . getArtifactId ( ) ) && StringUtils . isNotEmpty ( dependency . getVersion ( ) ) ) ) . forEach ( dependency -> projectInfo . getDependencies ( ) . add ( dependency ) ) ; logger . debug ( "ProjectInfo direct dependency added : {}" , projectInfo . getDependencies ( ) . size ( ) ) ; return projectInfo ; } ) . collect ( Collectors . toList ( ) ) ; } else { logger . warn ( "Failed to scan and send {}" , getLsCommandParams ( ) ) ; } } catch ( IOException e ) { logger . warn ( "Error getting dependencies after running {} on {}, {}" , getLsCommandParams ( ) , rootDirectory , e . getMessage ( ) ) ; logger . debug ( "Error: {}" , e . getStackTrace ( ) ) ; } if ( projects != null && projects . isEmpty ( ) ) { if ( ! showMavenTreeError ) { logger . warn ( "Failed to getting dependencies after running '{}'" , getLsCommandParams ( ) ) ; showMavenTreeError = true ; } } } return projects ; }
/* --- Public methods ---